Presentation on theme: "The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command."— Presentation transcript:
The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command line and makes arrangements for its execution. Generally waits for command to complete execution (locking the terminal). A variety of shells to choose from: Bourne, Korn, C, Bash, Tcsh Killed on logging out.
The Shell Metacharacters Wildcard characters such as *, ?, [, ], ! Redirection characters such as >, < The pipe character, | Command substitution characters ` ` The $ as a variable prefix
cat chap* Shell expands * to match all filenames in the current directory that begin with chap. date > foo Shell sees the > first, opens the file foo and connects the date output to it. who | sort Shell understands the strings on either side of the | as two separate programs and connects them. ls`cat foo` Shell first runs cat and supplies the output as arguments to ls. echo $HOME Evaluates $HOME as a variable before running echo. Examples of Shell Behavior
Wildcards Set of metacharacters used in an expression to match multiple but similar filenames. * means any sequence of characters (including none) ? means a single character [ ] means any characters specified in the bracket ! means not - means a range of characters or numbers Shell creates list of filenames before allowing command to run. Expansion can be prevented by quoting and escaping. Filenames must not contain wild-card characters.
Some Wildcard Examples ls *.lst Lists all files with extension.lst. rm ??* Removes all files with at least 2 characters. cp *.[ch] cprogs Copies all files with.c or.h extension into cprogs. rm *[!a-zA-Z]* Removes files not containing at least one letter. cat note[0-1][0-9] Displays files note00, … note19 rm *.o Removes all files then removes all files with.o extension (an error). Watch out!
Escaping (Using a \ Before a Character) Escaping reverses the usual meaning of metacharacter following it. Example: rm \* removes a file named * Can also protect itself. Example: echo \\ displays a \ Protects space and [Enter]. Example: cd My\ Documents changes to the directory named My Documents Inconvenient to use when command line contains too many metacharacters that need to be escaped.
Quoting Quoting protects most metacharacters from interpretation by the shell. Example: echo “*” prints a * More convenient than escaping when protecting a group of metacharacters. Quoted string understood as a single argument by shell and C programs. (a.out foo “My Documents” has 2 arguments and not 3.) Double quotes and single quotes are not equivalent. Example: echo “$SHELL” is not the same as echo ‘$SHELL’ Quoting doesn’t protect the \ metacharacter. Escaping is required. Quoting
Be Careful Quoting Special Characters Like “, ‘ and \ Try these: echo ‘ ‘ ‘ echo “ “ “ echo ‘ “ ‘ echo “ ‘ “ echo ‘ \ ‘ echo “ \ “
File Handling in the Shell A file is opened by accessing it by its pathname. Opening returns a file descriptor (an integer). Subsequent read/write operations on the file use the descriptor. Kernel allocates lowest available number as descriptor. First three descriptors (0, 1 and 2) are always allocated : 0 is standard input, 1 is standard output, 2 is standard error
Standard Input Uses file descriptor 0. By default, assigned to the user’s terminal keyboard. Can also be obtained from standard output of another program (redirection). Commands using redirection continue to read descriptor 0 and have no knowledge of this manipulation.
Standard Output Uses file descriptor 1. By default, assigned to the user’s display terminal. Can also be used as input to another program (redirection). Commands using redirection continue to write to descriptor 1 and have no knowledge of this manipulation.
Standard Error Uses file descriptor 2. By default, file is assigned to the user’s display terminal. Standard error output can’t be used as input to another program but error output can be redirected to a file (redirection). Commands using redirection continue to write error messages to descriptor 2 and have no knowledge of this manipulation.
Redirection Using, and >> Redirection is a shell feature for manipulating command input and output. Most commands designed to write output to standard output. Most commands designed to take input from standard input when used without a filename as argument. Shell can assign these files to disk files through redirection: wc prog.c > countfile wc prog.c >> countfile wc < prog.c wc prog.c > countfile 2> errorfile Commands themselves have no knowledge of the reassignment.
Redirection Using a Pipe | Connects standard output on one command to standard input of another Example: who | wc –l Displays # users on system No temporary file is created. Used to solve many text manipulation problems. Commands have no knowledge that they are reading from or writing to a pipe. tee command duplicates its input and sends to both file and standard out Example : ls | tee filelist List files on screen and in a file.
Two More Special Files to Redirect Output /dev/null basically sends out nowhere. File size is always zero. Examples: cat myfile > /dev/null Command does nothing! find / -name myfile –print 2> /dev/null This command hunts for file named myfile starting at root and lists it. Send all error messages nowhere. /dev/tty is the file indicating your terminal (not the same file as standard output or standard error!) Example: who > /dev/tty Lists users at your terminal.
Command Substitution Allows command arguments to be obtained from standard output of another command. In command1 `command2`, command2 is run first and its standard output used as arguments to command1. Example: echo Today is `date` Command enclosed by ` ` (backquotes) must write to standard output. Convenient mechanism for running commands whose arguments are known only at runtime. Example: echo “There are `ls | wc –l` files in this directory.” (Use double quotes around echo, not single quotes.)
Shell Variables Users define shell variables as variable=value. Referenced with $. Environment variables such as SHELL, HOME, and PATH are shell variables. Example: echo $HOME Variables have no type and need no declaration before being used. All shell variables initialized to null ( x=“” or x=‘’ or x= ). To remove a shell variable, use unset. Example: unset x To prevent changes to a shell variable, use readonly. Example: readonly pi
Using Shell Variables Set a variable x to 5 and display the value: x=5 ; echo $x (no whitespace on either side of =) x = 5 is illegal; x is interpreted as a command to run. Can be set from command substitution: Example: directory=`pwd` Useful for storing pathnames that are repeatedly used in a shell script: Example: addressbook=$HOME/data/addressbook