Unix bourne Shell Scripting (an Introduction)



Yüklə 19,39 Kb.
tarix08.10.2017
ölçüsü19,39 Kb.
#3711

UNIX Bourne Shell Scripting (an Introduction)

Purpose


The purpose of this lab is to familiarise you with the basics of Bourne shell scripting. There are various scripts included in this material which illustrate the points being made, and which you can copy into a file and run to see what they do. At the end of this file are some exercises which test your knowledge of shell scripting.
In shell scripting the Bourne shell (sh) and its extension the Bourne again shell (Bash) are very similar. On robin, type

sh or /bin/sh

to invoke the Bourne shell. (To invoke Bash, type bash)


References


  • Bourne Shell Programming: http://www.ooblick.com/text/sh/

  • Steve Bourne's original shell tutorial: http://partmaps.org/era/unix/shell.html

Calling external programs / Exit status and Return code


  • Chaining. First, note that by convention C programs return a 0 to indicate successful exit status, e.g.,

PROGRAM: code.c #include int main(int argc, char *argv[]) { printf("Hello World!\n"); return 0; }

Later on in the labs we will return to catching the exit code of a program.

This C hello program can be tested by running:

gcc -o out code.c

and then run with:



./out

after calling out use this command to get the exit code:



echo $?

Try changing the return value in the C program to 10, recompile, run, and finally output the return code



Hello World!


Create the file hello.sh.

#!/bin/sh echo Hello World

You must have execute permission on this script in order to run it. Make the file executable by typing the following at the command prompt and hitting return:

chmod u+x hello.sh

You can then run the file as follows:



./hello.sh Hello World!

Command-line arguments and variables


The Command-line parameters provide scripts with a way to access data from the shell. These reserved parameter variables start with the name of the script, $0, and are assigned by position for each argument $i. The number of parameters is set in $#. The script below simply prints its first two arguments:

#!/bin/sh echo $1 echo $2

The example below shows how the programmer can define variables. The variable $string is assigned the value '' argument 1: '' and then concatenated to the command-line parameter $1. Copy the text into a file and call it hello2.sh:

#!/bin/sh string="the argument 1 is: " echo $string$1 #concatenate $string with $1

The script prints out its first command-line argument.

$ ./hello2.sh "Hello World!" argument 1: Hello World!

With regard to user-defined variables (like $string in the above example), NOTE the following:



  • Variables can simply be used without any explicit introduction;

  • Changes to variables within a script are not visible outside the script, e.g.:

$ string="original string" $./hello2.sh argument 1: Hello World! $ echo $string original string

  • The export command makes variables visible to subscripts, but not outer scripts, e.g. suppose script s1.sh uses the variable $STR:

$ export STR="original string" $ ./s1.sh # s1.sh will see STR="original string"

  • There are various built-in variables set by the shell, for example, PATH, HOME, etc.

Arithmetic operations


Performing arithmetic in a shell script does not work as expected. E.g., if you did the following:

x=1+2 echo $x

you would get the result:

1+2


Instead, use the double parenthesis to perform arithmetic:

(( ii=1 )) (( jj=$ii+1 )) echo $jj 2


Conditionals


There are several forms for conditionals, the simplest of which uses if statements. This script tests to see if there is more than one command-line argument:

#!/bin/sh MIN_ARGS=1 if [ "$#" -lt "$MIN_ARGS" ]; then echo more than one command line argument is present else echo Fail fi

The following script tests to see if FILE exists and if it is a “regular file” (i.e. one that is neither a directory nor device file):

#!/bin/sh FILE="fifo" if [ -f $FILE ]; then echo FILE fifo is a regular file fi The appendix at the end of this lab gives some further test conditions that can be used in if statements.


Looping


Loops are useful in shell scripting and have several forms. This script prints three strings:

#!/bin/sh for num in one two three; do echo $num done

When run, it outputs:

one two three

This example prints out a directory listing:

#!/bin/sh for val in $( ls ); do echo item: $val done

Try copying this script into a file and running it. What do you get?

C-style loops are also available.

#!/bin/sh UB=3 for (( i=1; i

When run it outputs:

item: 1 item: 2

The while loop is also available:

#!/bin/sh i=1 LIM=3 while [ "$i" -lt "$LIM" ]; do echo item: $i (( i++ )) done

When run this outputs:

item: 1 item: 2

Exercise 1

Special Variables


Try and find out what these variables do (precede with echo ):

echo $?

$? Exit status or return code of last command $# Number of arguments $@ Argument 1 through to n with Input Field Separator $* "$1" $2" ... $n $! Process id of last background process $$ Process id of shell running this script $- The current shell flags



Exercise 2

Write a shell script in the file myscript.sh that takes 2 or more arguments (call them ff1, ff2, ... ffn). Follow these guidelines:



  • All arguments should be filenames.

  • If less than two arguments are given, an error message is printed to STDERR.

  • If the file ff1 exists (test by -e), an error message is printed to STDERR.

  • If some files in ff2, ..., ffn are not regular files, then print an error message to STDERR at the end, displaying each missing file at once. For example, if files ff1 and ff2 are missing, print ``ERROR: missing files ff1, ff2''.

  • Otherwise, concatenate files ff2, ..., ffn using cat, and store the result in ff1

Run some test cases to be sure the solution is correct. Say we have files ff2:

If you attend all the labs

and ff3:

you will understand more about UNIX!

Then, ./myscript.sh ff1 ff2 ff3 should output the following into ff1:

File created on March 17, 3:23 PM by myscript.sh Author: uxx06m on robin ---------------------------------------------- If you attend all the labs you will understand more about UNIX! -----------------------------------------------

Test your script for the error cases, e.g., ff1 already exists, or ff2 does not exist.

Appendix


This section shows different test operations that can be done within IF statements in shell scripts.

“num1”, “num2”, “string” and “FILE” are numbers strings and files respectively:



string1 = string2 string 1 is equal to string 2 string1 !=string2 string 1 is not equal to string 2 -n string string is not zero length -z string string is zero length -d FILE FILE is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -r FILE FILE exists and is readable -s FILE FILE exists and has length greater than zero -w FILE FILE exists and is writable -x FILE FILE exists and is executable num1 -eq num2 num1 equal to num2 num1 -ne num2 num1 not equal to num2 num1 -lt num2 num1 less than num2 num1 -le num2 num1 less than or equal to num2 num1 -gt num2 num1 greater than num2 num1 -ge num2 num1 greater than or equal to num2


Yüklə 19,39 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2022
rəhbərliyinə müraciət

    Ana səhifə