AIX 5L Basics (Unit 15) – Additional Shell Features – Shell Scripts

#! /usr/bin/ksh – As the first line in a shell script this line specifies that the Korn shell will be used to run this script.

Shell Variables:
———————–
$$ – Process Id (PID)
$0 – Shell script name
$# – Number of arguments passed to the shell script
$* – All command line arguments passed to the script
$? – Exit value of the last command
$! – Process Id of last background process

Positional Parameters:
———————————-
Passed to shell scripts as arguments

$1, $2, …$9
${10}. ${11}, … ${n} (Korn Shell only)

cat para_script
echo First Paramenter entered was $1
echo Second Parameter entered was $2

para_script($0) good($1) day($2) sydney($3)

expr utility:
—————-
Performs integer arithmetic in following precedence
\* – multiply
/ – division
% – remainder
+ – addition
– – subtraction

var1=6, var2=3
expr $var1 / $var2 = 2
expr $var1 – $var2 = 3
expr \(var1 + var2 \)\* 5 = 45
\ – means to ignore the following character
echo $var3

Conditional Execution:
———————————
command1 && command2
if (command1 successful) then do (command2)
ls s* && rm s*

command1 || command2
if (command1 not sucessful) then do (command2)
cd /dir || echo cannot change to /dir1

test command:
———————-
test for a given condition (3 different syntaxes)

1. test expression
2. [expression]
3. [[expression]]

test -f file1
[-f file1]
[[-f file1]]

Operators:
—————
$string1 = $string2 (equal)
$string1 != $string2 (not equal)

$number1 -gt $number2 (greater than)
$number1 -lt $number2 (less than)
$number1 -eq $number2 (number equal)
$number1 -ne $number2 (not equal)

-a $file – File Exists
-d $file – File is a directory
-r $file – File is readable
-w $file – File is writable

if command:
——————
if condition is true
then
carry out this set of actions
else
carry out these alternative actions
fi

$active
cat active
USAGE=”$0 userid” – $0 refers to the script name
if [[$# -ne 1]] – $# refers to the number of parameters
then
echo “Proper Usage: $USAGE”
exit 1
fi

$active wltest

if who | grep $1 > /dev/null
then
echo “$1 is active”
else
ehco “$1 is not active”
fi
exit

cat check_user
$check_user wltest

USAGE=”$0 username”
if [[ $# -ne 1]]
then
echo “Proper usage: $USAGE”
exit 2
fi

grep $1 /etc/passwd > /dev/null
if [[ $? -eq 0 ]]
then
echo “$1 is a valide user”
exit 0
else
ehco “$1 is not a valid user”
exit 1
fi

read command:
———————-
cat delfile
#Usage: delfile
echo “Please enter the filename:”
read name
if [[ -f $name ]]
then
rm $name
else
echo “Error: $name is not an ordinary file”
fi

for loop Syntax:
———————–
for variable in list
do
commands
done

cat count
for var in file1 file2…
do
wc -l $var
done

cat rm_tmp
for FILE in /tmp/*
do
echo “Removing $FILE”
rm $FILE
done

While Loop Syntax:
—————————–
while expression
do
commands
done

cat information
x=1
while [[ $x -lt 9 ]]
do
echo “It is now $(date)”
echo “There are $(ps -e | wc -l) processes running”
echo “There are $(who | wc -l) users logged in”
x=$(expr $x + 1)
sleep 600 (10 min)
done

Command Search Order:
————————————
1. Qualified Pathname
2. Reserved Words
3. Aliases
4. Built-in Commands
5. Functions
6. Path Variables

Leave a Reply

Your email address will not be published. Required fields are marked *

*