Scarborough BS

Bourne Shell Scripting


Main Links:
| Home | Business Directories | About Scarborough | Events In Scarborough | Scarborough History | Topics Of Interest |

This page contains some basic shell scripting commands for use in the UNIX environment. These commands may also be used in the Linux environment although the syntax will vary between different OS environments (UNIX or LINUX). This information is provided for reference only. Other UNIX/LINUX commands will be added as time goes by.

Page Listing:


The shell

The shell selection for a script can be performed with options. These are as follows: ( - turns on + turns off) :

    -v Print lines before they are executed.

    -x Print the interpreted command before it is executed.

    -f Suppress normal filename expansion.

    Shell Script selection must be in the first line of the script - Example:

      #!/bin/sh -xv

      sets -xv

* Home


Variables

  • assign a value to a variable
    • var=value

  • To echo the contents of a variable
    • echo $var

  • To reference the variable
    • $var

  • To change the variable
    • read var

If the variable is contained within text then the variable name should be included within braces {}

    echo ${variable}s

To include blank spaces in a variable definition, use ""

    the_variable_name="The variable content"

* Home


Displaying Strings

The Echo command automatically adds a new line at the end. To supress this include a: \c

Examle:

    echo "This is some information: \c"

Set reserved special characters can be used within variable assignment and echo statements. These are as follows:

    \b

    backspace

    \c

    no new line

    \n

    new line

    \r

    carriage return

    \t

    tab

    \0nnn

    The ASCII character represented

    by the octal number nn

* Home


User Input - variables assignment

The read command handles its input as follows:

Input Read

Variables assigned

Single variable read Variable set to entire line
Equal number of variables and words Each variable holds one word
More words than variables Last variable holds all remaining words
More variables than words Extra variables have a null value
Variables, but no input read silently fails 1

* Home


Filename matching

To list all normal files in the shell's working directory.

    echo *

    tar_files=*.tar

    ab_files=*.[ab] files ending in .a or .b (not .ab)

    echo [A-Z]* files starting with a capital letter

NOTE: If a filename pattern does not match any files then the variable would be set to the actual pattern itself!

    []

range of single characters to match

    [!]

range of single characters to exclude

    ?

any single character

    *

zero or more characters

* Home


Command Assignment

To assign the output of a command to a variable, we use command substitution ``

todays_date=`date`

