You are on page 1of 33

03117704416

Q1 Write a shell script to find the largest among 3 numbers

echo "Enter 3 numbers"


read a b c
if ((a>b&&a>c))
then
echo "$a is greater"
elif((b>c))
then
echo "$b is greater"
else
echo "$c is greater"
fi

Output:
03117704416
Q2 Find factorial of a number

echo "Enter a number"


read num
fact=1
for (( i=$num;i>=1;i-- ))
do
fact=`expr $fact \* $i`
done
echo "Factorial is $fact"

Output:
03117704416
Q3 Wap to accept a file name from user and find the number of words in it.

echo "Enter file name"


read fname
wc $fname

Output:
03117704416
Q4 Accept a name from user, if its a file display its content if directory show information of folder.

echo "Enter a file/directory name"


read fname
if [ -e $fname ]
then
if [ -d $fname ]
then
ls -l $fname
else
cat $fname
fi
else
echo "No file"
fi

Output:
03117704416
Q5 Write script, using case statement to perform basic math operation of addition, subtraction,
multiplication and division

echo "1.Add 2.Subtract 3.Multiply 4.Divide"


echo "Enter choice"
read ch
echo "Enter two numbers"
read a b
case $ch in
1) c=`expr $a + $b`
echo "Addition: $c"
;;
2) c=`expr $a - $b`
echo "Subtraction: $c"
;;
3) c=`expr $a \* $b`
echo "Multiplication: $c"
;;
4) c=`expr $a \/ $b`
echo "Division: $c"
;;
*) echo "Invalid data"
;;
esac

Output:
03117704416
Q6 Write a script to print the following patterns as indicated below:
#
##
###
####
#####
####
###
##
#

for (( i=1;i<=5;i++ ))
do
for (( j=1;j<=i;j++ ))
do
echo -n "#"
done
echo ""
done
for (( i=4;i>=1;i-- ))
do
for (( j=i;j>=1;j-- ))
do
echo -n "#"
done
echo ""
done

Output:
03117704416
Q7 Write script to see current date, time, username and current directory

echo " Current date and time "


date
echo " Username "
whoami
echo " Current directory "
pwd

Output:
03117704416
Q8 Write script to print given number in reverse order

echo "Enter a number"


read num
s=0
while (($num>0))
do
r=`expr $num \% 10`
s=`expr $s \* 10 + $r`
num=`expr $num \/ 10`
done
echo "Reverse is $s"

Output:
03117704416
Q9 Write script to print the sum of all digits of a given number

echo "Enter a number"


read num
s=0
while ((num>0))
do
r=`expr $num \% 10`
s=`expr $s + $r`
num=`expr $num \/ 10`
done
echo "Sum of digits: $s"

Output:
03117704416
Q11 Write a shell script which takes a file name along the command line and prints its size

echo "Size of file: "


ls -s $1

Output:

Q12 Write a script to accept some numbers at the command prompt and display their sum

echo "Number1 : $1"


echo "Number2 : $2"
c=`expr $1 \+ $2`
echo "Sum is : $c"

Output:
03117704416
Q13 Write a script to input a file name and check whether the file exists. If yes, then check whether
it is a regular file, directory, symbolic link, pipe, socket, character device or any other type of file

echo "Enter file/directory name"


read fname
if [ -e $fname ]
then
if [ -d $fname ]
then
echo "$fname is a directory "
elif [ -c $fname ]
then
echo "$fname is a character file"
elif [ -s $fname ]
then
echo "$fname is a socket file"
elif [ -p $fname ]
then
echo "$fname is a pipe file"
elif [ -f $fname ]
then
echo "$fname is a regular file"
else
echo "Not a file/directory"
fi
else
echo "Does not exist"
fi

Output:
03117704416
Q14 Write a script to input two strings and compare them, whether they are equal. Also print the
string which is greater (which comes later in the dictionary).

echo "Enter two strings"


read s1 s2
if [ $s1 = $s2 ]
then
echo "Strings are equal"
elif [ $s1 \< $s2 ]
then
echo "$s1 comes first in dictionary"
else
echo "$s2 comes first in dictionary"
fi

Output:
03117704416
Q15 Write a script to print all the files in the current directory and remove those files where
filenames begin with ‘j’ or ‘m’

pwd
ls
rm {j,m}*
echo "After deletion "
ls

Output:
03117704416
Q16 Write a shell script to print all prime numbers from 1 to 100

n=1
echo "Prime no b/w 1 to 100"
while (($n<=100))
do
i=2
while (($i<=$n-1))
do
if (($n%$i==0))
then
break
else
i=`expr $i \+ 1`
fi
done
if (($n==$i))
then
echo $n
fi
n=`expr $n \+ 1`
done

Output:
03117704416
Q18 Write a script to enter some elements in the array and print them in reverse order.

arr=(10 8 20 100 12)

echo "Array in original order"


echo ${arr[*]}

echo "Reverse order"


for ((i = 4; i>=0; i--))
do
echo ${arr[$i]}
done

Output:
03117704416
Q19 Display all the command line arguments supplied by user using shift (also called positional
parameters).

echo "All command line arguments $*"

Output:

Q20 Write a script to concatenate two arrays

a=(1 2 3 4 5)
b=(8 7 6 9 3 1);
echo ${a[*]}
echo ${b[*]}
echo "After concatenation:"
c=("${a[@]}" "${b[@]}")
echo ${c[@]}

