Creating Unix Shell Scripts
Part 1
Ted Ward
Oklahoma State University Institute of Technology
What is a shell script?
In its simplest form, its just a text file with Unix
commands in it
A shell script can also contain “programming logic”
and variables
For this presentation we will assume we are using a
BASH shell
Different shells may have different commands
Specifying the interpreter
BASH scripts should always begin with
#!/bin/bash
This tells the operating system that this is a script
and the interpreter that should be used to execute
this script is /bin/bash
bash is the program and it is located in the /bin folder
Creating a BASH program
The traditional Hello World in BASH
Use any text editor to create a file named hello.sh
and type into the following program
#!/bin/bash
# Comments begin with number signs
# This is my hello world program
echo “Hello World”
Making the BASH program
executable
Make sure you are in the same folder as your BASH
script “hello.sh”
Set the permissons on hello.sh to make it executable
$> chmod +x hello.sh
You can verify by entering “ls l hello.sh”
Running the BASH program
Run the program by entering ./hello.sh
$> ./hello.sh
Hello World
Why the ./ ?
It instructs the OS to find hello.sh in the current folder
It is needed if hello.sh is not located in your computer's
execution path
Type “echo $PATH” to see your path
You can modify the path by editing ~/.bashrc
Initializing variables
Variables are automatic, meaning if you assign something to
a unique name, a variable will be automatically created
filename=“file.txt”
Creates a variable named filename with the value “file.txt”
x=2
Creates a variable named x with the value 2
p1=“$1”
Creates a variable named p1 with the value of the first
parameter from the command line
Using variables
The name of the variable must be prepended with a
$ to be accessed
echo “Variable v1 contains: $v1”
echo $somevar
echo `expr $1 + $2`
Displays the sum of command line arguments 1 and 2
Note the ` character is not a single quote, but the accent mark
located on the top left of your keyboard
Command line arguments
$1 represents the first argument after the command
name
$2 the second argument, etc...
$* can be used to represent all arguments
$# can be used to represent the number of arguments
given
Command line arguments
Enter the following program
cmdline.sh
#!/bin/bash
echo “First Argument: $1”
echo “Second Argument: $2”
echo “All Arguments: $*”
Run command with: “./cmdline.sh A B C D”
Shift
Cycles through the arguments
cmdline.sh
#!/bin/bash
echo “First Argument: $1”
shift
echo “Second Argument: $1”
shift
echo “Third Argument: $1”
Run command with: “./cmdline.sh A B C D”
Checking for the existence
of parameters
The if command is used to check conditions and
perform actions depending on the result
if [ $# eq 0 ]
Checks if the number of arguments is zero
if [ ] are standard parts of the if statement
$# represents the number of arguments
eq represents “is equal” when dealing with numbers
Checking for the existence
of parameters
if [ “$user” = “joe” ]
Checks to see if the string $user contains the string “joe”
Use the = sign for comparing strings
if [ z "$1" ]
The expression is true if the variable $1 is not defined
Conditional bodies
The complete if statement includes a then and a fi
statement to denote the beginning and end of the
body of the if statement
if [ expression ]
then
statements to execute if expression is true
fi
Example conditional
If no arguments given, say so and exit, otherwise
display a long listing of the file specified by $1
Arguments.sh
if [ z $1 ]
then
echo “No argument was given”
exit
fi
ls lh $1
Executing arguments.sh
$ ./arguments.sh
No argument was given
$ ./arguments.sh /programs/bash
total 12K
rwxrxrx 1 tward tward 79 20080505 06:58 arguments.sh
rwxrxrx 1 tward tward 231 20080429 16:28 cmdline.sh
rwxrxrx 1 tward tward 2.6K 20080429 16:28 loops.sh
$
The End
Of the presentation