Download presentation
Presentation is loading. Please wait.
Published byEstella Hampton Modified over 9 years ago
1
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming
2
CIT 140: Introduction to ITSlide #2 Topics 1.Shell Programming 2.Shell Variables 3.Command Substitution 4.Command Line Arguments 5.Reading from the User 6.Control Flow 7.if/elif/else 8.for loops 9.while loops 10.case statements
3
CIT 140: Introduction to ITSlide #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.
4
CIT 140: Introduction to ITSlide #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
5
CIT 140: Introduction to ITSlide #5 Shell Variables
6
CIT 140: Introduction to ITSlide #6 Read-only Shell Variables
7
CIT 140: Introduction to ITSlide #7 Displaying Shell Variables > printenv PWD=/export/home0/waldenj TZ=US/Michigan PAGER=less HOSTNAME=zappa LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/ucblib MANPATH=/usr/local/man:/usr/share/man:/usr/openwin/share/man VISUAL=vim USER=waldenj ENV_SET=1 CVS_RSH=ssh EDITOR=vim LOGNAME=waldenj SHLVL=1 TEXMF=/usr/local/share/texmf SHELL=/bin/bash HOSTTYPE=sparc CDPATH=::/export/home0/waldenj HOME=/export/home0/waldenj TERM=xterm PERL_RL=Perl
8
CIT 140: Introduction to ITSlide #8 Setting Shell variables Assign a value to a variable: varname=value Examples monty=python spam=“spam, spam, spam, spam” PATH=/bin:/usr/ucb:/usr/bin Notes No spaces on either side of equal sign.
9
CIT 140: Introduction to ITSlide #9 Using Shell Variables > spam=eggs > echo $spam eggs > echo spam spam > echo \$spam $spam > spam=spam and eggs bash: and: command not found > echo $spam eggs > spam="spam and eggs" > echo $spam spam and eggs > spam=c* > echo $spam cit140 csc382 csc501
10
CIT 140: Introduction to ITSlide #10 Special echo Characters
11
CIT 140: Introduction to ITSlide #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`
12
CIT 140: Introduction to ITSlide #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
13
CIT 140: Introduction to ITSlide #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.bashrc PATH=/bin:/usr/bin:/usr/local/bin:/usr/ucb MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man export PATH MANPATH
14
CIT 140: Introduction to ITSlide #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
15
CIT 140: Introduction to ITSlide #15 Reading from Standard Input read variable-list Purpose: Read one line from standard input and assign words in the line to variables in ‘name-list’.
16
CIT 140: Introduction to ITSlide #16 Shell Script Arguments $ cat cmdargs_demo #!/bin/sh echo “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 i The 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 6 The 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.
17
CIT 140: Introduction to ITSlide #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
18
CIT 140: Introduction to ITSlide #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 author 2.Date written 3.Date last modified 4.Purpose of the script
19
CIT 140: Introduction to ITSlide #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
20
CIT 140: Introduction to ITSlide #20 Program Control Flow Commands
21
CIT 140: Introduction to ITSlide #21 If Statement
22
CIT 140: Introduction to ITSlide #22 Operators for the test Command
23
CIT 140: Introduction to ITSlide #23 Program Control Flow Commands
24
CIT 140: Introduction to ITSlide #24 Example: fileinfo.sh #!/bin/sh if [ $# -ne 1 ]; then echo "Usage: fileinfo filename" fi file=$1 if [ -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 echo else echo "File $file is not a regular file." fi >./fileinfo.sh fileinfo.sh Owner: waldenj Group: students Permissions: Read Write Execute
25
CIT 140: Introduction to ITSlide #25 Program Control Flow Commands
26
CIT 140: Introduction to ITSlide #26 Shell Example: Filetype #!/bin/sh if [ $# -ne 1 ]; then echo "Usage: filetype filename" fi filename=$1 if [ -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
27
CIT 140: Introduction to ITSlide #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.
28
CIT 140: Introduction to ITSlide #28 The for Statement
29
CIT 140: Introduction to ITSlide #29 Shell Example: for > ls *.man bash.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.bz2 bash.man.bz2 cat.man.bz2 tcsh.man.bz2
30
CIT 140: Introduction to ITSlide #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" fi done >./userinfo.sh waldenj newellg spam User waldenj has home directory /export/home0/waldenj User newellg has home directory /export/home1/newellg No such user spam
31
CIT 140: Introduction to ITSlide #31 The while statement
32
CIT 140: Introduction to ITSlide #32 Shell Script Example: while #!/bin/sh secret=agent007 guess=none echo "Guess the secret word." while [ $secret != $guess ] do echo -n "What's your guess? " read guess done echo "You guessed the secret word!"
33
CIT 140: Introduction to ITSlide #33 Shell Script Example: while >./guessgame.sh Guess the secret word. What's your guess? spam What's your guess? eggs What's your guess? agent007 You guessed the secret word!
34
CIT 140: Introduction to ITSlide #34 The case Statement
35
CIT 140: Introduction to ITSlide #35 The case Statement
36
CIT 140: Introduction to ITSlide #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" ;; esac done
37
CIT 140: Introduction to ITSlide #37 LittleSh Example >./littlesh.sh littleshell> ls I didn't understand that command littleshell> dir total 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.bz2 littleshell> users longa pts/12 Nov 8 14:36 waldenj 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.