Sunday, 17 June 2012

¬Write a shell script to check whether the file whose name is scanned exists and readable


read file
if [ -e $file ]
then
if [ -r $file ]
then              
echo The file exists and readable
else
echo The file exist but not readable
fi
else
echo The file is not exist
fi

Write a shell script to accept the name of the user and check out if the same has logged in or not


echo Enter user name:
read user
f=1
for i in `who|tr -s " "|cut -d " " -f 1`
do
if [ $i = $user ]
then
echo The user is login now
f=0
break
fi
done
if [ $f != 0 ]
then
echo The user is logged off
fi

Write a Shell script to accept a string as command line argument and reverse the same


if [ $# -gt 0 ]
then
x=`echo $1 | wc -c`
x=`expr $x - 1`
final=""
while [ $x -gt 0 ]
do
c=`echo $1 | cut -c $x`
x=`expr $x - 1`
final=$final$c
done
fi
echo Reverse string is : $final

Write a Shell script, which counts the number of words in a file, without taking into consideration the blank space, tab spaces and the newline characters without using wc


read file
l=`wc -l $file|cut -d " " -f 1`
echo $l
count=0
coch=0
for (( i=1; i<=l; i++))
do
        n=1
        line=`head -$i $file|tail -1`
        echo $line
        ch=`echo $line|cut -c $n`
        echo character is $ch
        while [ "$ch" != "" ]
        do
                if [ "$ch" = " " ]
                then
                count=`expr $count + 1`
                else
                coch=`expr $coch + 1`
                fi
                n=`expr $n + 1`
                ch=`echo $line|cut -c $n`

        done
done
echo no. of space $count
echo no. of characters $coch


Write a shell script to count number of spaces in a file.


count=0
       space
       for i in `cat a.sh`
       do
       if [ $i != " " ]
       then
       count=`expr $count + 1`
       else
       space=`expr $space + 1`
       fi
       done
       echo $space

Write a shell script to display desired line from a file


read a;
       read b;
       head -$a desline.sh|tail -$b;

Write a shell script that greets the user by saying Good Morning, Good Afternoon, and Good Evening according to the system time


d=`date|cut -d " " -f 4|cut -d ":" -f 1`
if [ $d -gt 4 -a $d -lt 12 ]
then
echo Good Morning
elif [ $d -gt 12 -a $d -lt 16 ]
then
echo Good Afternoon
elif [ $d -gt 16 -a $d -lt 20 ]
then
echo Good Evening
else
echo Good Night
fi

Write a shell script to check whether the entered number is prime or not


flag=0
read a;
for (( i=2; i<a; i++ ))
do
b=`expr $a % $i`
if [ $b -eq 0 ]
then
flag=1
fi
done
if [ $flag -eq 1 ]
then
echo not prime number
else
echo prime number
fi

Write a shell script to generate fibonacci series.


echo Enter the number of terms you want to be in the series;
read n
a=0
b=1
j=0
echo 0
echo 1
while [ $j -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
j=`expr $j + 1`
done

Write a shell script which displays January if we enter Jan, Janu, Janua or January


read mon;
case $mon in
jan|janu|janua|january) echo january ;;
*) echo invalid ;;
esac