Ryan Hulguin
Outline
• Unix shells • Bourne-again Shell (bash) • Interacting with bash • Basic scripting • References
Unix shells
• This lets users issue commands to the Unix operating system
• Users can interact with various parts of the Unix operating system such as – the input/output system – the scheduler – memory management
• The shell is simply a program whose job is to execute other programs
• Different shells exist such as the C shell (csh), the Korn shell (ksh), the Bourne shell (sh) and the Bourne-again shell (bash)
Bourne-again shell (bash)
• bash is the default shell on modern Linux distributions as well as Mac OS X
• bash is both a command interpreter and a high-level programming language
• When used as a programming language, bash processes commands stored in files called shell scripts
• Most system shell scripts are written for bash • Like other programming languages, bash has
variables and control flow commands (i.e. for loops and if statements)
Active participation
• It is highly encouraged to try all the examples as we go along
• You may use your own shell from a Linux/Mac terminal
• You may also use the free linux shell provided by http://simpleshell.com
Shell Variables
• Variables in a bash script are typically written with all-uppercase letters – This is not a strict requirement
• Assignment is done using the equals sign without spaces VAR1=“test string”
• Referencing the value of a variable is done using the dollar sign echo “My assigned variable contains $VAR1” – An exception to this rule is evaluating arithmetic
expressions, covered later
Documenting bash scripts with comments • It is always a good idea to document bash
scripts with comments • Any text immediately following the # character
will be considered a comment and ignored by the shell # This is a comment
• The exception to this the shebang #! found at the top of most scripts
• The shebang specifies which shell to use to execute the script – Example: #!/bin/bash
Command substitution
• To assign the value of another bash command/script to a variable, command substitution is used TODAYS_DATE=$(date “+%m/%d/%y”) TODAYS_DATE=`date “+%m/%d/%y”` echo $TODAYS_DATE echo $(TODAYS_DATE)
• Newer scripts will use $(), while older scripts will use backticks ` `
• The $() convention makes it easier to read and nest commands within commands
• Visit http://www.simpleshell.com • Click ‘Start my session’ • nano hello_world.sh • Ctrl-X • Y to save modified buffer
• Hit return/ enter to accept filename
Our first bash script
Running our first bash script
• We can run our bash script several ways • We can pass the script to the shell of our choice /bin/bash ./hello_world.sh /bin/sh ./hello_world.sh
• We can specify which shell to use with the shebang, and then make the script executable nano hello_world.sh Insert #!/bin/bash at the top Ctrl x, y, enter to save chmod u+x ./hello_world.sh ./hello_world
Embedding variable values in text
• Suppose you have the following variable defined PREFIX=sun
• If you wanted to echo the following words using this prefix: sunflower, sunshine, sunset you would use echo ${PREFIX}flower echo ${PREFIX}shine echo ${PREFIX}set
• Note the use of the braces Without the braces, echo $PREFIXflower would return nothing since the variable PREFIXflower is undefined
Using parameters in a bash script
• scripts are often called using input parameters • To reference these parameters use ${position #}
where position # is the specific parameter to use that starts at 1
• Example: echo “The first input parameter is ${1}” echo “The second input parameter is ${2}”
• You can also get the number of input parameters passed using ${#} echo “There were ${#} input parameters passed”
Setting default values for parameters
• Many times you would like parameter set to a default value if none is given
• Suppose you have a script that is expecting a directory as an input parameter, but needs to default to the $HOME directory if none is specified
• default_param.sh:
#!/bin/bash # List the contents of the 1st argument # Use the $HOME directory if none is specified TARGET=${1:-$HOME} ls $TARGET
Setting default values for variables
• Just like parameters, variables can also have default values assigned to them
• default_variable.sh: #!/bin/bash # Show the first 5 lines of $TARGET file TARGET=/proc/cpuinfo head -5 ${TARGET:=./default_variable.sh} echo “TARGET file is $TARGET” unset TARGET echo “----------” head -5 ${TARGET:=./default_variable.sh} echo “TARGET file is $TARGET”
The for control structure
• for loop-index do
commands done
• The loop-index takes on the values of each of the command line arguments
• Example: # Display all of the command line arguments for INPUT_ARG do
echo $INPUT_ARG done
The for…in control structure
• for loop-index in argument-list do
commands done
• The loop-index takes on the values of each argument in the specified argument-list
• Example: # Display all of the animals in a given list for ANIMAL in lions tigers bears do
echo $ANIMAL done
The for control structure with C like syntax
• bash also allows a syntax for for loops much like the C programming language
• # count from 1 to 10 on a single line for (( count=1; count<10; count+=1 )) do
echo –n “$count ” done echo
References
• A Practical Guide to Linux® Commands, Editors, and Shell Programming by Mark G. Sobell
• Bash Cookbook: Solutions and Examples for Bash Users by Carl Albing, JP Vossen, and Cameron Newham
• http://en.wikibooks.org/wiki/Bash_Shell_Scripting
• These slides will be posted on http://www.nics.tennessee.edu/hpc-seminar-series