University of Maryland Eastern Shore Dept. of Computer Science Jianhua Yang
1
CSDP399 Introduction to Unix and Programming
12/22/12
Outline
Unix Shell programming:
Shell scripts. Definition. Uses of shell scripts. Writing shell scripts.
2
CSDP399 Introduction to Unix and Programming
12/22/12
Shell scripts
A shell script is a text file with Unix
commands in it. Shell scripts usually begin with a #! and a shell name (complete pathname of shell).
Pathname of shell be found using the which
command. The shell name is the shell that will execute this script.
E.g: #!/bin/bash
3
CSDP399 Introduction to Unix and Programming
12/22/12
Shell scripts (contd.)
If no shell is specified in the script file, the
default is chosen to be the currently executing shell.
4
CSDP399 Introduction to Unix and Programming
12/22/12
Shell scripts (contd.)
Any Unix command can go in a shell script
Commands are executed in order or in the
flow determined by control statements.
Different shells have different control
structures
The #! line is very important. We will write shell scripts with the Bourne
shell (bash).
5
CSDP399 Introduction to Unix and Programming
12/22/12
Shell scripts (contd.)
A shell script as a standalone is an
executable program:
Must use chmod to change the permissions
of the script to be executable also.
Can run script explicitly also, by specifying
the shell name.
E.g: $ bash myscript E.g: $ csh myscript
6
CSDP399 Introduction to Unix and Programming
12/22/12
Shell scripts (contd.)
Consider the example
$ bash myscript Invokes the bash shell and then runs the
script using it. Its almost as if we had the line #! /bin/bash in the file myscript. Similarly with the second example using csh. myscript need not be an executable as bash is running the script on its behalf.
7
CSDP399 Introduction to Unix and