Professional Documents
Culture Documents
Output:
03117704416
Q2 Find factorial of a number
Output:
03117704416
Q3 Wap to accept a file name from user and find the number of words in it.
Output:
03117704416
Q4 Accept a name from user, if its a file display its content if directory show information of folder.
Output:
03117704416
Q5 Write script, using case statement to perform basic math operation of addition, subtraction,
multiplication and division
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
Output:
03117704416
Q8 Write script to print given number in reverse order
Output:
03117704416
Q9 Write script to print the sum of all digits of a given number
Output:
03117704416
Q11 Write a shell script which takes a file name along the command line and prints its size
Output:
Q12 Write a script to accept some numbers at the command prompt and display their sum
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
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).
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.
Output:
03117704416
Q19 Display all the command line arguments supplied by user using shift (also called positional
parameters).
Output:
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
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
}
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
}
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
Output:
03117704416
Q32 Write a shell script which takes a name as parameter and returns the PID(s) of processes with
that name
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
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
Output:
03117704416
Q39 Write a script to search for the word ‘exam’ in a file and display total no. of matching words
Output:
03117704416
Q40 Write a script to convert seconds accepted from user to hours and minutes
Output: