Sunday, 17 June 2012

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


clear
echo enter any number..
read n
m=$n
sum=0
while [ $m != 0 ]
do
            y=`expr $m % 10`
            x=`expr $y \* $y \* $y`
            sum=`expr $sum + $x`
            m=`expr $m / 10`
done
if [ $sum -eq $n ]
then
            echo the number is armstrong...
else
            echo the number is not armstrong...
fi

Write a shell script to arrange numbers in ascending or descending order as per the user choice



echo enter the no of element
read n1
for (( i=0; i<$n1; i++ ))
do
            echo enter `expr $i + 1` the element.
            read a[$i]
done
for (( i=0; i<$n1; i++ ))
do
            for (( j=`expr $i + 1`; j<$n1; j++ ))
            do
                        if [ ${a[$i]} -gt ${a[$j]} ]
                        then
                                    x=${a[$i]}
                                    a[$i]=${a[$j]}
                                    a[$j]=$x
                        fi
            done
done
echo 1.Ascending  2.Descending
echo enter your choice...
read c
if [ $c = 1 ]
then
            echo the ascending order is....
            for (( i=0; i<$n1; i++ ))
            do
                        echo ${a[$i]}
            done
elif [ $c = 2 ]
then
            echo the descending order is...
            for (( i=$n1; i>0; i-- ))
            do
                        echo ${a[`expr $i - 1`]}
            done
else
            echo wrong choice......
fi

Write a shell script to accept two filenames and check if both exist. If the second filename exists, then the contents of the first filename should be appended to it. If the second filename does not exist then create a newfile with the contents of the first file


echo enter the file name
read first
echo enter the second file name
read second
if [ -e $first ]
then
if [ -e $second ]
then
cat $first>>$second
else
cat $first>$second
fi
fi

Write a shell script to accept an alphabet from the user and list all the files/directory starting with that alphabet in the current directory


echo enter character
read c
for i in `ls -l|tr -s " "|cut -d " " -f 9`
do
 x=`echo $i|cut -c 1`
if [ $x = $c ]
then echo $i
fi
done

Write a shell script to find the number of ordinary files and directory files in the current directory


f=0
d=0
for i in `ls -l|tr -s " "|cut -c 1`
do
if [ $i = "-" ]
then
f=`expr $f + 1`
elif [ $i = "d" ]
then
d=`expr $d + 1`
fi
done
echo no of ordinary files are $f
echo no of directories are $d

Write a shell script to find the file or directory with the maximum size in the current directory


max=0
for i in `ls -l|tr -s " "|cut -f 5 -d " "`
do
if [ $max -le $i ]
then
max=$i
fi
done
echo maximum size is: $max

Write a shell script to accept a number and a word as command line arguments and print the word the given number of times on each line


i=1
f=""
temp=$1" "
while [ $i -le $3 ]
do
f=$f$temp
i=`expr $i + 1`
done
i=1
while [ $i -le $2 ]
do
echo $f
i=`expr $i + 1`
done

Write a shell script to check if the input string is a palindrome


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
if [ $final = $1 ]
then
echo String is palindrome
else
echo The string is not palindrome
fi

¬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