Posts Tagged ‘script’

expr – external program for arithmetic assignments
let – built0in command for arithmetic assignments

e.g.:

$ let x=1+4
$ let ++x # Now x is 6
$ let x=’1 + 4’
$ let ’x = 1 + 4’
$ let x="(2 + 3) * 5" # now x is 25
$ let "x = 2 + 3 * 5" # now x is 17
$ let "x += 5" # now x is 22
$ let "x = x + 5" # now x is 27; NOTE NO $

There’s no need to quote special characters with let. For white spaces, use quoting.

[ … ] or test is the command used to test information about files.

e.g.:

$ [ -f file ] # true if file is an ordinary file
$ [ ! -f file ] # true if file is NOT an ordinary file
$ [ -d file ] # true if file is a directory
$ [ -u file ] # true if file has SUID permission
$ [ -g file ] # true if file has SGID permission
$ [ -x file ] # true if file exists and is executable
$ [ -r file ] # true if file exists and is readable
$ [ -w file ] # true if file exists and is writeable
$ [ file1 -nt file2 ] # true if file1 is newer than file2

Important Notes:
– Use spaces after the ‘[‘ and before the ‘]’.
– Use spaces around operators.

for statement

Posted: 09/08/2011 in Shell Scripting
Tags: , , ,

syntax:
for (name) in (words)
do
(loop-body-statements)
done

e.g.:

for planet in Mercury Venus Earth Mars \
Jupiter Saturn Uranus Neptune Pluto
do
echo $planet
done

for i in *.txt
do
echo $i
grep ’lost treasure’ $i
done

i=0
for parameter
do
let ’i = i + 1’
echo "parameter $i is $parameter"
done

while statement

Posted: 09/08/2011 in Shell Scripting
Tags: , , ,

syntax:
while (test-commands)
do
(loop-body-statements)
done

e.g.:

i=0
while [ "$i" -lt 10 ]
do
echo -n "$i " # -n
let "i = i + 1" # i=$(expr $i + 1) 

if-statement

Posted: 09/08/2011 in Shell Scripting
Tags: , , ,

syntax:
if (test-commands)
then
(statements-if-test-commands-1-true)
elif (test-commands-2)
then
(statements-if-test-commands-2-true)
else
(statements-if-all-test-commands-false)
fi

e.g.:

if grep $1 /etc/passwd > /dev/null 2>&1
then
echo $1 has a local account here
else
echo $1 has no local account here
fi

Execute the enclosed command and output it back.

Symbols used:
$(…) –> enclosed command
`…` –> backticks

e.g.:

$ expr 3 + 2
5
$ i=expr 3 + 2 # error: try execute command ‘3’
$ i=$(expr 3 + 2) # correct
$ i=‘expr 3 + 2‘ # also correct

e.g.:

$ hostname
dhee
$ h=hostname
$ echo $h
hostname
$ h=$(hostname)
$ echo $h
dhee

Quoting

Posted: 09/07/2011 in Shell Scripting
Tags: , ,

Quoting is used to use a special character literally (a character without its meaning).

Three methods of quoting:
– use of double quotes (“weak quotes”)
– use of single quotes (“strong quotes”)
– use of a backslash in front of each special character

e.g.:

$ echo "2 * 3 > 5 is a valid inequality"
$ echo ’2 * 3 > 5 is a valid inequality’
$ echo 2 \* 3 \> 5 is a valid inequality

When to use quoting?
Double quotes – stop the special behavior of all special characters, except:
– $ (variable interpretation)
– ` (back ticks)
– \ (back slash)

Single quotes – stop the special behaviour of all special characters

Backslash – preserves literal behavior of character, except for newline; a long line with one or more than one physical line is treated as one line when placing “\” at the end of each line.

e.g.:

$ find . -name \*.jpg --> find all filenames ending with .jpg
$ locate ’/usr/bin/c*’
$ grep ’main.*(’ *.c
$ i=$(expr i \* 5)

Special Characters and their meaning:

~ - Home directory
 - Command substitution. Better: $(...)
# - Comment
$ - Variable expression
& - Background Job
* - File name matching wildcard
| - Pipe
( - Start subshell
) - End subshell
[ - Start character set ?le name matching
] - End character set ?le name matching
{ - Start command block
; - Command separator
\ - Quote next character
 - Strong quote
" - Weak quote
< - Redirect Input
> - Redirect Output
/ - Pathname directory separator
? - Single-character match in ?lenames
! - Pipline logical NOT
pace or tab - shell normally splits at white space

Command-line parameters are called $0, $1, $2, …
e.g.:

$ shell-script param1 param2 param3 param4 

Special Built-in Variables:
$@: lists all the parameters
$*: lists all the parameters
$?: exit status of last command
$$: processid of the current shell

e.g.:
1. Create a script called parametersample.

#! /bin/sh
echo $0 is the full name of this shell script
echo "first parameter is $1"
echo "second parameter is $2"
echo "third parameter is $3"
echo "total number of parameters is $#"
echo "process ID is $$"

2. Make the file executable

chmod +x parametersample

3. Execute the file.

sh parametersample 1 2 3 4
or
./parametersample 1 2 3 4

4. Check the output.

parametersample is the full name of this shell script
first parameter is 1
parameter is 2
parameter is 3
total number of parameters is 4
process ID is 1693

Variables

Posted: 09/07/2011 in Shell Scripting
Tags: , ,

To assign a variable: no space is around the equal sign
e.g.:

x=10 #correct
x = 10 #incorrect

To read a value of a variable: put a ‘$’ before the variable name
e.g.:

echo "The value of x is $x"

To unset a variable, use the command unset.
e.g.:

unset VAR