Shell Exercise for practice - Bourne Shell



CIS 118 Intro to LINUX Class Exercise

Week 5

Simple - Bourne Shell Programming

A shell script is text file containing a list of commands that are run in sequence. A shellscript should start with a line such as the following:

#!/bin/bash

This indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is very important, since the syntax of different shells can vary greatly. An example:

#!/bin/bash

echo "Hello World”"

echo $USER $PWD

$USER and $PWD are system variables.- standard variables defined by the bash shell.

Variables

Variables are set on the left with just the name, referenced by placing a ‘$’ in front of the name:

Person=”Alex” -OR- set person=”Alex”

and refer to it as follows: echo $X

• bash will reference an eror if there is a space on either side of the = sign. Person = Alex

• quotes are not always necessary except where the variable contains whitespace characters (space or tab). For example,

person=Alex and Jenny; echo $person

person=”Alex and Jenny”; echo $person

The shell evaluates the command line as a sequence of words or tokens separate by whitespace characters

Single quotes versus double quotes versus escape character

Variable names are expanded within double quotes, suppressed in single quotes or if preceded by a “\’ or “escape” character. An example:

echo $person

echo "$person"

echo ‘$person’

echo "\$person=$person"

Using Braces to Protect Your Variable names

A potential problem in variable expansion has to do with string concatenation onto a variable name.

Text example:

person=Alex

echo $person

echo $personander

you can delineate the variable name by placing it in brackets

echo ${person}ander

Always Use Quotes to enclose your variables

Not enclosing variable names in double quotes can cause logic problems especially if your variable's value contains either (a)spaces or (b) is an empty string. An example is as follows:

#!/bin/bash

X=""

if [ -n $X ]; then # -n tests to see if the argument is non empty

echo "the variable X is not the empty string"

fi

A better script would have been:

#!/bin/bash

X=""

if [ -n "$X" ]; then # -n tests to see if the argument is non empty

echo "the variable X is not the empty string"

fi

The Test Command and Operators

All in conditional are variation of s the test command. Test returns true or false (or exits with 0 or non zero) depending respectively on whether the test is passed or failed. It works like this:

test operand1 operator operand2 -OR-

if  [operand1 operator operand2]

The test command needs to be in the form "operand1operatoroperand2"

if  [ 1=2 ] is true

if  [ 1 = 2 ] is false

The test command is typically abbreviated in this form:

[ operand1 operator operand2 ]

Another potential trap comes from not protecting variables in quotes (see above).

A brief summary of test operators

|operator |produces true if... |number of operands |

|-n |operand non zero length |1 |

|-z |operand has zero length |1 |

|-d |there exists a directory whose name is operand |1 |

|-f |there exists a file whose name is operand |1 |

|-eq |the operands are integers and they are equal |2 |

|-neq |the opposite of -eq |2 |

|= |the operands are equal (as strings) |2 |

|!= |opposite of = |2 |

|-lt |operand1 is strictly less than operand2 (both operands should be integers) |2 |

|-gt |operand1 is strictly greater than operand2 (both operands should be integers) |2 |

|-ge |operand1 is greater than or equal to operand2 (both operands should be integers) |2 |

|-le |operand1 is less than or equal to operand2 (both operands should be integers) |2 |

Loops

Loops are constructions that enable one to reiterate a procedure or perform the same procedure on several different items. There are the following kinds of loops available in bash

• for loops

• while loops

• until loops

For loops

#!/bin/bash

for X in red green blue

do

echo $X

done

Values in the loop are evaluated as variable and quoting rules apply.The for loop iterates the loop over the space seperated items. Note that if some of the items have embedded spaces, you need to protect them with quotes. An example:

#!/bin/bash

for X in "dark green" "light blue" “red”

do

echo $X

done

While Loops

While loops repeat a command sequence "while" a given condition is true. An example of this:

#!/bin/bash

X=0

while [ $X -le 20 ]

do

echo $X

X=$((X+1))

done

Until Loops

Until loops epeat a command sequence "while" a given condition is true. An example of this:

#!/bin/bash

X=0

while [ $X -le 20 ]

do

echo $X

X=$((X+1))

done

Command Substitution

Command Substitution enables you to take the output of a command and assign it to a variable. It takes to forms:

variable=`command`

variable=$(command)

An example:

#!/bin/bash

files="$(ls ~)"

dirs=`ls –ald ~`

echo $files

echo $dirs

It also allows for performing mathermatics in BASH:

#!/bin/bash

X=`expr 3 * 2 + 4`; echo X

X=$((3 * 2 + 4))

Using the usual algerbraic symbols: Add +, Subtract -, Multiply *, Divide /

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download