Download List of Shell Programs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Positional notation wikipedia , lookup

Approximations of π wikipedia , lookup

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Transcript
List of Shell Programs
S.No
1
2
3
4
5
6
11
12
13
List of Programs
Shell Program to Swap Two Numbers
Shell Program to Add Two Numbers
Shell Program to Find the Biggest of Two Numbers
Shell program to Find the Biggest of Three Numbers
Shell Program to Find the Area and Circumference of a Circle
Shell Program to Find the whether the given Number is Positive,
Negative and Zero
Shell Program to Find the whether the given Number is Even or
Odd
Shell Program to Find the whether the given Year is Leap Year or
Not
Shell Program to Convert Fahrenheit to Celsius and Celsius to
Fahrenheit
Shell Program to Find the Quotient and Remainder of two
Numbers
Shell Program to check whether a number is divisible by 5
Shell Program to find the sum and average of four integers
Shell Program to compute simple interest and compound interest
14
Shell Program to calculate the sum of digits(without using loop)
15
16
17
18
19
20
21
Shell
Shell
Shell
Shell
Shell
Shell
Shell
22
23
Shell Program to
Shell Program to
Number or Not
Shell Program to
Shell Program to
Shell Program to
Statements
7
8
9
10
24
25
26
Program
Program
Program
Program
Program
Program
Program
to
to
to
to
to
to
to
Print the first “N” Odd Number
Print the first “N” Even Number
Find the Factorial of a given Number
Find the Factorial of a given “N” Number
find the GCD of numbers
Compute Power of X
Find whether the given Number is Prime or Not
Find the First “N” Prime Number
Find whether the given Number is Armstrong
reverse the digits of a number
find the sum of the digits of a given number
execute various UNIX commands using Case
1
27
28
29
30
31
32
33
34
35
36
37
38
Shell Program to
Shell Program to
number
Shell Program to
of number
Shell Program to
same
Shell Program to
Shell Program to
Shell Program to
given Numbers
Shell Program to
numbers
Shell Program to
Shell Program to
Shell Program to
given Number
Shell Program to
count the number of vowels in a line of text
find the sum of odd digits and even digits from a
find the smallest and largest number from a set
check the given number and its reverse are
generate Fibonacci Series
find the sum of square of Individual Digits
find the sum of cube of Individual digits of a
find the Smallest and Largest digits of a
compute Gross Salary of an Employee
find the degree of x in a Quadratic Equation
display the digits which are in Odd Position in a
check the given string is palindrome or not
2
1. Write a Shell Program to Swap Two Numbers
ccetstudent@EMCA:~$ cat>swap.sh
echo "Enter value for X:"
read x
echo "Enter value for Y:"
read y
echo "Before swap, x=$x and y=$y"
z=$x
x=$y
y=$z
echo "After swap, x=$x and y=$y"
ccetstudent@EMCA:~$ sh swap.sh
Enter value for X:
10
Enter value for Y:
20
Before swap, x=10 and y=20
After swap, x=20 and y=10
ccetstudent@EMCA:~$
2. Write a Shell Program for Addition of Two Numbers
echo "Addition of Two numbers"
echo "Enter two numbers"
read a
read b
c=`expr $a + $b `
echo "$c"
Output
ccetstudent@EMCA:~$ sh add.sh
Addition of Two numbers
Enter two numbers
3
4
7
3
3. Write a Shell Program to find the Biggest of Two
Numbers
#compare two numbers
echo "Enter the two numbers"
read a
read b
if test $a -gt $b
then
echo "a is big"
else
echo "b is big"
fi
4. Write a Shell program to find the Biggest of Three
Numbers
#compare three numbers
echo "Enter the three numbers"
read a
read b
read c
if test $a -gt $b -a $a -gt $c
then
echo "a is big"
elif test $b -gt $c
then
echo "b is big"
else
echo "c is big"
fi
4
5. Write a Shell Program to Find the Area & Circumference
of a Circle
ccetstudent@EMCA:~$ cat>area.sh
#Area and Circumference of a Circle
echo "Enter the Radius"
read radius
arear=`echo 3.14 \* $radius \* $radius |bc`
circu=`echo 2\* 3.14 \* $radius |bc`
echo "Area of the Circle="$arear
echo "Circumference of the Circle="$circu
ccetstudent@EMCA:~$ sh area.sh
Enter the Radius
10
Area of the Circle=314.00
Circumference of the Circle=62.80
ccetstudent@EMCA:~$
6. Write a Shell Program to Find the whether the given
Number is Positive, Negative & Zero
ccetstudent@EMCA:~$ cat>check.sh
echo "Enter the Number"
read x
if test $x -gt 0
then
echo "The given Number is Positive="$x
elif test $x -eq 0
then
echo "The given Number is Zero="$x
else
echo "The given Number is Negative="$x
fi
ccetstudent@EMCA:~$ sh check.sh
Enter the Number
10
The given Number is Positive=10
ccetstudent@EMCA:~$ sh check.sh
5
Enter the Number
-10
The given Number is Negative=-10
ccetstudent@EMCA:~$ sh check.sh
Enter the Number
0
The given Number is Zero=0
7. Write a Shell Program to Find the whether the given
Number is Even or Odd
ccetstudent@EMCA:~$ cat>odd.sh
echo "Enter the Number to Check"
read n
if test `expr $n % 2` -eq 0
then
echo "The given Number is Even="$n
else
echo "The given Number is Odd="$n
fi
ccetstudent@EMCA:~$ sh odd.sh
Enter the Number to Check
10
The given Number is Even=10
ccetstudent@EMCA:~$ sh odd.sh
Enter the Number to Check
33
The given Number is Odd=33
8. Write a Shell Program to Find the whether the given
Year is Leap year or Not
ccetstudent@EMCA:~$ cat>leap.sh
echo "Enter the Year to Check"
read leap
if test `expr $leap % 4` -eq 0
6
then
echo "The given Year is Leap="$leap
else
echo "The given Year is Not Leap="$leap
fi
ccetstudent@EMCA:~$ sh leap.sh
Enter the Year to Check
1994
The given Year is Not Leap=1994
ccetstudent@EMCA:~$ sh leap.sh
Enter the Year to Check
1996
The given Year is Leap=1996
9. Write a Shell Program to Convert Fahrenheit to Celsius
and Celsius to Fahrenheit
echo "*** Converting between the different temperature scales ***"
echo "1. Convert Celsius temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperatures into Celsius"
echo -n "Select your choice (1-2) : "
read choice
if [ $choice -eq 1 ]
then
echo -n "Enter temperature (C) : "
read tc
# formula Tf=(9/5)*Tc+32
tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
echo "$tc C = $tf F"
elif [ $choice -eq 2 ]
then
echo -n "Enter temperature (F) : "
read tf
# formula Tc=(5/9)*(Tf-32)
tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
echo "$tf = $tc"
7
else
echo "Please select 1 or 2 only"
exit 1
fi
Output
*** Converting between the different temperature scales ***
1. Convert Celsius temperature into Fahrenheit
2. Convert Fahrenheit temperatures into Celsius
Select your choice (1-2) : 1
Enter temperature (C) : 32
32 C = 89.60 F
10. Write a Shell Program to find the Quotient and
Remainder of two Numbers
ccetstudent@EMCA:~$ cat>rem.sh
echo "Enter the Value of Divisor"
read a
echo "Enter the Value of Divident"
read b
quo=`echo $a\/$b |bc`
rem=`echo $a\%$b |bc`
echo "Quotient = "$quo
echo "Remainder = "$rem
ccetstudent@EMCA:~$ sh rem.sh
Enter the Value of Divisor
10
Enter the Value of Divident
3
Quotient = 3
Remainder = 1
11. Write a shell program to check whether a number is
divisible by 5
ccetstudent@EMCA:~$ cat>div5.sh
echo "Enter the Number to Check"
read number
8
if test `expr $number % 5` -eq 0
then
echo "The give Number is Divisible by 5"
else
echo "The given Number is Not Divisible by 5"
fi
ccetstudent@EMCA:~$ sh div5.sh
Enter the Number to Check
4
The given Number is Not Divisible by 5
ccetstudent@EMCA:~$ sh div5.sh
Enter the Number to Check
5
The give Number is Divisible by 5
12. Write a shell program to find the sum and average of
four integers
echo "Enter 4 Numbers to Find Sum and Average"
echo "Enter the 1st Number"
read no1
echo "Enter the 2nd Number"
read no2
echo "Enter the 3rd Number"
read no3
echo "Enter the 4th Number"
read no4
sum=`expr $no1 + $no2 + $no3 + $no4`
avg=`expr $sum / 4`
echo "Sum of 4 Numbers= "$sum
echo "Average of 4 Numbers= "$avg
ccetstudent@EMCA:~$ sh avg.sh
Enter 4 Numbers to Find Sum and Average
Enter the 1st Number
10
Enter the 2nd Number
20
Enter the 3rd Number
9
30
Enter the 4th Number
40
Sum of 4 Numbers= 100
Average of 4 Numbers= 25
13. Write a shell program to compute simple interest and
compound interest
echo "Enter the Principal Amount"
read p
echo "Enter the Rate of Interest"
read r
echo "Enter the Number of Years"
read n
si=`expr $p \* $r \* $n`
si=`expr $si / 100`
echo "The Simple Interest is: Rs $si"
co=`expr 100 + $r`
co=`expr $co / 100`
i=2
ci=1
while [ $i -le $n ]
do
ci=`expr $ci \* $co`
i=`expr $i + 1`
done
ci=`expr $ci \* $p`
echo "The Compound Interest is: Rs $ci"
ccetstudent@EMCA:~$ sh simp.sh
Enter the Principal Amount
5000
Enter the Rate of Interest
10
Enter the Number of Years
1
The Simple Interest is: Rs 500
The Compound Interest is: Rs 5000
ccetstudent@EMCA:~$ sh simp.sh
1 0
Enter the Principal Amount
5000
Enter the Rate of Interest
10
Enter the Number of Years
2
The Simple Interest is: Rs 1000
The Compound Interest is: Rs 5000
14. Write a Shell Program to calculate the sum of digits
(without using loop)
echo "Enter 5 Digit Number"
read num
d1=`expr $num % 10`
num=`expr $num / 10`
d2=`expr $num % 10`
num=`expr $num / 10`
d3=`expr $num % 10`
num=`expr $num / 10`
d4=`expr $num % 10`
num=`expr $num / 10`
d5=`expr $num % 10`
sum=`expr $d1 + $d2 + $d3 + $d4 + $d5`
echo "Sum of 5 Digit Number="$sum
ccetstudent@EMCA:~$ sh sum1.sh
Enter 5 Digit Number
12345
Sum of 5 Digit Number=15
15. Write a Shell Program to print the first “N” Odd
Number
echo "To Print the First N Odd Numbers"
echo "Enter the Number"
read n
i=1
j=1
while [ $j -le $n ]
1 1
do
echo $i
i=`expr $i + 2`
j=`expr $j + 1`
done
ccetstudent@EMCA:~$ sh genodd.sh
To Print the First N Odd Numbers
Enter the Number
5
1
3
5
7
9
16. Write a Shell Program to print the first “N” Even
Number
echo "To Print the First N Even Numbers"
echo "Enter the Number"
read n
i=2
j=1
while [ $j -le $n ]
do
echo $i
i=`expr $i + 2`
j=`expr $j + 1`
done
ccetstudent@EMCA:~$ sh geneven.sh
To Print the First N Even Numbers
Enter the Number
5
2
4
6
8
10
1 2
17. Write a Shell Program to Find the Factorial for a given
Number
echo "Enter a number: "
read num
i=2
res=1
if [ $num -ge 2 ]
then
while [ $i -le $num ]
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
fi
echo "Factorial of $num = $res"
Output
ccetstudent@EMCA:~$ sh fact.sh
Enter a number:
4
Factorial of 4 = 24
18. Write a Shell Program to Find the Factorial for a given
“N” Number
echo "Enter the Number"
read num
i=1
j=1
res=1
if test $j -eq 1
then
echo "The Factorial for the $j Number is="$j
j=`expr $j + 1 `
fi
while [ $j -le $num ]
do
while [ $i -le $j ]
do
res=`expr $res \* $i `
1 3
i=`expr $i + 1 `
done
echo "The Factorial for the $j Number is="$res
res=1
i=1
j=`expr $j + 1 `
done
ccetstudent@EMCA:~$ sh fact.sh
Enter the Number
4
The Factorial for the 1 Number is=1
The Factorial for the 2 Number is=2
The Factorial for the 3 Number is=6
The Factorial for the 4 Number is=24
ccetstudent@EMCA:~$ sh fact.sh
Enter the Number
6
The Factorial for the 1 Number is=1
The Factorial for the 2 Number is=2
The Factorial for the 3 Number is=6
The Factorial for the 4 Number is=24
The Factorial for the 5 Number is=120
The Factorial for the 6 Number is=720
19. Write a Shell Program to compute the GCD for a
Number
echo Enter two numbers with space in between
read a b
m=$a
if [ $b -lt $m ]
then
m=$b
1 4
fi
while [ $m -ne 0 ]
do
x=`expr $a % $m`
y=`expr $b % $m`
if [ $x -eq 0 -a $y -eq 0 ]
then
echo gcd of $a and $b is $m
break
fi
m=`expr $m - 1`
done
ccetstudent@EMCA:~$ sh gcd.sh
Enter two numbers with space in between
20 30
gcd of 20 and 30 is 10
20. Write a Shell Program to compute Power of x
echo "Input number"
read no
echo "Input power"
read power
counter=0
1 5
ans=1
while [ $power -ne $counter ]
do
ans=`expr $ans \* $no`
counter=`expr $counter + 1`
done
echo "$no power of $power is $ans"
Output
Input number
3
Input power
4
3 power of 4 is 81
21. Write a Shell Program to Find the given Number is
Prime or Not
i=2
rem=1
echo -e "Enter a number: \c"
read num
if [ $num -lt 2 ]; then
echo -e "$num is not prime\n"
exit 0
fi
while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do
rem=`expr $num % $i`
i=`expr $i + 1`
done
if [ $rem -ne 0 ]; then
echo "$num is prime\n"
else
echo "$num is not prime\n"
fi
Output
ccetstudent@EMCA:~$ sh prime1.sh
-e Enter a number: 4
1 6
4 is not prime
ccetstudent@EMCA:~$ sh prime1.sh
-e Enter a number: 7
7 is prime
22. Write a Shell Program to Find the First “N” Prime
Number
echo "Enter a number: "
read num1
i=2
j=2
k=0
while [ $j -lt $num1 ]
do
num=$j
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
k=1
fi
i=`expr $i + 1`
done
if [ $k -eq 0 ]
then
echo "$num is Prime Number"
fi
j=`expr $j + 1 `
i=2
k=0
done
ccetstudent@EMCA:~$ sh prime.sh
Enter a number:
9
2 is Prime
3 is Prime
5 is Prime
1 7
7 is Prime
ccetstudent@EMCA:~$ sh prime.sh
Enter a number:
15
2 is Prime Number
3 is Prime Number
5 is Prime Number
7 is Prime Number
11 is Prime Number
13 is Prime Number
23. Write a Shell Program to Find the given Number is
Armstrong Number or Not
echo -n "Enter the number: "
read Number
Length=${#Number}
Sum=0
OldNumber=$Number
while [ $Number -ne 0 ]
do
Rem=$((Number%10))
Number=$((Number/10))
Power=$(echo "$Rem ^ $Length" | bc )
Sum=$((Sum+$Power))
done
if [ $Sum -eq $OldNumber ]
then
echo "$OldNumber is an Armstrong number"
else
echo "$OldNumber is not an Armstrong number"
fi
ccetstudent@EMCA:~$ sh ams1.sh
Enter the number: 3445
3445 is not an Armstrong number
1 8
24. Write a Shell Program to reverse the digit of a given
Number
echo "Program to Reverse the Digit"
echo "Enter the Number"
read num
reve=0
while [ $num -ne 0 ]
do
reve=`expr $num % 10 + $reve \* 10`
num=`expr $num / 10 `
done
echo "The Reverse Digit is :"$reve
ccetstudent@EMCA:~$ sh rever.sh
Program to Reverse the Digit
Enter the Number
123
The Reverse Digit is :321
ccetstudent@EMCA:~$ sh rever.sh
Program to Reverse the Digit
Enter the Number
12345
The Reverse Digit is :54321
25. Write a Shell Program to Find the Sum of digit for a
given Number
echo "Enter the Number"
read n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
sum=$(( $sum + $sd ))
1 9
done
echo "Sum of all digits is $sum"
ccetstudent@EMCA:~$ sh sum.sh
Enter the Number
1234
Sum of all digits is 10
26. Write a Shell program to execute various UNIX
Commands using Case Statements
echo " MENU "
echo "1. List of Files in the Current Directory"
echo "2. Today's Date"
echo "3. Display the Present Month of the Calendar"
echo "4. Logged In Users"
echo "5. Display the Current Working Directory"
echo "6. Quit from the Menu"
echo -n "Enter your Choice:"
read ch
case $ch in
1) ls;;
2) date;;
3) cal;;
4) who;;
5) pwd;;
6) exit;;
*) echo "invalid choice"
exit;;
esac
Enter your Choice:3
March 2013
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
2 0
27. Write a Shell program to count the number of vowels in
a line of text
echo "Enter the text:"
read s
l=`expr length $s`
c=1
vc=0
while [ $c -le $l ]
do
r=`expr substr $s $c 1`
if [ $r = 'a' -o $r = 'e' -o $r = 'i' -o $r = 'o' -o $r = 'u'
then
vc=`expr $vc + 1`
fi
c=`expr $c + 1`
done
echo "The number of vowels in the text $s is: $vc"
]
The number of vowels in the text love is: 2
ccetstudent@EMCA:~$ sh vow.sh
Enter the text:
manivannan
The number of vowels in the text manivannan is: 4
28. Write a Shell program to find the sum of odd digits and
even digits from a number
# shell program to find the sume of odd digits and even digits
echo "Enter a number"
read n
os=0
es=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
if [ `expr $r % 2` = 0 ]
then
es=`expr $es + $r`
2 1
else
os=`expr $os + $r`
fi
n=`expr $n / 10`
done
echo "The sum of odd digits is: $os"
echo "The sum of even digits is: $es"
ccetstudent@EMCA:~$ sh oddeven.sh
Enter a number
1234
The sum of odd digits is: 4
The sum of even digits is: 6
29. Write a Shell program to find the smallest and larges
number from a set of given number
echo "Enter the number of elements:"
read n
s=9999
s1=0
i=1
while [ $i -le $n ]
do
echo "enter the numbers"
read no
if [ $no -lt $s ]
then
s=$no
fi
if [ $no -gt $s1 ]
then
s1=$no
fi
i=`expr $i + 1`
done
shecho "The smallest number is:$s"
echo "The greatest number is:$s1"
ccetstudent@EMCA:~$ sh great.sh
Enter the number of elements:
4
2 2
enter the numbers
123
enter the numbers
456
enter the numbers
234
enter the numbers
336
The smallest number is:123
The greatest number is:456
30. Write a Shell program to check the given number and
its reverse are same
echo "Enter the Number"
read n
# store Single Digit
sd=0
# Stroe number in reverse order
rev=""
#store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(($n % 10 ))
n=$(($n / 10 ))
rev=$(echo ${rev}${sd})
done
echo "$on in a reverse order $rev"
if [ $on -eq $rev ]
then
echo "The two Numbers are same"
else
echo "The two Numbers are different"
fi
ccetstudent@EMCA:~$ sh rev.sh
Enter the Number
123
123 in a reverse order 321
2 3
The two Numbers are different
31. Write a Shell program to generate Fibonacci Series
echo "Enter the Limit"
read n
i=2
echo "Fibonacci Series"
echo "-----------"
a=0
b=1
echo $a
echo $b
while [ $i -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done
ccetstudent@EMCA:~$ sh fib.sh
Enter the Limit
5
Fibonacci Series
----------0
1
1
2
3
32. Write a Shell program to find the sum of square of
Individual Digits
echo "Enter the Number"
read a
b=1
c=0
2 4
while [ $a -gt 0 ]
do
b=`expr $a % 10`
c=`expr $c + $b \* $b \* $b`
a=`expr $a / 10`
done
echo "The Sum of Square of digits is "$c
ccetstudent@EMCA:~$ sh squ.sh
Enter the Number
12
The Sum of Square of digits is 9
33. Write a Shell program to find the sum of cube of
Individual digits of a given Numbers
echo "Enter the Number"
read a
b=1
c=0
while [ $a -gt 0 ]
do
b=`expr $a % 10`
c=`expr $c + $b \* $b \* $b \* $b`
a=`expr $a / 10`
done
echo "The Sum of Cube of digits is "$c
ccetstudent@EMCA:~$ sh cub.sh
Enter the Number
12
The Sum of Cube of digits is 17
34. Write a Shell program to find the Smallest and Largest
digits of a numbers
echo "Enter a number:"
read n
s1=9
s=0
while [ $n -gt 0 ]
2 5
do
r=`expr $n % 10`
if [ $r -gt $s ]
then
s=$r
fi
if [ $r -le $s1 ]
then
s1=$r
fi
n=`expr $n / 10`
done
echo "The largest digit is:$s"
echo "The Smallest digit is:$s1"
ccetstudent@EMCA:~$ sh larg.sh
Enter a number:
2540235
The largest digit is:5
The Smallest digit is:0
35. Write a Shell program to compute Gross Salary of an
Employee
echo "Enter the basic salary:"
read basic
if [ $basic -lt 1500 ]
then
hra=`expr $basic \* 10 / 100`
da=`expr $basic \* 90 / 100`
else
hra=500
da=`expr $basic \* 98 / 100`
fi
gs=`expr $basic + $hra + $da`
echo "HRA:$hra"
echo "DA:$da"
echo "Gross Salary: $gs"
ccetstudent@EMCA:~$ sh gross.sh
Enter the basic salary:
14000
2 6
HRA:500
DA:13720
Gross Salary: 28220
36. Write a Shell program to find the Degree of X in a
Quadratic Equation
echo "enter the value for a"
read a
echo "enter the value for b"
read b
echo "enter the value for c"
read c
d=`expr $b \* $b - 4 \* $a \* $c`
x1=`echo "scale=3; (-$b + sqrt($d)) / (2 * $a)" | bc`
x2=`echo "scale=3; (-$b - sqrt($d)) / (2 * $a)" | bc`
echo "Root 1: $x1"
echo "Root 2: $x2"
ccetstudent@EMCA:~$ sh qua.sh
enter the value for a
3
enter the value for b
5
enter the value for c
2
Root 1: -.666
Root 2: -1.000
37. Write a Shell program to display the digits which are in
Odd Position in a given Number
#shell program to display the digits which are in odd position
echo "Enter a number"
read n
i=1
while [ $n -gt 0 ]
do
r=`expr $n % 10`
2 7
if [ `expr $i % 2` -ne 0 ]
then
echo "The odd digits are: $r"
fi
n=`expr $n / 10`
i=`expr $i + 1`
done
ccetstudent@EMCA:~$ sh pos.sh
Enter a number
12567
The odd digits are: 7
The odd digits are: 5
The odd digits are: 1
38. Write a Shell program to Check the given String is
Palindrome or Not
echo "Enter the String"
read s
l=`expr length $s`
c=1
p=""
while [ $c -le $l ]
do
e=`expr substr $s $c 1`
p=$e$p
c=`expr $c + 1`
done
if [ $p = $s ]
then
echo "palindrome"
else
echo "Not palindrome"
fi
ccetstudent@EMCA:~$ sh pol.sh
Enter the String
malayalam
palindrome
2 8