0% found this document useful (0 votes)
6 views14 pages

Shell Programme

The document provides a comprehensive guide on writing and executing shell scripts using the vi editor and Cygwin terminal. It includes various examples of shell scripts for tasks such as printing user input, checking even or odd numbers, finding the greatest of three numbers, and generating multiplication tables. Additionally, it covers basic arithmetic operations, file manipulation, and control structures in shell scripting.

Uploaded by

jatinchauhan6560
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)
6 views14 pages

Shell Programme

The document provides a comprehensive guide on writing and executing shell scripts using the vi editor and Cygwin terminal. It includes various examples of shell scripts for tasks such as printing user input, checking even or odd numbers, finding the greatest of three numbers, and generating multiplication tables. Additionally, it covers basic arithmetic operations, file manipulation, and control structures in shell scripting.

Uploaded by

jatinchauhan6560
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

Bca & Bsc.

IT sem – 4
Subject : Unix

Shell script program in vi editor

 How to execute program follow given


step
1) Open cygwin terminal
2) Create file using text editor like (vi [Link])
3) Name script file with extension .sh
4) Go to input mode press i or A
5) Start script with #!/bin/sh
6) Write some code
7) Then press ESC key
8) Save the file as filename .sh
9) Then press :wq (file save and exit)
10) For executing script file in cygwin command
11) bash [Link]
1) Bash Shell script to print a number entered
by a user
#!/bin/sh
echo "enter a number:"
read number
echo "you have entered : $number"
2) Bash Script to check whether a number is
even or odd
#!/bin/sh
read -p "enter a number : " number
if [ `expr $number % 2` -eq 0 ]
then
echo "number is even"
else
echo "number is odd"
fi

3)Shell script to find the greatest of three


numbers
#!/bin/sh
echo "enter num1:"
read num1
echo "enter num2:"
read num2
echo "enter num3:"
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
echo "num1 is :$num1"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]

then
echo "num2 is :$num2"
else
echo "num3 is :$num3"
fi

4)Largest number command line


#!/bin/sh
a=$1
b=$2
c=$3
if [ $# -lt 3 ]
then
echo "$0 n1 n2 n3"
exit 1
fi
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is largest integer"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "$b is largest integer"

elif [ $c -gt $a -a $c -gt $b ]


then
echo "$c is largest integer"
else
echo "sorry cannot guess number"
fi

5)Write a shell script to find out leap year


#!/bin/sh
echo "Enter Year:"
read y
year=`expr $y % 4`
if [ $year -eq 0 ]
then
echo "$y is Leap Year"
else
echo "$y is not Leap Year"
fi

6)Best Shell script to half pyramid using *


#!/bin/sh
rows=4
for((i=1; i<=rows; i++))
do
for((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done
7)Bash Shell script to print inverted half
pyramid using *
#!/bin/sh
rows=5
for((i=rows; i>=1; i--))
do
for((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done

8)Swap two numbers using third variable


#!/bin/sh
firstNumber=12
secondNumber=24
echo "Before Swapping"
echo "First number: $firstNumber"
echo "Second number: $secondNumber"
temp=$firstNumber
firstNumber=$secondNumber
secondNumber=$temp
echo "After Swapping"
echo "First number: $firstNumber"
echo "Second number: $secondNumber"

9) Swap two numbers without using third


variable
firstNumber=5
secondNumber=10
echo "Before Swapping"
echo "First number: $firstNumber"
echo "Second number: $secondNumber"
firstNumber=$((firstNumber - secondNumber))
secondNumber=$((firstNumber + secondNumber))
firstNumber=$((secondNumber - firstNumber))
echo "After Swapping"
echo "First number: $firstNumber"
echo "Second number: $secondNumber"

10)multiplication table
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line
argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for given
number"
exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$n * $i =$(($i * $n))
done
11) marksheet
echo "Enter the five subject marks for the student"
read m1 m2 m3 m4 m5
sum1=`expr $m1 + $m2 + $m3 + $m4 + $m5`
echo "Sum of 5 subjects are: " $sum1
per=`expr $sum1 / 5`
echo " Percentage: " $per
if [ $per -ge 60 ]
then
echo "You get Distinction"
elif [ $per -ge 50 ]
then
echo "You get First class"
elif [ $per -ge 40 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi
11)(2)marksheet program 2nd method
echo "Enter the five subject marks for the student"
read m1 m2 m3 m4 m5
sum1=$(($m1+$m2+$m3+$m4+$m5))
echo "Sum of 5 subjects are: " $sum1
per=$(($sum1/5))
echo " Percentage: " $per
if [ $per -ge 60 ]
then
echo "You get Distinction"
elif [ $per -ge 50 ]
then
echo "You get First class"
elif [ $per -ge 40 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi

12) file read command


echo "Enter Your choice:"
echo "1 : read file"
echo "2 : copy file"
echo "3 : Rename file"
read n
case $n in
1)
echo "Enter file name which file content you want
to display"
ls
read file
cat $file
;;
2)
echo "Enter old file name and new file name which
you want to copy"
ls
read file1 file2
cp $file1 $file2
;;
3)
echo "Enter old file name and new file name which
you want to rename"
ls
read file1 file2
mv $file1 $file2
;;
*)
echo "Enter perfect choice.."
;;
esac
13) shell script for find factorial number.
#!/bin/sh
echo "enter number:"
read num
i=`expr $num - 1 `
while [ $i -ge 1 ]
do
num=`expr $num \* $i`
i=`expr $i - 1`
done
echo "factorial number is :$num"

14) write shell script on Arithmatic in shell


read -p “enter value a:” a
read -p “enter value b:” b
c=`expr $a + $b`
echo “the value of addition=$c”
d=`expr $a - $b`
echo “the value of subtraction=$d”
e= expr $a \* $b`
echo “the value of multiplication=$e”
f=`expr $a / $b`
echo “the value of division=$f”
g= `expr $a % $b`
echo “the value of modulus=$g”

You might also like