Shell Scripts Language
Shell Scripts Language
on the website
Lecture 6
Shell Scripting
What is a shell?
• The user interface to the operating system
• Functionality:
– Execute other programs
– Manage files
– Manage processes
• Full programming language
• A program like any other
– This is why there are so many shells
Shell History
• There are
many choices
for shells
• Shell features
evolved as
UNIX grew
Most Commonly Used Shells
– /bin/csh C shell
– /bin/tcsh Enhanced C Shell
• Scripting
– A set of shell commands that constitute an
executable program
Review: UNIX Programs
• Means of input:
– Program arguments
[control information]
– Environment variables
[state information]
– Standard input [data]
• Means of output:
– Return status code [control information]
– Standard out [data]
– Standard error [error messages]
Shell Scripts
• A shell script is a regular text file that
contains shell or UNIX commands
– Before running it, it must have execute
permission:
• chmod +x filename
• A script can be invoked as:
– ksh name [ arg … ]
– ksh < name [ args … ]
– name [ arg …]
Shell Scripts
• When a script is run, the kernel determines which
shell it is written for by examining the first line of
the script
– If 1st line starts with #!pathname-of-shell,
then it invokes pathname and sends the script as
an argument to be interpreted
– If #! is not specified, the current shell assumes it
is a script in its own language
• leads to problems
Simple Example
#!/bin/sh
#!/bin/sh
• Read: $var
#!/bin/sh
MESSAGE="Hello World"
echo $MESSAGE
Environmental Variables
NAME MEANING
$HOME Absolute pathname of your home directory
$PATH A list of directories to search for
$MAIL Absolute pathname to mailbox
$USER Your login name
$SHELL Absolute pathname of login shell
$TERM Type of your terminal
$PS1 Prompt
Parameters
• A parameter is one of the following:
– A variable
– A positional parameter, starting at 1
– A special parameter
• To get the value of a parameter: ${param}
– Can be part of a word (abc${foo}def)
– Works within double quotes
• The {} can be omitted for simple variables,
special parameters, and single digit positional
parameters.
Positional Parameters
• The arguments to a shell script
– $1, $2, $3 …
• The arguments to a shell function
• Arguments to the set built-in command
– set this is a test
• $1=this, $2=is, $3=a, $4=test
#!/bin/sh
# Parameter 1: word
# Parameter 2: file
grep $1 $2 | wc –l
$ PATH=`myscript`:$PATH
$ grep `generate_regexp` myfile.c
File name expansion
• Wildcards (patterns)
* matches any string of characters
? matches any single character
[list] matches any character in list
[lower-upper] matches any character in range
lower-upper inclusive
[!list] matches any character not in list
File Expansion
$ /bin/ls
• If multiple matches, all are returned file1 file2
$ cat file1
and treated as separate arguments:
a
$ cat file2
b
$ cat file*
a
b
• Handled by the shell (exec never sees the wildcards)
– argv[0]: /bin/cat
– argv[1]: file1 – argv[0]: /bin/cat
– argv[2]: file2 NOT – argv[1]: file*
Compound Commands
• Multiple commands
– Separated by semicolon or newline
• Command groupings
– pipelines
• Subshell
( command1; command2 ) > file
• Boolean operators
• Control structures
Boolean Operators
• Exit value of a program (exit system call) is a number
– 0 means success
– anything else is a failure code
• cmd1 && cmd2
– executes cmd2 if cmd1 is successful
• cmd1 || cmd2
– executes cmd2 if cmd1 is not successful
$ ls bad_file > /dev/null && date
$ ls bad_file > /dev/null || date
Wed Sep 26 07:43:23 2001
Control Structures
if expression
then
command1
else
command2
fi
What is an expression?
• Any UNIX command. Evaluates to true if the
exit code is 0, false if the exit code > 0
• Special command /bin/test exists that does
most common expressions
– String compare
– Numeric comparison
– Check file properties
• /bin/[ often linked to /bin/test for
syntactic sugar (or builtin to shell)
• Good example UNIX tools working together
Examples
if test "$USER" = ”mohri"
then
echo "I know you"
else
echo "I dont know you"
fi
FILES="file1 file2"
$ cat "$FILES"
cat: file1 file2 not found
Simple Commands
• A simple command consists of three types of
tokens:
– Assignments (must come first)
– Command word tokens
– Redirections: redirection-op + word-op
– The first token must not be a reserved word
– Command terminated by new-line or ;
• Example:
– foo=bar z=`date`
echo $HOME
x=foobar > q$$ $xyz z=3
Word Splitting
• After parameter expansion, command
substitution, and arithmetic expansion, the
characters that are generated as a result of
these expansions that are not inside double
quotes are checked for split characters
• Default split character is space or tab
• Split characters are defined by the value of
the IFS variable (IFS="" disables)
Word Splitting Examples
FILES="file1 file2"
cat $FILES
a
b
IFS=
cat $FILES
cat: file1 file2: cannot open
IFS=x v=exit
echo exit $v "$v"
exit e it exit
Pathname Expansion
• After word splitting, each field that
contains pattern characters is replaced by
the pathnames that match
• Quoting prevents expansion
• set –o noglob disables
– Not in original Bourne shell, but in POSIX
Parsing Example
DATE=`date` echo $foo > \
/dev/null