Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 1
Lecture 3
Log into Linux Questions about Homework 1? Reminder: Additional on-line references
Lecture 3 Log into Linux Questions about Homework 1? Reminder: - - PowerPoint PPT Presentation
Lecture 3 Log into Linux Questions about Homework 1? Reminder: Additional on-line references Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 1 Outline Filesystems BASH - Bourne Again SHell Redirection
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 1
Log into Linux Questions about Homework 1? Reminder: Additional on-line references
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 2
Filesystems BASH - Bourne Again SHell Redirection BASH programming
Variables and environment Selection and repetition Positional parameters
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 3
Disk devices (/dev/sdc1) contain raw disk data
A filesystem contains inode lists and data blocks. An inode structure (on disk) contains all file info except the
A directory block has only filename and inode # pairs. Use “ls -i” to display inode numbers. Use mkfs (as administrator) to create a filesystem (format):
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 4
All files in a UNIX systems are arranged in a
No C:, D:, etc drives as in Windows. Easily add disk space and allow files to maintain the
mount is used to attach a filesystem to the tree
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 5
List of automatically mounted devices is in
Use df (diskfree) to display the amount of
umount (not unmount) detachs a filesystem Many systems have an automounter program
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 6
/ root directory (don't confuse with /root) /bin essential utilities /lib essential libraries /sbin essential admin tools /etc configuration files /home contains user HOME directories /root root user HOME directory /dev device directory /var system files that change (log, spool files) /tmp for temp file usage (avail to all users) /usr/bin user applications /usr/lib user application libraries /usr/local contains applications added by local admin
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 7
The shell is a command interpreter and a full-
Several different shells are available: sh, bash, csh, zsh, ksh Change your default shell to bash
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 8
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 9
Start with a she-bang (#!), then name of shell. The kernel will pass the script to the proper
Comments begin with a # Make the script executable OR you can also
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 10
Wildcard expansion is done by the shell; not
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 11
Every program automatically has three
In C, the FILE streams are stdin, stdout, and
In C++, the IO streams are cin, cout, cerr. By default, they are connected to the keyboard,
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 12
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 13
$ ./redirect Hi there stdout: Hi there stderr: Hi there $ ./redirect < /etc/passwd stdout: root:x:0:0:root:/root:/bin/bash stderr: root:x:0:0:root:/root:/bin/bash $ ./redirect < /etc/passwd > /dev/null stderr: root:x:0:0:root:/root:/bin/bash $ ./redirect < /etc/passwd 2> /dev/null stdout: root:x:0:0:root:/root:/bin/bash $ ./redirect < /etc/passwd 2> /dev/null > output.txt $ cat output.txt stdout: root:x:0:0:root:/root:/bin/bash
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 14
To redirect both standard output and error:
Shell script input and output is redirected in a
To redirect output from standard output to
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 15
A pipe (|) connects the standard output of the
Here's an example that displays all usernames
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 16
Many commands are built into the shell: cd,
Many other commands are external programs:
There are both built-in and external versions of
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 17
All shell variables are strings:
Variable substitution occurs within double
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 18
Each program owns an area of memory called
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 19
Program exit status can be used as a condition
The test command (there are built-in and
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 20
File conditionals are unary. E.g.,
String comparison
Arithmetic comparison
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 21
Use “help test” to find out more about the test
The commands “true” and “false” have exit
Here is the C++ code equivalent for true and
true is especially useful as a loop condition
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 22
The commands if and case are available
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 23
Shell variables $1, $2, ..., ${10}, etc. refer to
The set command can be used to assign to the
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 24
$* and $@ represent all of the positional
The shift command shifts all of the parameters
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 25
The commands while and until are available
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 26
The for command executes for each value in a
Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 27
Without a list, the for command defaults to the