Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang

Similar presentations


Presentation on theme: "CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang"— Presentation transcript:

1 CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang chang@cs.twsu.edu chang@cs.twsu.edu

2 /dev/null and /dev/tty: Two Special Files Quite often, you may want to test whether a program runs successfully without seeing the output or the error messages on the screen. You have a special file that simply accepts any stream without growing in size – the file /dev/null: $ cal chap100 2>/dev/null $ cat /dev/null

3 Pipes $ who > user.lst $ wc –l < user.lst The method of using two commands in sequence has certain obvious disadvantages: –The process is slow. –The intermediate file is need. –Temporary files can be very large. You can use the | (pipe) to connect two commands. $ who | wc -l

4 Pipes who is said to be piped to wc. When a sequence of commands combined together with one or more | symbols, a pipeline is said to be formed. You can use the col –b command to remove control characters from the manual pages. man grep | col –b > grep.man grep locates lines containing a specified pattern in its input. $ cat * | grep “print”

5 tee: Splitting a Stream The UNIX tee command breaks up its input into two components; one component is saved in a file, and the other is connected to the standard output. Being also a filter (uses standard input and standard output), tee can be placed anywhere in a pipeline. $ who | tee user.lst $ who | tee user.lst | wc -l

6 Command Substitution The shell enables the argument of a command to be obtained from the standard output of another. This feature is called command substitution. When scanning the command line, the ` (backquote or backtick) is another metacharacter that the shell looks for.

7 Command Substitution If the ` pair is found, the shell executed the command enclosed in the ` pair, and replaces the enclosed command line with the output of the command. $ echo Today is `date` Today is Thu Oct 11 22:08:36 CDT 2001 You can use this feature to generate useful message. $ echo “There are `ls | wc -l` files”

8 Shell Variables A shell variable is of the string type, which means that the value is stored in ASCII rather than in binary format. A shell variable take on the generalized form variable=value (except in the C shell). $ x = 37; echo $x 37 $ unset x; echo $x

9 Shell Variables The C shell uses the set statement set variables. set x = 10 You can set a pathname or a command to a variable or substitute to set the variable. $ dir=“ls -Fax”; $dir $ mydir=`pwd`; echo $mydir

10 Shell Scripts Try executing the file containing these commands by invoking the filename: $ date-dir.sh date-dir.sh: Permission denied. Executable permission is usually necessary for any shell script to run. $ chmod u+x date-dir.sh $ date-dir.sh

11 Shell Scripts A shell script is an ordinary file containing a set of commands, which is executed in an interpretive manner. Also known as a shell program or shell procedure. The date-dir.sh shell script has a sequence of three commands. directory=`pwd` echo The date today is `date` echo The current directory is $directory

12 The Shell’s Treatment of the Command Line The shell processes the command in the following order: –Parsing – break up the command into words. –Variable evaluation – A words preceded by a $ are evaluated as variables. –Command substitution – Any command in back quotes is executed and replaced. –Redirection – redirect input and output. –Wild-card interpretation – scan the command line for wild cards and match the file list. –Path evaluation – search for the command.

13 The Other Shells The original UNIX system came with the Bourne shell. The Korn shell (ksh), the bash (bash), and the C shell are widely used in UNIX. Korn and bash maintain near-complete compatibility with the Borne shell. Because commands and integer and string handling features are built in, programs run under Korn and bash execute faster than under Bourne.


Download ppt "CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang"

Similar presentations


Ads by Google