Output:
03117704416
Q21 Write a script to enter the names of some countries in the array and replace UK with United
Kingdom

cty=('USA' 'India' 'Italy' 'UK')


echo "Before replacing UK"
echo ${cty[*]}
echo "After replacing UK"
echo ${cty[@]/UK/United Kingdom}

Output:
03117704416
Q22 Write a shell script which renames all .txt files as .text files.

ls
f=`ls`
for f in *.txt; do
mv -- "$f" "${f%.txt}.text"
done
echo "After replacing"
ls

Output:
03117704416
Q23 Write a script to enter some numbers in the array and print the sum of all the numbers

num=(1 2 34 41 5 6)
s=0
echo "Array elements ${num[*]}"
for((i=0;i<6;i++))
do
s=`expr $s + ${num[$i]}`
done
echo "Sum of array elements: $s"

Output:
03117704416
Q24 Define a function that receives two parameters and returns the larger number

large()
{
if [ $1 -gt $2 ]
then
return $1
else
return $2
fi
}

echo "Enter two numbers"


read a b
large $a $b
c=$?
echo "$c is greater"

Output:
03117704416
Q25 Define a function to demonstrate local and global variable visibility

a=10
check()
{
a=20
}
check1()
{
local a=4
}
check
echo "Value of a: $a"
check1
echo "Value of a: $a"

Output:
03117704416
Q26 Write a script to accept some strings from command line, assign them in the array and print the
string with the smallest length

i=0
for arg in $@
do
ar[$i]=$arg
i=`expr $i + 1`
done
echo ${ar[*]}
l=${#ar[*]}

s=${#ar[1]}
ss=${ar[1]}
for ((i=0;i<l;i++))
do
sl=${#ar[$i]}
if [ $s -gt $sl ]
then
ss=${ar[$i]}
fi
done
echo "$ss is of smallest length"

Output:
03117704416
Q27 Write a script to load an array with some contents, display them, unset the entire array and load
the new contents.

arr1=(1 2 3 4 5)
echo "Content of array ${arr1[*]}"
unset arr1[*]
echo "Unsetting array..Loading new content"
arr1=(5 3 7 9 8 5 42)
echo "New Content: ${arr1[*]}"

Output:
03117704416
Q28 Define a recursive function to display a text message n no. of times

recur()
{
if [ $1 -gt 0 ]
then
echo "Glory Glory Man Utd"
c=`expr $1 - 1`
recur $c
else
exit
fi
}

echo "Enter a number"


read n
recur $n

Output:
03117704416
Q29 Write a script to search an item in the array

arr1=(32 24 12 65)
echo "Array elements ${arr1[*]}"
echo "Enter number to be searched"
read n
for i in ${arr1[*]}
do
if [ $n -eq $i ]
then
f=1
break
else
f=0
fi
done
if [ $f -eq 0 ]
then
echo "Number not found"
else
echo "Number found"
fi

Output:
03117704416
Q30 Write a shell script to count the number of ordinary files in root directory

`cd ..`
c=0
for i in `ls`
do
if [ -f $i ]
then
c=`expr $c \+ 1`
fi
done
echo "Total regular files: $c"

Output:
03117704416
Q31 Write a shell script to determine whether the given string is a palindrome or not

echo "Enter a string"


read str
len=${#str}
str1=`echo $str | rev`
echo $str1
if [ ${str}=${str1} ]
then
echo "String is palindrome"
else
echo "Not a palindrome"
fi

Output:
03117704416
Q32 Write a shell script which takes a name as parameter and returns the PID(s) of processes with
that name

echo "enter a command"


read c
echo "Process id $c"
echo `pgrep $c`

Output:
03117704416
Q33 Write a script to input names of two files and print the name of the file which is older than
another and also print the permissions assigned to owner, group and others for it

echo "Enter two file names"


read a b
l1=`stat $a`
l2=`stat $b`
if [ "$l1" -nt "$l2" ]
then
echo "Older file $b"
c=`ls -l $b | cut -d " " -f1`
else
echo "Older file $a"
c=`ls -l $a | cut -d " " -f1`
fi
echo "Permissions: $c"

Output:
03117704416
Q34 Write a shell script to print Fibonacci series echo "Enter total number in series"
read n
x=0
y=1
i=2
echo "Series is"
echo -n "$x $y"
while [ $i -lt $n ]
do
i=$(( $i + 1))
z=$(( $x + $y))
echo -n " $z "
x=$y
y=$z
done
echo ""

Output:
03117704416
Q35 Write a shell script to display 100 even numbers

echo "100 even numbers"


for ((i=0;i<=200;i=$i+2))
do
echo -n " $i "
done
echo ""

Output:
03117704416
Q39 Write a script to search for the word ‘exam’ in a file and display total no. of matching words

echo "Enter filename"


read f
if [ -e $f ]
then
echo "Total number of occurences for word exam is: "
echo `tr -cs 'A-Za-z' '\n' < $f | grep -ci "exam"`
else
echo "Not a file"
fi

Output:
03117704416
Q40 Write a script to convert seconds accepted from user to hours and minutes

echo "Enter seconds"


read num
min=0
hour=0
day=0
if((num>59));then
((sec=$num%60))
((num=$num/60))
if((num>59));then
((min=$num%60))
((num=$num/60))
((hour=$num))
else
((min=$num))
fi
else
((sec=$num))
fi
echo "$hour h $min m $sec s"

Output:

You might also like