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) :

* Home


Variables

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

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

* Home


Displaying Strings

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

Examle:

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.

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.

* 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:

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:

NOTE: arithmetic expresssions are NOT handled by the test command

* 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:

* Home


Numerical testing

where:

* Home


Combined tests

the following operators are also available

examples

* Home


if-then-else

* Home


if-then-elif

(elif - else if)

* Home


Case

* Home


For

Where word_list could be the following :

* Home


While-until

while statements executes loop whilst the tested command returns true

until statements executes loop whilst the tested condition is false

* 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

done

* Home


true-false

true always exits with 0

false always exits with 1

these are useful for infinite loops

* Home


Arguments

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

e.g.

* 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

set is very useful for extracting tokens from lines!

* Home


The $@ variable

If we execute a script with the following parameters:

to get around this problem we use $@

but ...

* 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.

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

* 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

* Home


File descriptors

Name

Filename

File Descriptor

Standard Input

stdin

0

Standard Output

stdout

1

Standard Error

stderr

2

Their uses:

* 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




A Bit More...