Arrays
08. ARRAYS
(positional
parameters) The Bourne shell does support an array,
but a word list can be created by using positional parameters. A list of words
follows the built-in set command, and the words are accessed by position. Up to
nine positions are allowed.The built-in shift command shifts off the first word
on the left-hand side of the list. The individual words are accessed by
position values starting at 1.
EXAMPLE
set word1 word2
word3
echo $1 $2 $3
Displays word1, word2, and word3
set apples peaches
plums
shiftShifts off
apples
echo $1
Displays first element of the list
echo $2
Displays second element of the list
echo $*
Displays all elements of the list
Command substitution To assign the output of a Linux command to a variable, or use the
output of a command in a string, backquotes are used.
EXAMPLE
variable_name=`command`
echo
$variable_name
now=`date`
echo $now
echo "Today
is `date`"
9. FILE TESTING
The Bourne shell
uses the test command to evaluate conditional expressions and has a built-in
set of options for testing attributes of files, such as whether it is a
directory, a plain file (not a directory), a readable file, and so forth.
EXAMPLE
-d File is a
directory
-f File exists
and is not a directory
–r Current user
can read the file
–s File is of
nonzero size
–w Current
user can write to the file
#!/bin/sh
1 if [ –f file ]
then
echo file exists
fi
2 if [ –d file ]
then
echo file is a
directory
fi
3 if [ -s file ]
then
echo file is not
of zero length
fi
4 if [ -r file -a
-w file ]
then
echo file is
readable and writable
fi
10. EXECUTION OF SHELL SCRIPT :
1.By using change
mode command
2.$chmod u + x
sum.sh
3.$ sum.sh
or
$ sh sum.sh
VIVA-VOCE QUESTIONS
I)
Number System Convertion.
a)
Convert hexadecimal to decimal.
#bc
ibase=16
105
261
b) convert decimal to binary.
#bc
obase =2
75
1110101
c)
convert decimal to hexadecimal
#bc
obase=16
203
010109
II)
Display date with day-month-year.
Display 28-06-16 in the same format it is
echo" 28-06-16"
III)
Create directory as given.
E F
a) Create the file a and b in directory B.
b) Copy the file a to b in directory E.
Step:
$mkdir A
$A]mkdir B
$B]mkdir C
$C]mkdir D
$cd D
$mkdir E
$mkdir F
Enter B
Create a file a
Copy a to b
Answer:
cd B
cat> a
cat>b
cp a b
Comments
Post a Comment