You are on page 1of 5

1.

Write A shell script that takes a command –line argument and reports on whether it is
directry ,a file,or something else
Sol:

echo "Enter a file name:"


read f
if [ -f $f ]
then
echo "File"
elif [ -d $f ]
then
echo "Directory"
else
echo "Not"
fi

2. Write a shell script that accepts a file name starting and ending line numbers as
arguments and displays all the lines between the given line numbers
Sol:

$ awk ‘NR<2 || NR> 4 {print $0}’ 5 lines.dat

I/P:line1
line2
line3
line4
line5

O/P: line1
line5
3. Write a shell script that computes the gross salary of a employee according to the
following

1) if basic salary is <1500 then HRA 10% of the basic and DA =90% of the basic
2) if basic salary is >1500 then HRA 500 and DA =98% of the basic
The basic salary is entered interactively through the key board

echo " Enter the Salary "

read sal

if [ $sal -lt 1500 ]

then

da=$(($sal*90/100 ))
hra=$(($sal*10/100))

gsal=$(($sal+$hra+$da))

elif [ $sal -ge 1500 ]

then

hra=500

da=$(($sal*98/100))

gsal=$(($sal+$hra+$da))

fi

echo "Gross Salary is :"$gsal

4. Write a shell script that accepts two integers as its arguments and computes the value
of first number added with the second number

a=$1
b=$2
c=$(($a+$b))
echo$c

5. Write a shell script for printing the following numbers as output.


1
2
3
4
5
6
7
8
9
10

x=0

while [ $x -lt 10 ]

do

echo $x
x=$(($x+1))

done

exercise: write a shell script to print the numbers in the above mentioned way where the user can
input the value of x.

6. Write a shell script for printing the following numbers as output.


0

10

210

3210

43210

x=0

while [ $x -lt 4 ]

do

y=$x

while [ $y -ge 0 ]

do

echo $y '\c'

y=$(($y-1))

done

echo

x=$(($x+1))
done

exercise: write a shell program to print the output as above but the user will give the value of x.
exercise: write a shell program to print the following outputs
a. *
**
***
****
b. ****
***
**
*

7. Write a shell script that displays a list of all files in the current directory to which the
user has read write and execute permissions

read -p "Enter a directory name : " dn


if [ -d $dn ]; then
printf "\nFiles in the directory $dn are :\n"
for fn in `ls $dn`
do
if [ -d $dn/$fn ]; then
printf "<$fn> Directory "
elif [ -f $dn/$fn ]
then
printf "$fn File "
fi
if [ -r $dn/$fn ]; then
printf " Read"
fi

if [ -w $dn/$fn ];then
printf " Write"
fi

if [ -x $dn/$fn ];then
printf " Execute"
fi
printf "\n"
done
else
printf "\n$dn not exists or not a directory"
fi

You might also like