Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Programs using Linux shell script

Page
No

Name of the Experiment

S.No
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)

Swapping without temporary variable


Swapping using temporary variable
Square & Cube of a number
Even or odd number
File or directory
Type of character entered
Usage of case statement
Comparison of two strings
Comparison of two `strings from command line
Length of a string

1
2
3
4
5
6
7-8
9
10
72

1) Swapping Without Temporary Variables


SOURCE CODE
echo -n "enter two numbers"
read a
read b
echo "Before swapping"
echo " A = $a B = $b"
a=`expr $a + $b`

# `expr $a + $b` = $(( $a + $b ))

b=`expr $a - $b`
a=`expr $a - $b`
echo "After swapping "
echo "A = $a B = $b"

OUTPUT:
"1swpwitouttemp.sh" [New] 11L, 185C written
[David@linuxstuserver ses2]$ sh 1swpwitouttemp.sh
enter two numbers10
20
Before swapping
A = 10 B = 20
After swapping
A = 20 B = 10
[David@linuxstuserver ses2]$

2) Swapping Using Temporary Variables


SOURCE CODE
echo -n "enter two numbers"
read a
read b
echo "Before swapping"
echo " A = $a B = $b"
c=$a
a=$b
b=$c
echo "After swapping "
echo "A = $a B = $b"

OUTPUT:
"2swpwittemp.sh" [New] 11L, 148C written
[David@linuxstuserver ses2]$ sh 2swpwittemp.sh
enter two numbers100
200
Before swapping
A = 100 B = 200
After swapping
A = 200 B = 100
[David@linuxstuserver ses2]$

3) Square and Cubes of A Number


SOURCE CODE:
echo -n "enter a number"
read num
sqr=`expr $num \* $num`
cube=`expr $sqr \* $num`
echo "the squre of $num is $sqr"
echo "the cube of $num is $cube"

OUTPUT:
"3sqrcub.sh" [New] 7L, 150C written
[David@linuxstuserver ses2]$ sh 3sqrcub.sh
enter a number10
the squre of 10 is 100
the cube of 10 is 1000
[David@linuxstuserver ses2]$

4) Even or Odd Number


SOURCE CODE:
clear
echo enter the number
read n
r=1
r=`expr $n % 2`
if [ $r -eq 0 ]

# -eq for digits or numbers

then
echo even
else
echo odd
fi
OUTPUT:
enter the number
5
odd
[David@linuxstuserver ses2]$

5) Given Input Is File or Directory


SOURCE CODE
clear
echo enter a filename
read fname
if test -f $fname
then
echo "$fname is a ordinary file"
elif test -d $fname
then
echo "$fname is a Directory"
else
echo "$fname is Invaliddd"
fi

OUTPUT:
enter a filename
aaa
aaa is a ordinary file
[David@localhost ses2]$

6) Type of Character Entered


SOURCE CODE:
clear
echo enter a character from the keyboard
read ch
case $ch in
[a-z]) echo its a alphabet
;;
[0-9]) echo its a number
;;
?) echo it is a special character
;;
*) echo u entered more
;;
esac
OUTPUT:
enter a character from the keyboard
a
its a alphabet
[David@linuxstuserver ses2]$

7) Usage of Case Statement


SOURCE CODE:
clear
echo -e "Files and Directories are @ present \n"
ls
echo -e "\n\n"
echo -e "MENU\n\n1.display a file"
echo 2.copy a file
echo 3.create a directory
echo enter ur choice
read ch
case $ch in
1) echo enter filename to display the content of......
read fname
cat $fname
;;
2) echo enter two files for copying from one to another
read file1 file2
cp $file1 $file2
echo -e "\nAfter copying The Directory is ....\n"
ls
;;
3) echo enter the directory
read dname
mkdir $dname
echo -e "\nAfter Creating The current Directory is ....\n"
ls
;;
*) echo invalid option
esac

OUTPUT:
Files and Directories are @ present
aaa b bbb case.sh jj
MENU
1.display a file
2.copy a file
3.create a directory
enter ur choice
1
enter filename to display the content of......
aaa
clear
echo -e "Files and Directories are @ present \n"
ls
echo -e "\n\n"
echo -e "MENU\n\n1.display a file"
echo 2.copy a file
echo 3.create a directory
echo enter ur choice
read ch
case $ch in
1) echo enter filename
read fname
cat $fname
ls
;;
2) echo enter two files for copying from one to another
read file1 file2
cp $file1 $file2
ls

;;
3) echo enter the directory
read dname
mkdir $dname
ls
;;
*) echo invalid option
esac
[David@localhost ses2]$

8) Comparison of Two Strings


SOURCE CODE
clear
echo "enter a string1"
read first
echo "enter a string2"
read second
if [ $first = $second ]

# this = for other than digits equals

then
echo equal
else
echo unequal
fi

OUTPUT:
enter a string1
abcxyz
enter a string2
abcxyz
equal
[David@linuxstuserver ses2]$

10

9) Comparison Of Two Strings From Command Lines

SOURCE CODE:
if [ $1 = $2 ]
then
echo "Strings are equal ...."
else
echo "Strings are not equal....."
fi
OUTPUT
[David@linuxstuserver ses2]$ sh 9cmp2strcmdline.sh vasavi vasavi
Strings are equal ....
[David@linuxstuserver ses2]$ sh 9cmp2strcmdline.sh vasavi ivasav
Strings are not equal.....
[David@linuxstuserver ses2]$

11

10) Finding the Length of A Given String


SOURCE CODE:
echo -n "Enter the string "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " the length of the string is $len"

OUTPUT:
9strlength.sh" [New] 6L, 123C written
David@linuxstuserver ses2]$ sh 9strlength.sh
Enter the string vasavi college
the length of the string is 14
David@linuxstuserver ses2]$

12

You might also like