session 2: shell scriptingcreating a shell script creating a shell script consists of three basic...
TRANSCRIPT
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Session 2: Shell Scripting
Craig A. Struble, Ph.D.
July 14, 2010
1 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Creating a Shell Script
Creating a shell script consists of three basic steps:
1 Create a text file with a text editor like pico, vi or emacscontaining the sequence of commands to execute.
2 Make the file executable with the command
$ chmod a+x scriptname
where scriptname is the name of your script.
3 Execute the script from the command line by typing
$ ./scriptname
assuming your script is in your working directory.
2 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Exercise
1 Using a text editor, create a file named hello with thecontents
#!/bin/bash
read name
echo Hello $name
2 Make it executable with the command
$ chmod a+x hello
3 Run the script
$ ./hello
3 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Variables
Variables are named locations storing values.
General format for assigning variables
variable=value
A simple example
year=2010
4 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Retrieving Values
Retrieve values preceding name with a dollar sign ($).
So if we have the script
year=2010
echo The year is $year
The output of the script will be
The year is 2010
5 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Without Variables
#!/bin/bash
echo The calendar for 2010 is:
cal 2010
echo Last "year’s" calendar was:
cal 2009
6 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
With Variables
#!/bin/bash
year=2010
echo The calendar for $year is:
cal $year
echo Last "year’s" calendar was:
cal $(( year - 1 ))
7 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Exercises
1 Modify the calendars script so that it has a variable namedp year for previous year. Assign it the value of the yearprevious to the one stored in year.
2 Continue to modify the calendars script so that it prints out
The calendar for the previous year 2009 was:
instead of
Last year’s calendar was:
using the p year variable.
8 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
With Comments
A comment starts with a # and continues until the end of the line.
#!/bin/bash
#
# Print out this year’s and last year’s calendars.
#
year=2010
echo The calendar for $year is:
cal $year
echo Last "year’s" calendar was:
cal $(( year - 1 ))
9 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Command Line Parameters
Consider the command
./myscript one two three
The command has three command line parameters, which areprovided to numbered variables $1, $2, and $3 as illustratedbelow. Note that the command itself is assigned to $0.
./myscript one two three
$0 $1 $2 $3
10 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
With Command Line Parameters
#!/bin/bash
#
# Print out this year’s and last year’s calendars.
#
year=$1
echo The calendar for $year is:
cal $year
echo Last "year’s" calendar was:
cal $(( year - 1 ))
11 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Number of Command Line Parameters
The number of command line parameters provided to a script isstored in the special variable $#.
#!/bin/bash
#
# Script name: nparams
# Print the number of command line parameters.
#
echo You passed $# parameters.
12 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Exercise
1 Use the nparams script to find out how many command lineparameters are passed in the following cases:
$ ./nparams
$ ./nparams one two three
$ ./nparams "one two" three
$ ./nparams "one two three"
$ ./nparams one ’two three’
13 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Exercise
2 Write a script named params to print out the first 5command line parameters. Then, use that script to see whatthe parameters are for the following runs.
$ ./params
$ ./params one two three
$ ./params "one two" three
$ ./params "one two three"
$ ./params one ’two three’
To get started, the script should look like
#!/bin/bash
echo The first parameter is $1
echo The second parameter is $2
to print the first two parameters.
14 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Conditional Execution
The general form of a conditional statement is
if CONDITION
then
COMMAND1
COMMAND2
...
fi
15 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
With Conditional Statements
#!/bin/bash
#
# Print out this year’s and last year’s calendars.
#
# Check for one parameter. If the number of parameters
# is not equal (-ne) to 1, print usage and exit.
if [ $# -ne 1 ]
then
echo "Usage: $0 year"
exit 1
fi
# Everything is OK
year=$1
echo The calendar for $year is:
cal $year
echo Last "year’s" calendar was:
cal $(( year - 1 ))
16 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
For Loop
The general form of a for loop is
for VARIABLE in WORDS
do
COMMAND1
COMMAND2
...
done
17 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Countdown
#!/bin/bash
#
# Countdown
#
for i in 10 9 8 7 6 5 4 3 2 1
do
echo T minus $i
done
echo "Liftoff!"
18 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
While Loop
The general form of a while loop is
while CONDITION
do
COMMAND1
COMMAND2
...
done
19 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Another Countdown
#!/bin/bash
#
# Another Countdown
#
i=10
while [ $i -gt 0 ]
do
echo T minus $i
i=$(( i - 1 ))
done
echo "Liftoff!"
20 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Redirect Output
To make the output of a command go to a file instead of thescreen, use the > operator. The following will create a file namedhello.out containing the text Hello World.
echo Hello World > hello.out
If the file already exists, it will be overwritten, so be careful.
21 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Redirect Output, Appending
To append instead of overwrite the file, use the >> operator. Thefile will be created if it does not already exist, but will add to theend of an existing file. The following will add the text GoodbyeWorld to the file hello.out.
echo Goodbye World >> hello.out
22 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Redirect Input
Use the < operator to read input from a file instead of typing in onthe keyboard. The following will copy the contents of somefileand output them to the file duplicatefile.
cat < somefile > duplicatefile
23 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Here Documents
#!/bin/bash
name=$1
cat <<EOF > afile
Hello World
Goodbye World
My name is $name
EOF
24 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Condor Submission Generation
#!/bin/bash
#
# Create a simple Condor submission file.
# This doesn’t handle complicated quoting
# or executables outside of the working directory.
#
if [ $# -lt 1 ]
then
echo "Usage: $0 program argument1 argument2 ..."
exit 1
fi
program=$1
shift # Remove the first argument
# Generate the submission file
cat <<EOF > $program.submit
Universe=vanilla
Executable=$program
Arguments=$@
Output=$program.out
Error=$program.err
Log=$program.log
Queue
EOF
echo $program.submit created.
25 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Accessing Scripts from Anywhere
If a script you write is generally useful, you’ll want to access itfrom any directory, not just your working directory. Almost allshells use a special variable $PATH to list the directories wherecommands our found. On pere.marquette.edu, the bin
directory in $HOME (i.e. $HOME/bin) is in the path. So to use yourscript from anywhere, move it with
$ mv scriptname $HOME/bin
and then execute with
$ scriptname
26 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Case Study 1: Preparing Data
In this case study, we will prepare a script to generate directoriesand input files for analysis on a Cartesian grid. We specify themaximum x and y coordinates of our study on the command lineand generate a simple input file containing x and y. Each input filewill be stored in a directory named runN, where N is defined by
N(x, y) = maxx ∗ y + x
27 / 28
Introduction Variables Comments Command Line Parameters Conditional Execution Loops File Redirection Accessing Scripts from Anywhere Case Studies
Case Study 2: Make Project Structures
In this case study, we will prepare a script to create a simpleproject directory structure and a template README.txt file. Thedirectory structure will be
$ProjectName
bin data doc workspace
28 / 28