0% found this document useful (0 votes)
5 views3 pages

UNIX lab 2

Uploaded by

saumy ladkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

UNIX lab 2

Uploaded by

saumy ladkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7. Write a shell script to find factorial of a given integer.

echo "Enter a number"


read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done

echo $fact

Output;
Enter a number
3
6
Enter a number
4
24
Enter a number
5
120

8.Write a script to print fibonacci series.


echo "How many number of terms to be generated ?"
read n
function fibonacci
{
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
# -lt stands for equal to
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
}
r=`fibonacci $n`
echo "$r"

Output;
How many number of terms to be generated ?
10
Fibonacci Series up to 10 terms :
0
1
1
2
3
5
8
13
21
34

9.Write a shell script to check whether a file is existing or not.

File=read_file.txt

if [[ -f "$File" ]]; then


echo "$File exist "

fi

Output:
read_file.txt exist

10.Write a shell script to list all of the directory files in a


directory.

# !/bin/bash
echo "enter directory name"
read dir
if[ -d $dir]
then
echo "list of files in the directory"
ls –l $dir|egrep ‘^d’
else
echo "enter proper directory name"
Fi

Output:
guest-glcbIs@ubuntu:~$sh lprg6.sh
enter directory name
dir1
list of files in the directory
drwxrwxr-x 4 guest-glcbls guest-glcbls 140 2012-07-06 14:40 dir1

You might also like