Dana Vrajitoru
C151 Multi-User Operating Systems
Scripts and Shell Programming
Script
- Shell script: a file containing a script or list of commands that the shell
can execute.
- A shell script file must be an executable file.
- Usually it has the extension .sh, although it's not necessary.
- Each line in the script is interpreted by the binary executing the script
and executed.
- Any command recognized by the specified shell can be included in the script
as such.
- We will discuss bash shell programming.
Comments
- Comments: any line starting with a # will be ignored except for the very
first one specifying the shell to execute the script.
- Exception: the first line usually contains information about which shell
must execute the script. This line starts with the #! symbols. Examples:
#!/bin/sh
#!/usr/bin/perl
#!/usr/bin/python
- The first one will be executed by the shell "sh". The second is a Perl
script. The last ones is a Python script.
Combining Commands
- Several commands on the same line: separated with ;
cd .. ; make ; cd src
- Continuing a command: with the \
echo "This is our first shell script \
and we are writing a long line"
- Grouping commands with ()
(cd .. ; make ) ; cd src
- This creates a separate process for each group of commands in separate
parenthesis.
Positional Parameters
- The positional parameters are arguments that can be given in the command
line when we run the script.
- $* contains all the positional parameters.
- These parameters are identified by $number:
- $0 is the name of the script file. $1 is the first argument in the command
line, $2 is the second, etc.
- After 10, the number must be placed in braces: ${10} .
- You can find how many arguments the script has by the variable $#:
- The shift command promotes all the arguments by one number and
discards the first one. The $2 becomes $1, $3 becomes $2, etc. The command is
not reversible.
Command-line Expansion
- The operation of transforming an expression into a command before executing
it. It replaces some patterns and wildcards and syntax elements with what they
stand for.
- Tilde expansion. Replacing ~ with the home directory and ~user with the home
directory of that user.
- Path expansion. Replacing the following wildcards in a path name or in a
file name:
* with anything ls libm*
? with a single character ls /usr/bin/?spell
- The above generates a list of all the paths/files that match the pattern.
Patterns
- Bracket expansion [ ]. Creating a word for every character between the
brackets. We can also specify a range like 0-12 or a-z.
- Brace expansion { }. If a list between braces appears in the expression, a
word is generated for every item in the list.
- Parameter and variable expansion. Replacing an expression starting with a $,
like $PATH, $1, ${12}, with the value of that variable or parameter.
Examples
$ ls *old.txt
bold.txt file_old.txt mold.txt
to_hold.txt
$ ls ?old.txt
bold.txt mold.txt
- Brackets expansion
$ ls slides[12a].html slides[4-6].html
slides1.html slides2.html slidesa.html
slides4.html
slides5.html slides6.html
$ mkdir memo[A-D]; ls memo*
memo[A-D]
- Braces expansion
$ mkdir memo{A,B,C,D} ; ls memo *
memoA memoB memoC memoD
Arithmetic Expansion
- Evaluating an arithmetic expression and replacing it with that value.
- All computations in a shell script are done with integers. Anything else is
converted to 0.
- In assignments the expression must be placed within the following syntax:
$[expression]
$ a=$[2+3] ; echo $a 5
- Another possibility: with the command let
$ let a=2+3 ; echo $a
5
$ declare -i b=7 ; let b=$b+1 ; echo $b
8