Title: UNIX Shell Scripting
1UNIX Shell Scripting
2What is shell scripting?
- Interpreted (non-compiled) language
- Slower compared to compiled languages
- Much more portable than compiled language though
- This presentation uses the bash scripting language
3Configuring for execution
- In order to execute a shell script in a UNIX
environment, you have to configure the files
permissions with the chmod command - gt Chmod 700 file1.csh
- The script must be executed from the folder which
it resides in.
4Shortcut alias
- You can set a script to be run from any folder by
setting an alias for the file. - This can be done by editing the user file which
contains aliases, for example, .bashrc - alias run/home/user/file1.csh
5Hello World
gt hello.csh gt Hello World gt
!/bin/bash echo Hello World
- The !/bin/bash line must be on the first line of
all bash scripts - it indicates where the
compiler for the script is located. - echo is the shell command to print out a line
6All for one, and quotes around them all
- In order to include whitespace in variables, and
in several other cases, statements must be
surrounded by quotation marks. There are three
different types
(double quotes) - basic type, enclosed
variables are substituted with their values
(single quotes) - same as doubles, except no
variable substitution (back ticks) - used for
evaluating enclosed commands
7Variability
- Variables are defined as follows
- variable_namevalue
- There must be no whitespace between the operands
and the operator (xy) - Unlike most compiled languages, bash scripts do
not have different variable types - so no
declaring variables of type int, char, etc.
8Echoing variables
- Except at execution, when referring to variables,
they must be prefixed with a - Depending on which type of quotation mark you use
to enclose an echo commands statement, enclosed
variables may be substituted, as in the following
example, where x 12.
INPUT echo variable x x echo variable x
x
OUTPUT gt variable x 12 gt variable x x
9Control Structures
- if - if (statement) is true, then do one set of
instructions, otherwise, do a different one - while - while (statement) is true, do a set of
instructions. - Also, there are the for, case, and until
structures, but we will not go over these two.
10If (wishes fishes)
- This structure basically goes
- if (x)then (y)else (z)fi
- There neednt necessarily be an else, but if you
use an if statement, you must conclude with a
fi statement.
11Ifthenelsefi
- In the following code segment, from_address is a
variable which contains the name of the sender of
an email.
if from_address Wendy then echo
You have mail from Wendy else echo Wendy
has not sent you any mail fi
12While - the loopiest of the loops
- The while control structure basically goes
- While (x)then (y)done
- While indicates the beginning of the loop and
tests the condition. - Then is executed as long as While is true.
- Done indicates the end of the loop segment.
13Whiledodone
- The following is a short while loop that counts
to 3
!/bin/bash number1 echo -n "Testing..." while
"number" -lt 4 do echo -n
"number..." numberexpr number
1 done echo ""
gt test.csh gt Testing123 gt
14Test conditions
- Below are several different tests that can be
done on numbers or strings - the left box is what
were used to, and the right box is the bash
script code.
15Readin, Riting, and Rithmatic
- There are two ways in a bash script to do simple
arithmatic (add , subtract -, multiply ,
divide /, and modulus ) - The first way is using the expr command
- x(expr 3 4) -or- xexpr 3 4
- The second is using double parenthesis
- x((3 4))
- Note that both ways deal only with integers -
neither handles deciman values.
16Please enter your name
- In some cases, you might need to prompt for user
input - this is done with the read command. - Format read variable_name
!/bin/bash echo What is your name? read
name echo Hello name.
17References
- http//www.linuxnewbie.org/nhf/Programming/Introdu
ction_to_bash_Shell_Scripting.html - http//pegasus.rutgers.edu/elflord/unix/bash-tute
.html - http//www.devhood.com/tutorials/tutorial_details.
aspx?tutorial_id468