Advanced UNIX CIS 218 – Advanced UNIX 6 . The Bourne and Bash Shells Objectives – explain how to write Bourne and Bash Shell scripts 1 CIS 218 Advanced UNIX
Overview 1 . Making a File Executable 2 . Combining Commands 3 . Redirecting Output 4 . Executing Scripts 5 . Variables 6 . Control Flow continued 2 CIS 218 Advanced UNIX
7 . Functions 8 . Other Commands 9 . Here Documents 10 . Debugging 11 . More Information 3 CIS 218 Advanced UNIX
1 . Making a File Executable $ cat whoson date echo Users currently logged on who Wrong: $ whoson whoson: Permission denied 4 CIS 218 Advanced UNIX
Right: $ ls -lg whoson -rw-r--r-- 1 ad pubs 42 Jun 17 10 : 55 whoson $ chmod u+x whoson $ ls -lg whoson -rwxr--r-- 1 ad pubs 42 Jun 17 10 : 55 whoson $ whoson Tue Nov 7 13 : 21 : 34 ICT 2000 Users currently logged in ad consol Nov 7 08: 26 jenny tty 02 Nov 7 10:04 5 CIS 218 Advanced UNIX
Possible PATH Problem $ whoson whoson: Command not found Due to PATH shell variable (see later) Quick fixes: $ ./whoson or $ sh whoson 6 CIS 218 Advanced UNIX
2 . Combining Commands Sequencing: $ a ; b ; c same as: $ a $ b $ c 7 CIS 218 Advanced UNIX
Process Creation (&) $ a & b & c 14271 (PID for a) 14272 (PID for b) $ a & b & c & 14290 14291 14292 $ $ a | b | c & 14302 (PID for piped commands) 8 CIS 218 Advanced UNIX
Processes in Action $ cat a echo - n “aaaaaaaaaaaaaaaaaaaaa” echo - n “aaaaaaaaaaaaaaaaaaaaa” sleep 2 echo - n “aaaaaaaaaaaaaaaaaaaaa” echo - n “aaaaaaaaaaaaaaaaaaaaa” On calvin there Similarly for b and c isn't much variation unless the machine is Try the following a few times: loaded. $ a & b & c & 9 CIS 218 Advanced UNIX
3 . Redirecting Output 1 > redirect standard output (stdout) 2 > redirect standard error (stderr) $ cat a b 1 > out 2 > err Files stdout out cat a b err stderr 10 CIS 218 Advanced UNIX
>& redirect one stream into another: – 2>&1 redirect stderr into stdout $ cat a b 1 > theLot 2>&1 stdout theLot cat a b stderr 11 CIS 218 Advanced UNIX
4 . Executing Scripts Make sure that a script is executed by the Bourne Shell: (no need for chmod ) $ sh whoson or: $ cat boss #!/bin/sh echo Definitely Bourne Shell Script continued 12 CIS 218 Advanced UNIX
On Linux machines (e.g. calvin ), the Bourne shell has been replaced by Bash – sh means the Bash shell 13 CIS 218 Advanced UNIX
5 . Variables 5 . 1 . User-defined Variables 5 . 2 . Environment Variables 5 . 3 . Readonly Shell Variables 14 CIS 218 Advanced UNIX
5 . 1 . User-defined Variables No spaces $ person=alex around the = $ echo person person $ echo $ person alex $var returns the value stored in var – called substitution 15 CIS 218 Advanced UNIX
5 . 1 . 1 . Switch off Substitution Swich off substitution with ' var ' or \var $ echo '$person' $person $ echo \$person $person 16 CIS 218 Advanced UNIX
5 . 1 . 2 . Switch off Special Chars (") "" switches off the special meaning of characters used in filename generation (e.g. *, ?) $ ls // directory contents ad.report ad.summary $ memo=ad* * means only * $ echo "$memo" ad* $ echo $memo * means any number ad.report ad.summary of characters 17 CIS 218 Advanced UNIX
5 . 1 . 3 . Exporting Variables Normally a variable is local to the running script (the process). It is sometimes useful if running scripts (processes) can access another script’s variables. we want to calls use cheese e.g. extest subtest in subtest cheese=english 18 CIS 218 Advanced UNIX
No Exporting: extest 1 & subtest Using " is $ cat extest 1 a good habit, cheese=english see later echo "extest 1 1 : $cheese" subtest echo "extest 1 2 : $cheese" $cat subtest echo "subtest 1 : $cheese" cheese=swiss echo "subtest 2 : $cheese" continued 19 CIS 218 Advanced UNIX
subtest does not $ extest 1 see extest 1 's extest 1 1 : english cheese value subtest 1 : subtest 2 : swiss extest 1 2 : english extest 1 is not affected by subtest 's setting of cheese 20 CIS 218 Advanced UNIX
Exporting: extest 2 & subtest $ cat extest 2 export cheese cheese=english echo “extest 2 1 : $cheese” subtest echo “extest 2 2 : $cheese” $ extest 2 extest 2 1 : english cheese value passed in subtest 1 : english subtest 2 : swiss change not exported extest 2 2 : english from subtest 21 CIS 218 Advanced UNIX
5 . 1 . 4 . Reading read inputs everything up to the newline $ cat readln echo - n “Type: ” read ln echo “You entered: $ln” $ readln Type: The Wizard of Oz You entered: The Wizard of Oz 22 CIS 218 Advanced UNIX
No Quotes $ cat readlnnq echo - n “Type: ” read ln echo You entered: $ln directory $ ls ad.report summary 1 contents $ readlnnq Type: * You entered: ad.report summary 1 23 CIS 218 Advanced UNIX
5 . 1 . 5 . Executing Commands A very simple shell $ cat proc_cmd echo - n “Enter a command: ” read command $command echo Thanks $ proc_cmd Enter a command: echo Display this Display this Thanks 24 CIS 218 Advanced UNIX
5 . 1 . 6 . Splitting Input Text is split based on white $ cat split 3 space. echo - n “Enter something: ” read word 1 word 2 word 3 echo “Word 1 is: $word 1 ” echo “Word 2 is: $word 2 ” echo “Word 3 is: $word 3” $ split 3 Enter something: this is something Word 1 is: this Word 2 is: is Word 3 is: something 25 CIS 218 Advanced UNIX
$ split 3 Enter something: this is something else, x Word 1 is: this Word 2 is: is Word 3 is: something else, x The last variable gets everything that is left in the input. 26 CIS 218 Advanced UNIX
5 . 1 . 7 . Command Subsitution Must use ‘ $ cat mydir this_dir=‘pwd‘ echo “Using the $this_dir directory.” this_date=$(date) A Bash echo "Today's date: $this_date" addition $ mydir Using /home/ad/teach/adv-unix/bourne directory Today's date: Tue Nov 7 13 : 52 : 46 ICT 2000 $ 27 CIS 218 Advanced UNIX
5 . 2 . Environment Variables Most environment variables get their values from the shell at login. The values of some of the variables can be set by editing the .profile file in your home directory – Bash uses .bash_profile and .bashrc 28 CIS 218 Advanced UNIX
5 . 2 . 1 . Examples HOME pathname of your home directory $ pwd /home/ad/planning $ echo $HOME /home/ad cd uses HOME $ cd to return to your $ pwd home directory /home/ad continued 29 CIS 218 Advanced UNIX
PATH – directories where executable can be found – represented as a string of pathnames separated by ‘:’ s $ echo $PATH Extend the /usr/local/bin:/usr/bin:/bin: default PATH $ PATH=SPATH":/home/ad/bin:." $ echo $PATH /usr/local/bin:/usr/bin:/bin:/home/ad/bin:. 30 CIS 218 Advanced UNIX
Note for SysAdmins If you are the system administrator (superuser, root) for your machine, do not extend your path with "." – it opens you to potential attack by hackers – e.g. 'fake' UNIX utilities placed in the current directory 31 CIS 218 Advanced UNIX
5 . 2 . 2 . Typical .profile $ cat .profile TERM=vt 100 PATH=$PATH":/home/ad/bin:." PS 1 =“ad: “ CDPATH=:$HOME export TERM PATH PS 1 CDPATH stty kill ^u export needed $ . .profile in the Bourne shell 32 CIS 218 Advanced UNIX
5 . 2 . 3 . Typical .bash_profile On calvin , .bash_profile simply invokes your .bashrc (if it exists): umask 002 if [ -f ~/.bashrc -a PS 1 ="\$ " ]; then . ~/.bashrc fi These shell commands will be explained later continued 33 CIS 218 Advanced UNIX
Typical .bashrc PS 1 ="\u@\h$ " # PS 1 ="\w[\#]$ " No export needed PATH=$PATH":." alias ls='/bin/ls -F' alias dir='ls -ba' alias cls="clear" These features : will be explained : later. psgrep() { ps aux | grep $1 | grep -v grep } 34 CIS 218 Advanced UNIX
5 . 2 . 4 . set The current settings for the environment variables can be listed with set : $ set | more BASH=/bin/bash : PATH=/usr/local/bin:/usr/bin:/bin:. : PS 1 ='\u@\h$ ' : 35 CIS 218 Advanced UNIX
5 . 3 . Readonly Shell Variables These are environment variables that cannot have their values changed. Most of them relate to the arguments supplied to a script when it is called. 36 CIS 218 Advanced UNIX
5 . 3 . 1 . Script Name ( $0 ) $ cat abc echo The name of this script is $0 $ abc The name of this script is abc 37 CIS 218 Advanced UNIX
5 . 3 . 2 . Script Arguments ( $1, $2,..., $9 ) $ cat display_ 5 args echo The first five command line echo arguments are $1 $2 $3 $4 $5 $ display_ 5 args jenny alex helen The first five command line arguments are jenny alex helen If the variable has no value, then nothing is printed. 38 CIS 218 Advanced UNIX
5 . 3 . 3 . All Arguments ( $* ) $ cat display_all echo $* $ display_all a b c de fg hi jk mno pqr stu w x y z a b c de fg hi jk mno pqr stu w x y z $@ is like $* but puts “...” around each printed argument 39 CIS 218 Advanced UNIX
5 . 3 . 4 . Number of Arguments ( $# ) $ cat num_args echo “This script has $# arguments.” num_args helen alex jenny This script has 3 arguments 40 CIS 218 Advanced UNIX
Recommend
More recommend