cit 140: introduction to itslide #1 cit 140: introduction to it shell programming
Post on 14-Jan-2016
225 Views
Preview:
TRANSCRIPT
CIT 140: Introduction to IT Slide #1
CIT 140: Introduction to IT
Shell Programming
CIT 140: Introduction to IT Slide #2
Topics
1. Shell Programming2. Shell Variables3. Command Substitution4. Command Line Arguments5. Reading from the User6. Control Flow7. if/elif/else8. for loops9. while loops10. case statements
CIT 140: Introduction to IT Slide #3
Introduction
Shell script: a shell program, which consists of shell commands to be executed by a shell and is stored in ordinary UNIX file.
Shell variable: read/write storage place for users and programmers to use as a scratch pad for completing a task.
Program control flow commands (or statements): allow non sequential execution of commands in a shell script and repeated execution of a block of commands.
CIT 140: Introduction to IT Slide #4
Running a Bourne Shell Script
1. Run /bin/sh command with script parameter$ /bin/sh script_file
2. Make the script file runnable directly
$ chmod u+x script_file
$ vim script_file
First line: #!/bin/sh
$ ./script_file
CIT 140: Introduction to IT Slide #5
Shell Variables
CIT 140: Introduction to IT Slide #6
Read-only Shell Variables
CIT 140: Introduction to IT Slide #7
Displaying Shell Variables> printenvPWD=/export/home0/waldenjTZ=US/MichiganPAGER=lessHOSTNAME=zappaLD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/ucblibMANPATH=/usr/local/man:/usr/share/man:/usr/openwin/share/manVISUAL=vimUSER=waldenjENV_SET=1CVS_RSH=sshEDITOR=vimLOGNAME=waldenjSHLVL=1TEXMF=/usr/local/share/texmfSHELL=/bin/bashHOSTTYPE=sparcCDPATH=::/export/home0/waldenjHOME=/export/home0/waldenjTERM=xtermPERL_RL=Perl
CIT 140: Introduction to IT Slide #8
Setting Shell variables
Assign a value to a variable:varname=value
Examplesmonty=python
spam=“spam, spam, spam, spam”
PATH=/bin:/usr/ucb:/usr/bin
Notes
No spaces on either side of equal sign.
CIT 140: Introduction to IT Slide #9
Using Shell Variables> spam=eggs> echo $spameggs> echo spamspam> echo \$spam$spam> spam=spam and eggsbash: and: command not found> echo $spameggs> spam="spam and eggs"> echo $spamspam and eggs> spam=c*> echo $spamcit140 csc382 csc501
CIT 140: Introduction to IT Slide #10
Special echo Characters
CIT 140: Introduction to IT Slide #11
Command Substitution
Command Substitution: When a command is enclosed in back quotes, the shell executes the command and substitutes the command (including back quotes) with the output of the command.
`command`Purpose:Substitute its output for `command`
CIT 140: Introduction to IT Slide #12
Using Command Substitution> dir=`pwd`> echo $dir/export/home0/waldenj> echo "The current directory is $dir"The current directory is /export/home0/waldenj> echo "The current date and time is `date`"The current date and time is Sun Nov 20 15:47:16
EST 2005
CIT 140: Introduction to IT Slide #13
Exporting Environment
export [name-list]
Purpose: Export the names and copies of the current values in the ‘name-list’ to every command executed from this point on.
Example:
> grep PATH .bashrcPATH=/bin:/usr/bin:/usr/local/bin:/usr/ucbMANPATH=/usr/local/man:/usr/man:/usr/X11R6/manexport PATH MANPATH
CIT 140: Introduction to IT Slide #14
Resetting Variables
unset [name-list]Purpose Reset or remove the variable or function corresponding to the names in ‘name-list’, where ‘name-list’ is a list of names separated by spaces.
> food1=spam
> food2=eggs
> echo "I like $food1 and $food2"
I like spam and eggs
> unset food1 food2
> echo "I like $food1 and $food2"
I like and
CIT 140: Introduction to IT Slide #15
Reading from Standard Input
read variable-listPurpose: Read one line from standard input and assign words in the line to variables in ‘name-list’.
CIT 140: Introduction to IT Slide #16
Shell Script Arguments $ cat cmdargs_demo#!/bin/shecho “The command name is: $0.”echo “The number of command line arguments passed as parameters are $#.”echo “The value of the command line arguments are: $1 $2 $3 $4 $5 $6 $7 $8
$9.”echo “Another way to display values of all of the arguments: $@.”echo “Yet another way is: $*.”exit 0$ cmdargs_demo a b c d e f g h iThe command name is: cmdargs_demo.The number of command line arguments passed as parameters are 9.The value of the command line arguments are: a b c d e f g h i.Another way to display values of all of the arguments: a b c d e f g h i.Yet another way is: a b c d e f g h i.$ cmdargs_demo One Two 3 Four 5 6The command name is: cmdargs_demo.The number of command line arguments passed as parameters are 6.The value of the command line arguments are: One Two 3 Four 5 6 .Another way to display values of all of the arguments: One Two 3 Four 5 6.Yet another way is: One Two 3 Four 5 6.
CIT 140: Introduction to IT Slide #17
Command Output as Arguments
> date
Sun Nov 20 16:23:08 EST 2005
> set `date`
> echo $@
Sun Nov 20 16:23:13 EST 2005
> echo $2 $3, $6
Nov 20, 2005
CIT 140: Introduction to IT Slide #18
Comments
Put comments in your programs to describe the purpose of a particular set of commands.
Use comments to create a program header for your scripts, including:
1. Name of the author2. Date written3. Date last modified4. Purpose of the script
CIT 140: Introduction to IT Slide #19
Control Flow Commands
Used to determine the sequence in which statements in a shell script execute.
Three types of control flow statements:1. Two-way branching
2. Multiway branching
3. Repetitive execution of one or more commands
CIT 140: Introduction to IT Slide #20
Program Control Flow Commands
CIT 140: Introduction to IT Slide #21
If Statement
CIT 140: Introduction to IT Slide #22
Operators for the test Command
CIT 140: Introduction to IT Slide #23
Program Control Flow Commands
CIT 140: Introduction to IT Slide #24
Example: fileinfo.sh#!/bin/shif [ $# -ne 1 ]; then echo "Usage: fileinfo filename"fifile=$1if [ -f $file ]; then set -- `ls -lg $file` owner=$3 group=$4 echo "Owner: $3" echo "Group: $4" echo -n "Permissions: " if [ -r $file ]; then echo -n "Read " fi if [ -w $file ]; then echo -n "Write " fi if [ -x $file ]; then echo -n "Execute " fi echoelse echo "File $file is not a regular file."fi
> ./fileinfo.sh fileinfo.shOwner: waldenjGroup: studentsPermissions: Read Write Execute
CIT 140: Introduction to IT Slide #25
Program Control Flow Commands
CIT 140: Introduction to IT Slide #26
Shell Example: Filetype#!/bin/sh
if [ $# -ne 1 ]; then echo "Usage: filetype filename"fifilename=$1if [ -L $filename ]; then echo "File $filename is a symbolic link."elif [ -f $filename ]; then echo "File $filename is a regular file."elif [ -d $filename ]; then echo "File $filename is a directory."else echo "I don't know what file $filename is."fi
CIT 140: Introduction to IT Slide #27
Shell Example: Filetype> ./filetype.sh filetype.sh
File filetype.sh is a regular file.
> ./filetype.sh /bin
File /bin is a symbolic link.
> ./filetype.sh /
File / is a directory.
CIT 140: Introduction to IT Slide #28
The for Statement
CIT 140: Introduction to IT Slide #29
Shell Example: for> ls *.manbash.man cat.man tcsh.man> for file in *.man> do> bzip2 -v $file> done bash.man: 4.821:1, 1.659 bits/byte, 79.26%
saved, 267350 in, 55456 out. cat.man: 2.684:1, 2.980 bits/byte, 62.75%
saved, 5366 in, 1999 out. tcsh.man: 4.259:1, 1.878 bits/byte, 76.52%
saved, 239534 in, 56236 out.> ls *.man.bz2bash.man.bz2 cat.man.bz2 tcsh.man.bz2
CIT 140: Introduction to IT Slide #30
Shell Script Example: for#!/bin/sh
for user in $@do grep "^"$user /etc/passwd >/dev/null 2>&1 if [ $? -eq 0 ]; then homedir=`grep "^"$user /etc/passwd | cut -d: -f6` echo "User $user has home directory $homedir" else echo "No such user $user" fidone
> ./userinfo.sh waldenj newellg spamUser waldenj has home directory /export/home0/waldenjUser newellg has home directory /export/home1/newellgNo such user spam
CIT 140: Introduction to IT Slide #31
The while statement
CIT 140: Introduction to IT Slide #32
Shell Script Example: while#!/bin/sh
secret=agent007guess=noneecho "Guess the secret word."while [ $secret != $guess ]do echo -n "What's your guess? " read guessdone
echo "You guessed the secret word!"
CIT 140: Introduction to IT Slide #33
Shell Script Example: while> ./guessgame.shGuess the secret word.What's your guess? spamWhat's your guess? eggsWhat's your guess? agent007You guessed the secret word!
CIT 140: Introduction to IT Slide #34
The case Statement
CIT 140: Introduction to IT Slide #35
The case Statement
CIT 140: Introduction to IT Slide #36
Case Example: LittleSh#!/bin/sh
while [ 1 ]do echo -n "littleshell> " read command case $command in 'dir' ) ls -lg ;; 'users' ) who ;; 'quit' ) exit ;; * ) echo "I didn't understand that
command" ;; esacdone
CIT 140: Introduction to IT Slide #37
LittleSh Example> ./littlesh.shlittleshell> lsI didn't understand that commandlittleshell> dirtotal 113-rw-r--r-- 1 waldenj students 55456 Nov 20 17:30
bash.man.bz2-rw-r--r-- 1 waldenj students 1999 Nov 20 17:30
cat.man.bz2-rwxr-xr-x 1 waldenj students 247 Nov 20 17:31
littlesh.sh-rw-r--r-- 1 waldenj students 56236 Nov 20 17:30
tcsh.man.bz2littleshell> userslonga pts/12 Nov 8 14:36waldenj pts/3 Nov 17 12:52 newellg pts/4 Nov 14 11:21 waldenj pts/2 Nov 20 15:19 partons pts/16 Nov 20 16:50
top related