date normally sends its output to stdout. Enclosing the date command within grave accents(`) sends it to the variable

echo "The date today is `date`"

* Home


Parameter Substitution

${variable} returns the value of variable. It is possible to return a substituted value depending on the original value.

    Substitution Expressions

    Substitute Expression

    Behaviour

    ${variable:-substitute} If variable exists and is not null, return its value, otherwise return substitute
    ${variable:=substitute} If variable exists and is not null, return its value, otherwise set it to substitute and then return its value
    ${variable:+substitute} If variable exists and is not null return substitute, otherwise return null
    ${variable:?message} If variable does not exist or is null print message and terminate the script

    examples:

    echo "Enter date[RETURN=`date`] \c"; read dt

    echo "date entered : $dt"

    echo "substitution returns : ${dt:=`date` }"

    echo "dt is now set to $dt"

* Home


Quoting ""

quote types :

    ""

Weak quotes.

Allow parameters and command substitution

    ''

Strong quotes.

Does not expand any characters.

    \

Ignores next character.

* Home


Handling numeric expressions(integers)

to mathematically evaluate an expression we have to use expr:

    echo "1 + 1"

    1+1

    echo `expr 1 + 1`

    2

expr evaluates the expression before the assignment

shell metacharacters i.e. * need to be escaped

expr 1 * 2 expr: syntax error

expr 1 \* 2

The following numeric are avaliable

+

integer addition

-

subtraction

*

multiplication

/

integer division

%

remainder

* Home


Operators - Comparisons

=

equal

!=

not equal

<

less than

>

greater than

<=

less than or equal

>=

greater than or equal

expr operator can be used on integers and strings.

expr "abc" \>= "aaa" - will return true.

* Home


Exit Status

All commands, when executed, return an exit status. The exit status indicates if the command was successful of not.

the exit code is held in the variable $?

* Home


Test command

test $a = $b

[ $a = $b ]

return exit status codes:
      0 if true
      1 if false

Test commands for strings:

    The following are true if ...

    -z string if length of string is zero

    -n string if length of string is non-zero

    string1 = string2 equal

    string1 != string2 not equal

    str str is not null

NOTE: arithmetic expresssions are NOT handled by the test command

    When testing strings always put variables in "   "
    This will test the condition even if the variable is empty

* Home


File testing

the following tests can be used with test on files:

test returns true if ..
-r exists and is readable
-w writable
-x executable
-f if file exists
-d directory
-h symbolic link
-s size greater than zero
-c character special file
-b block special file

example:

    test -d /home/myusername

* Home


Numerical testing

    • -eq
    • -ne
    • -gt
    • -ge
    • -lt
    • -le

where:

    l = less

    g=greater

    e,eq=equal

    n=not

* Home


Combined tests

the following operators are also available

  • ! Unary negation operator
  • -a Binary and operator
  • -o Binary or operator (higher precidence than -0)

examples

    test ! -f /tmp true if /tmp does not exist

    test -w /tmp -o -w /usr/tmp

    True if /tmp is writable or /usr/tmp is writable, if checking /tmp is writable then the check for /usr/tmp is not executed.

* Home


if-then-else

        if condition

        then

          command

        fi

        if condition

        then

          command

        else

          command

        fi

    example:

      if [ -w /tmp ]

      then

        echo "/tmp is writable"

      else

        echo "/tmp not writable"

      fi

* Home


if-then-elif

(elif - else if)

        used the same way as if-then-else but allows multiple testing of condition statements

* Home


Case

        case string in

          pattern1)

            cmds ;;

          pattern2)

            cmds ;;

          pattern3 | pattern4)

            cmds ;;

          *)

            cmds ;;

        esac

* Home


For

        for variable in word_list

        do

          cmds

        done

Where word_list could be the following :

    * - all files in current directory

    1 2 3 4 5 6 - simple list

    `fgrep -l 193 *.log`

* Home


While-until

while statements executes loop whilst the tested command returns true

until statements executes loop whilst the tested condition is false

          while cond-cmd

          do

            cmds

          done

          until cond-cmd

          do

            cmds

          done

* Home


break-continue

The command break and continue are used to skip sections of a while and until loop.

break exits from the enclosing for or while loop

continue resumes the next iteration of the enclosing for or while loop

while cond-cmd

do

    if cond then

      break

    fi

    cmds

done

* Home


true-false

true always exits with 0

false always exits with 1

these are useful for infinite loops

    while true

    do

      cmds

    done

* Home


Arguments

Scripts can be envoked with arguments. These arguments are assigned in the following way

e.g.

    script par1 par2 par3

    $0 = script

    $1 = para1

    $2 = para2

    $3 = para3

    $# = number of arguments (not including $0)

    $* lists all arguments except $0

* Home


Shift

A limitation on using $n to print out the n'th argument is we cannot print out the $10 argument and above as n can only be a single digit!

to access the 10th or later argument we have to use shift

shift will cause the command line arguments to be left shifted so the $1 returns the value of $2

Does not affect $0

* Home


Set

set will set the scripts personal parameters

For example:
set hello there
    sets $1 to "hello"
    and $2 to "there"


Or

set `date` will set the following

    $1=day of the week

    $2=month

    $3=day

    $4=time

    $5=TZ

    $6=year

set is very useful for extracting tokens from lines!

* Home


The $@ variable

If we execute a script with the following parameters:

    script "file a" "file b"

    $* = file a file b (looks like 4 files)

    "$*" = "file a file b" (looks like 1 file)

to get around this problem we use $@

    $@ = file a file b (looks like 4 files)

but ...

    "$@" generates "file a" and "file b"

* Home


Functions

A function is a number of shell commands that are grouped together under a 'function name'. Call the function to execute the group of commands. Functions help to create meaningful scripts that are easier to de-bug.

      function_name()

      {

        command

        command

      }

      function_name [args]

functions emulate scripts within scripts. $* may hold different values to the parent executing script. An Exit codes can be produced by using return

* Home


Return

return causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed

      function()

      {

        command

        command

        return n ( n is a numeric value usually 1 for true )

      }

* Home


Redirection

The following operators can be used for re direction

file1 < file2
file 1 will receive its input from file 2

file1 > file2
file1's output will be redirected to file2, creating file2 if it does not exist, and overwriting any existing contents of file2

file1 >> file2
file1's output will be appended to the end of file2, if file2 does not exist it will be created and written to.

file2 << string
will use the input from stdin for file1 until string is met.

* Home


Standard Error

cat filename > file1

If file filename does not exits or there is an error then redirection to file1 will fail, and instead the content will be displayed on the screen (stderr)

Redirecting stderr

    To ensure that error messages go to file1 we need to use the following command

        cat filename > file1 2>&1

    The operator 2>&1 instructs the shell to redirect stderr to stdout (&1)

    we could redirect the errors to a separate file ...

        cat filename > file1 2>error_file

    To ignore all errors redirect to /dev/null

        cat filename > file1 2>/dev/null

* Home


File descriptors

Name

Filename

File Descriptor

Standard Input

stdin

0

Standard Output

stdout

1

Standard Error

stderr

2

Their uses:

    2>file redirect stderr to file

    2>&1 merge stderr with stdout

* Home


Word Count

wc -l counts the number of words

* Home


Which

There may be multiple instances of the same command within an operating system environment. Use the "which " command to determine the command you are using.

which echo

Should return "/usr/bin/echo" in most instances

* Home



Main Links:
| Home | Business Directories | About Scarborough | Events In Scarborough | Scarborough History | Topics Of Interest |


Page Author: S-BS
Scarborough BS
©2005 and beyond