ctec 1863 – operating systems shell scripting. ctec1863 - 2009f2 overview how shell works command...
TRANSCRIPT
![Page 1: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/1.jpg)
CTEC 1863 – Operating Systems
Shell Scripting
![Page 2: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/2.jpg)
CTEC1863 - 2009F 2
Overview
• How shell works
• Command line parameters– Shift command
• Variables – Including predefined/specials vars
• Functions
![Page 3: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/3.jpg)
CTEC1863 - 2009F 3
Shells
• sh is a command interpreter– It supports a powerful command language– Each invocation of the sh program is called a shell
![Page 4: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/4.jpg)
CTEC1863 - 2009F 4
Shells (2)
• OS’s allow invoking commands one at a time from a terminal
• Shells allow users to:– Combine commands to form new commands– Pass positional parameters to a command– Add or rename commands– Execute commands within loops– Execute commands conditionally– Send commands into the background
![Page 5: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/5.jpg)
CTEC1863 - 2009F 5
How it works:
![Page 6: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/6.jpg)
CTEC1863 - 2009F 6
How it works:
• When you type a command:– Your shell makes a copy of itself through the fork
system call (PID 230)– Your shell puts itself to sleep (via the wait system call)
until its child process (PID 230) calls exit– The child process calls exec(date) to overwrite itself
with the code for date– When the date command is finished, it calls exit()– The child process (230) dies, and your shell wakes up
again, and displays the prompt
![Page 7: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/7.jpg)
CTEC1863 - 2009F 7
Variables
• Spring into existence by being mentioned– May be assigned string values– Syntax: NAME=STRING– Thereafter "$NAME" will yield "STRING"– No spaces around the = sign!!!!!
• Double quotes around the right-hand side allow STRING to contain semicolons, spaces, tabs, and newlines
![Page 8: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/8.jpg)
CTEC1863 - 2009F 8
Examples:
$ echo NAMENAME$ NAME=Howard$ echo $NAMEHoward
$ HIS="/usr/user3"$ ls $HIS
![Page 9: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/9.jpg)
CTEC1863 - 2009F 9
Positional Parameters
• Implicitly created when a shell script runs– These variables are known as: $0, $1, ..., $9
Try this script:echo "Example of positional parameters"echo "$5"echo $1echo $2echo $3 $4$ PARAMS These parameters are passed
Also try changing “$5” to ‘$5’
![Page 10: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/10.jpg)
CTEC1863 - 2009F 10
shift
• Used when more than 10 ($0-$9) parameters– After the shift command is executed, the positional
parameters from $2 ... are renamed $1 ...
• E.g. if commandfile is invoked as:commandfile arg1 arg2 arg3 arg4 arg5 arg6
$0 $1 $2 $3 $4 $5 $6
• After a shift command is executed:commandfile arg1 arg2 arg3 arg4 arg5 arg6
$0 gone $1 $2 $3 $4 $5
![Page 11: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/11.jpg)
CTEC1863 - 2009F 11
Shift exampleTry this script:
echo "Example of shift command"echo $1shiftecho $1shiftecho $1
With:$ ./shiftit shows shift command
![Page 12: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/12.jpg)
CTEC1863 - 2009F 12
set command
• Forces values into the positional parameters• For example:
set -- abc def ghi is equivalent to:$1=abc$2=def$3=ghi
• Note:– Positional parameters are not allowed on the left hand
side of an assignment statement.
Try this script:
set -- THESE THREE VALUESecho $1 $2 $3
![Page 13: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/13.jpg)
CTEC1863 - 2009F 13
Shell Maintained Variables
• HOME: Upon login, the shell assigns a user's login directory
• IFS: Internal field separators, default is blank, tab, newline
• MAIL: Pathname of the file where your mail is deposited
• PATH: Search path used to find commands
![Page 14: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/14.jpg)
CTEC1863 - 2009F 14
Special Variables
$# $? $! $$ $-
$# records the number of arguments passed to a shell procedure
e.g. $ someproc x y z
sets $# to 3
– Tell if correct number of args passed in
![Page 15: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/15.jpg)
CTEC1863 - 2009F 15
Special Variables (2)
$? contains the exit status of the last command executed– Zero indicates successful completion– Anything from 1 to 255 indicates failure
• Often only want to execute next command if last was successful
$! contains the process number of the last process run in the background (using &)
![Page 16: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/16.jpg)
CTEC1863 - 2009F 16
Special Variables (3)
• $$ contains the process number of the currently running process.– Process numbers are unique among all existing
processes.– Often used if a shell procedure needs to create
temporary files:…temp=$HOME/temp.$$ls > $temp…rm $temp…
![Page 17: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/17.jpg)
CTEC1863 - 2009F 17
Special Variables (4)
• $- is string containing the names of all the flags currently turned on in the shell
• Also…– $* is not considered a special variable– Contains $1, $2, ... separated by spaces
![Page 18: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/18.jpg)
CTEC1863 - 2009F 18
Testing Variables
• Test command• Usage: test EXPRESSION
– test evaluates EXPRESSION to true or false– True exit with STATUS 0 – False exit with STATUS !0
• test is used for three categories of testing:– Files– Strings– Numbers
![Page 19: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/19.jpg)
CTEC1863 - 2009F 19
Functions
• Sequence of commands
• Defining:
function_name () {
statements
}
• Use:
function_name
![Page 20: CTEC 1863 – Operating Systems Shell Scripting. CTEC1863 - 2009F2 Overview How shell works Command line parameters –Shift command Variables –Including](https://reader031.vdocuments.site/reader031/viewer/2022012918/56649dc85503460f94abe343/html5/thumbnails/20.jpg)
CTEC1863 - 2009F 20
Functions (2)
• Arguments:
print_args () {
echo “Arg 1 is $1”
echo “Arg 2 is $2”
}
• Passing arguments:
print_args arg1 arg2