More about Shell q Shells sh csh ksh
More about Shell q Shells (sh, csh, ksh) are m Command interpreters • Process the commands you enter m High-level programming languages • Process groups of commands stored in a file called shell scripts. • Like other languages, shells have – Variables – Control flow commands More about Shells 1
Create a shell script q Creating a simple shell script m A shell script is a file that contains commands that the shell can execute. • Any commands you enter in response to a shell prompt. – A utility – A compiled program – Another shell script • Control flow commands q Run a shell script m Enter the script filename on the command line m The shell interprets and execute the commands one after another q Why shell script? m Simply and quickly initiate a complex series of tasks or a repetitive procedure. More about Shells 2
Shell programming q Make the file executable m When you create a shell script using a editor, does it have execute permission typically? • Example [ruihong@dafinn ~/cs 3451]$. /test: Permission denied. [ruihong@dafinn ~/cs 3451]$ ls -l test -rw------- 1 ruihong csdept 22 Jan 28 09: 33 test [ruihong@dafinn ~/cs 3451]$ chmod +x test [ruihong@dafinn ~/cs 3451]$. /test this is a test More about Shells 3
Invoking a Shell script q Give the shell a command on the command line m The shell forks a process • Which creates a duplicate of the shell process ( subshell) m The new process attempt to exec the command • If the command is a executable program – Exec succeeds – System overlays the newly created subshell with the executables programs • The the command is a shell script – Exec failed – The command is assumed to be a shell script – The subshell runs the commands in the shell. More about Shells 4
Invoking a Shell script q Shell itself is program, m It can be run as a command in a shell m It accepts arguments q To run a shell script m Which does not have executable permission Ex: $sh filename m Run the script with different shell other than your interactive shell Ex: $ksh filename More about Shells 5
Invoking a Shell script q Put special characters on the first line of a shell script m m To tell OS checks what kind of file it is before attempting to exec it To tell which utility to use (sh, csh, tcsh, …) q Special sequence m The firsts two character of a script are #! m Then followed by the absolute pathname of the program that should execute the script Ex: sh-2. 05 b$ more /etc/init. d/sshd #!/bin/bash # # Init file for Open. SSH server daemon # More about Shells 6
Make a comment # q Comments make shell scripts easier to read and maintain q Pound sign (#) start a comment line until the end of that line, except m #! In the first line. m Or inside quotes More about Shells 7
Startup files q Bourne shell m System wide /etc/profile m. profile in your home dir m Example: /etc/profile in undergrad lab. q Make the change take effect m Log out and log back in m Running. profile with. (DOT) built-in Ex: $. . profile • . Command runs the script as the part of the current process • Changes will affect the login shell. • If without the first. , the new variable would be in effect only in the subshell running the script. More about Shells 8
Parameters and Variables q A shell parameter is associated with a value that is accessible to the user. m Shell variables m Positional parameters m Special parameters • Names consist of letters, digits and underscore – By convention, environment variables uses uppercase • User created variables ( create and assign value) • Keyword shell variables – Has special meaning to the shell – Being created and initialized by the startup file • Allow you to access command line arguments • Such as – The name of last command – The status of most recently executed command – The number of command-line arguments More about Shells 9
Positional Parameters q The command name and arguments are the positional parameters. m Because you can reference them by their position on the command line m $0 : Name of the calling program m $1 - $9 : Command-line Arguments • • The first argument is represented by $1 The second argument is represented by $2 And so on up to $9 The rest of arguments has to be shifted to be able to use $1 - $9 parameters. More about Shells 10
Positional Parameters q Example: [ruihong@dafinn ~/cs 3451]$ more display_5 args echo you are running script $0 with parameter $1 $2 $3 $4 $5 [ruihong@dafinn ~/cs 3451]$. /display_5 args 1 2 3 4 5 you are running script. /display_5 args with parameter 1 2 3 4 5 More about Shells 11
Positional Parameters q $1 -$9 allows you to access 10 arguments m How to access others? q Promote command-line arguments: shift m Built-in command shift promotes each of the command-line arguments. • • The first argument ( which was $1) is discarded The second argument ( which was $2) becomes $1 The third becomes the second And so on m Makes additional arguments available m Repeatedly using shift is a convenient way to loop over all the command-line arguments More about Shells 12
Positional Parameters q Example: [ruihong@dafinn ~/cs 3451]$ more. /demo_shift echo $1 $2 $3 shift echo $1 $2 shift echo $1 [ruihong@dafinn ~/cs 3451]$. /demo_shift 1 2 3 123 23 3 More about Shells 13
[ruihong@dafinn ~/cs 3451]$ more demo_shift echo $1 $2 $3 shift echo $1 $2 shift echo $1 shift echo $? [ruihong@dafinn ~/cs 3451]$. /demo_shift 1 2 3 2 3 3 0 1 1 More about Shells 14
Positional Parameters q Use quote for variable reference m Example: • what’s the difference if $1 is null $ display_4 args $1 a b c d $ display_4 args “$1” a b c d • What will happen if a is null if [ $a = 3 ]; then echo a is 3 fi q Initialize command line arguments : set (sh/ksh only) m Set the positional parameters starting from $1, … More about Shells 15
Special Parameters q Useful values m Command-line arguments m Execution of shell commands m Can not change the value directly, like positional parameters q Value of Command-line arguments: $* and $@ m $* and $@ represent all the command_line arguments ( not just the first nine) m “$*” : treat the entire list of arguments as a single argument m “$@” : produce a list of separate arguments. More about Shells 16
sh-2. 05 b$ more for_test echo "using $@ " for arg in "$@" do echo "$arg" done echo "using $* " for arg in "$*" do echo "$arg" Done sh-2. 05 b$. /for_test 1 2 3 using $@ 1 2 3 using $* 1 2 3 More about Shells 17
Special Parameters q The number of arguments: $# m Return a decimal number m Use the test to perform logical test on this number sh-2. 05 b$. /num_args this script is called with 0 arguments. sh-2. 05 b$. /num_args 1 this script is called with 1 arguments. sh-2. 05 b$. /num_args alice in wonder land this script is called with 4 arguments. sh-2. 05 b$ more num_args echo this script is called with $# arguments. More about Shells 18
Special Parameters q PID number: $$ m Ex: sh-2. 05 b$ echo $$ 11896 sh-2. 05 b$ echo "today is `date`" >> $$. memo sh-2. 05 b$ more $$. memo today is Sun Jan 30 00: 18: 09 EST 2005 q The PID number of last process that you ran in the background: $! m Ex: sh-2. 05 b$ sleep 1000 & [1] 11962 sh-2. 05 b$ echo $! 11962 More about Shells 19
Special Parameters q Exit status: $? m When a process stops executing for any reason, it returns an exit status to its parent process. m By convention, • Nonzero represents a false value that the command failed. • A zero value is true and means that the command was successful m You can specify the exit status that a shell script returns by using the exit built-in followed by a number • Otherwise, the exit status of the script is the exit status of the last command the script ran. More about Shells 20
sh-2. 05 b$ ls a ls: a: No such file or directory sh-2. 05 b$ echo $? 1 sh-2. 05 b$ echo tttt sh-2. 05 b$ echo $? 0 sh-2. 05 b$ more exit_status echo this program will have the exit code of 8. exit 8 sh-2. 05 b$. /exit_status this program will have the exit code of 8. sh-2. 05 b$ echo $? 8 sh-2. 05 b$ echo $? 0 More about Shells 21
Summary q A shell is both a command interpreter and a programming language q Job control m Control-z/fg/bg/& q Variables m Local and environment variables m Declare and initialize a variable ( no type) m Export unset q Command line expansion m Parameter expansion/variable expansion/command/substitution/pathname expansion m Quote ( ‘ ‘ “ “ ) • “ “ all but parameter, variable expansion and • ‘ ‘ suppress all types of expansion • escaping the following special character More about Shells 22
Summary q Shell parameters m HOME m PATH m PS 1 m SHELL m $0 m $n m $* m $@ m $# m $$ m $! m $? More about Shells 23
Summary q Special Characters m NEWLINE m; m () m& m| m> m >> m< m << More about Shells 24
Summary m m m m * ? ‘ “ ` ` [] $. # && || ! More about Shells 25
- Slides: 25