Assignment clarifications
How many errors to print? – at most 1 per
token.
Interpretation of white space in { } – treat as
a valid extension, involving white space characters.
Assignment FAQs have been updated.
Assignment clarifications How many errors to print? at most 1 per - - PowerPoint PPT Presentation
Assignment clarifications How many errors to print? at most 1 per token. Interpretation of white space in { } treat as a valid extension, involving white space characters. Assignment FAQs have been updated. Shells and Shell
How many errors to print? – at most 1 per
Interpretation of white space in { } – treat as
Assignment FAQs have been updated.
A shell is a command line interpreter that is
A “program launcher” of sorts. The shell:
analyzes each command determines what actions are to be performed performs the actions
Example:
sh – Bourne shell
Most common, other shells are a superset Good for programming
csh or tcsh – default for command line on CDF
C-like syntax Best for interactive use. Not good for programming.
bash – default on Linux (Bourne again shell)
Based on sh, with some csh features.
korn – written by David Korn
Based on sh – Some claim best for programming. Commercial product.
When a shell is invoked, it does the following:
used to set shell options, set up environment
sh – executes .profile if it’s there. ksh – executes .profile if in interactive mode.
csh – executes .cshrc if it exists. If a login
bash – executes .bashrc, if a login shell,
Most commands you run are other compiled
Found in /bin Example: ls – shell locates ls binary in /bin
Some are not compiled programs, but built
cd, echo
Input-output redirection
Pipelining commands
send the output from one command to the input of
A job is a program whose execution has been
At any moment, a job can be running or suspended. Foreground job:
a program which has control of the terminal
Background job:
runs concurrently with the parent shell and does not take
control of the keyboard.
Start a job in the background by appending & Commands: ^Z, jobs, fg, bg, kill
* stands in for 0 or more characters ? stands in for exactly one character [1-6] stands in for one of 1, 2, 3, 4, 5, 6 [^oa] stands in for any char except o or a ~/ stands in for your home directory ~reid stands in for reid’s home directory
Enter series of simple commands or pipelines separated
by semicolons (;)
Commands will be executed in sequence Each process the shell executes has return value – shell
checks for it
conditional execution with &&,|| && executes next command if return value is 0 (success) || executes next command if return value is nonzero
(failure)
A bunch commands stored inside a text file Must add execute permission with chmod provides all the programming essentials: variables,
powerful way to automate tasks
First line of script should always be #! Pathname Pathname will be used to interpret script If #! Not there, sh (Bourne shell) is used
Should provide explicit return value by using exit
A copy of the current shell (child) Created when:
grouped commands with ; are executed. Parent
Script is executed. Parent sleeps until script is
A background job is executed. Parent runs
In 1st two cases, parent doesn’t wait if jobs
Environment space copied from parent Local variables are clean:
file starts with #!/bin/sh
absolute path to the shell program not the same on every machine.
can also write programs interactively by
Tip: this is a good way to test your shell programs
In a file:
At the command line:
eddie% sh $ echo “Hello World!” Hello World! $ exit eddie% penguin% sh bash$ echo “Hello World!” Hello World! bash$ exit penguin%
You can run any program in a shell by calling it as
When you run a program like grep or ls in a shell
There are also some built-in commands where no
echo set read exit test shift wait
“man sh” to
see all builtins.
are not.
sh-2.05a$ pet1=fuzzoo sh-2.05a$ pet2=moomoo sh-2.05a$ echo $pet1 $pet2 fuzzoo moomoo sh-2.05a$ export pet1 sh-2.05a$ sh sh-2.05a$ echo $pet1 $pet2 fuzzoo sh-2.05a$ exit sh-2.05a$ echo $pet1 $pet2 fuzzoo moomoo
$HOME – full path of your home directory $PATH – list of directories to search for files $MAIL – full path of mailbox $USER – your user name $SHELL – full path of your login shell
The initial values of the environment variables
$$ – process id of the current shell $! – process id of last background command $0 – name of shell script $1...$9 – commandline arguments $* – list of all commandline args $? – return value of the previously executed
$# – number of commandline params.
$name – replaced by value of name ${name} – replaced by value of name. Useful
positional parameters: variables that are
Command line arguments are placed in
#!/bin/sh echo arg1: $1 echo arg2: $2 echo name: $0 echo all: $* giant $ giant fee fie fo fum arg1: fee arg2: fie name: giant all: fee fie fo fum
sh-2.05a$ cat > shelldemo.sh #! /bin/sh echo $HOME echo $PATH echo commandline=$* echo 1st arg=$1 echo $? gcc hello.cpp echo $? gcc noexist.cpp echo $? sh-2.05a$ chmod ugo+x shelldemo.sh sh-2.05a$ shelldemo.sh arg1 arg2 arg3 /u/kenxu /u/kenxu/bin/Linux:/u/kenxu/bin:/local/bin/X11:/usr/X11R6/bin:/local/bin:/usr/ucb:/bin:/usr/bin:.:/u/kenxu commandline=arg1 arg2 arg3 1st arg=arg1 gcc: noexist.cpp: No such file or directory gcc: no input files 1
local variables – spaces matter
name=value – assignment variables can have a single value or list of values.
Single value:
List of values (separated by spaces):
Single and double quotes are
is often on the same key as ~.
Double quotes inhibit wildcard replacement
Single quotes inhibit wildcard replacement,
Back quotes cause command substitution. Practice and pay attention.
” – double quotes ’ – single quote ` - back quote
What do the following statements produce if
Command substitution causes another
Which is better? What is the difference?
Easy way to provide input to commands
sh-2.05a$ cat << ENDOFTEXT > type some text and > have cat repleat > it! > ENDOFTEXT type some text and have cat repleat it! sh-2.05a$
Bourne Shell doesn’t support arithmetic natively. Use expr command (arithmetic & string)
sh-2.05a$ echo `expr length "cat"` 3 sh-2.05a$ echo `expr substr "donkey" 4 3` key sh-2.05a$ echo `expr index "donkey" "key"` 4 sh-2.05a$ echo `expr match "smalltalk" '.*lk'` 9 sh-2.05a$ echo `expr match "transputer" '.*lk'` sh-2.05a$ echo `expr \( 4 \> 5 \)` sh-2.05a$ echo `expr \( 4 \> 5 \) \| \( 6 \< 7 \)` 1
test is an utility that gives 0 exit status if boolean
Some versions of Bourne shell has it built in syntax: test expression or [ expression ]
sh-2.05a$ if test 2 -eq 3 > then > echo equal > else > echo not equal > fi not equal sh-2.05a$ if [ 2 -eq 2 ]; then echo equal; else echo not equal; fi equal sh-2.05a$
Common
And, or.
≠ , > , < , ≤
True if int1 equals int2 int1 -eq int2 True if str1 not equal to str2 str1 != str2 True if str1 equals str2 str1 = str2 True if empty string
Exists as an executable file.
Exists as a writable file.
Exists as a readable file
Exists as a regular file.
Exists as a directory
test arguments
The general syntax for if is:
if command1 then command2 command3 ...etc elif command4 command5 ...etc else command6 fi
Looks at exit status to make decision
Does nothing Consider an “unload” program:
How to convert this to a backup program?
Take a string and match it to a pattern:
sh-2.05a$ choice=2 sh-2.05a$ case $choice in > "1") > echo choice was 1 > ;; > "2"|"3") > echo choice was 2 or 3 > ;; > *) > echo unknown choice > ;; > esac choice was 2 or 3
The for loop takes a list of arguments and executes
for color in red green blue pink do echo The sky is $color done
The while loop tests on a boolean condition
sh-2.05a$ count=1 sh-2.05a$ while [ $count -lt 10 ] > do > echo $count > count=`expr $count + 1` > done 1 2 3 4 5 6 7 8 9 sh-2.05a$
set – assigns positional parameters to its
$ set `date` $ echo “The date today is $2 $3, $6” The date today is Aug 27, 2001
shift – change the meaning of the positional
#!/bin/sh while test “$1” do echo $1 shift done giant2 $ giant2 fee fie fo fum fee fie fo fum
read one line from standard input and
#!/bin/sh echo “Enter your name:” read fName lName echo “First: $fName” echo “Last: $lName” name $ name Enter your name: Alexander Graham Bell First: Alexander Last: Graham Bell
set –vx (v: echo commands as they are read,
Set – to turn off tracing
$ cat testscript2 #!/bin/sh set -vx a=`ls -l` b=5 ls -l | awk '{print $1;}' $ testscript2 a=`ls -l` ls -l ++ ls -l + a=total 1
1 kenxu instrs 0 May 25 10:26 myfile1
1 kenxu instrs 0 May 25 10:26 myfile2
b=5 + b=5 …