Выбрать главу

Apr 2008 30

66. Moving shell files from PWD to specified directory.

$vi prg66

if [ $# -lt 1 ]

then

echo Improper Usage : $0 Pathname

fi

mv *.sh $1

echo All files are moved in the $1 directory

ls $1

Sample Run

$sh prg66 abc

All files are moved in the abc directory

a.sh

b.sh

$sh prg66

Improper Usage : p1 Pathname

67. To print all the files and total number of files in given directory.

$vi prg67

clear

if [ $# -lt 1 ]

then

echo Improper Usage : $0 pathname

fi

oldifs=$ifs

ifs=/

for arg in $*

do

if [ -d $arg ]

then

cd $arg

echo Present directory

echo $arg

echo Files in the directory :

ls

echo total number of files in this directory :

echo ‘ls | wc -w’

else

if [ -f $arg ]

then

echo $arg is a file

exit

fi

fi

done

ifs=$oldifs

Sample Run

$sh prg67

Improper Usage : p1 pathname

$sh prg67 /root

96

Present directory

/root

Files in the directory :

a  aaa.c  abc2  b  c  ddd  ddd1

total files in this directory :

9

$sh prg67 abc

abc is a file

exit

Desktop  p1

68. To sort strings.

$vi prg68

clear

echo Type string 1.

cat >> srt1

echo Type string 2.

cat>> str2

echo Type string 3.

cat>> str3

echo sorted strings are

sort str1 str2 str 3

Sample Run

$sh prg68

Type string 1.

abc

Type string 2.

xyz

Type string 3.

mnop

sorted strings are

abc

mnop

xyz

69. To find binary equivalent of a decimal number.

$vi prg69

clear

echo Enter a number

read a

pow=1

sol=0

while [ $a -gt 0 ]

do

x=‘expr $a % 2’

inter=‘expr $x \* $pow’

sol=‘expr $sol + $inter’

pow=‘expr $pow \* 10’

a=‘expr $a / 2’

done

echo $sol

Sample Run

$sh prg69

enter a number

12

1100

$sh prg69

Enter a number

102

1100110

$sh prg69

Enter a number

2984

101110101000

99

70. To calculate simple interest.

$vi prg70

#Calculate a simple interest

clear

echo Enter values of Principle, Time (in yrs), and rate

read p n r

si=‘expr $p \* $n \* $r / 100’

echo Simple Interest=Rs.

$si

Sample Run

$sh prg70

Enter values of Principle, Time (in yrs), and rate

2500 3 25

Simple Interest=Rs. 1875

71. If the sides of a triangle are denoted by a, b and c then area of the triangle is given by

area = Square root of (s(s-a)(s-b)(s-c))

where, s = (a+b+c)/2

$vi prg71

clear

echo Enter sides of a triangle

read a b c

s=‘expr \( $a + $b + $c \) / 2’

area=‘expr \( $s \* \( $s - $a \) \* \( $s - $b \) \* \( $s - $c \) \)’

area=‘echo sqrt \( $area \) | bc’

echo Area of the triangle is $area

Sample Run

$sh prg71

Enter sides of a triangle

60 70 50

Area of the triangle is 1469

101

72. Program to display system date in format MM/DD/YY & system time in format hrs:mins:secs.

$vi prg72

clear

echo The current system date in required format is :

date +%D

echo The current system time in required format is :

date +%T

Sample Run

$sh prg72

The current system date in required format is :

04/05/08 //  Means 5

th

April 2008

The current system time in required format is :

10:26:47 //  Means 10 hrs 26 mins 47 secs

73. Program to say hello to the user.

$vi prg73

clear

echo Enter your Name

read name

echo Hello $name

Sample Run

$sh prg73

Enter your Name Charles Babbage Hello Charles Babbage Enter your Name Dennis Ritchie Hello Dennis Ritchie

74. Program to display a message using switch case.

$vi prg74

clear

echo Enter a number between 1 and 3

read num

case $num in

1) echo You have Entered 1 ;;

2) echo You have Entered 2 ;;

3) echo You have Entered 3 ;;

*) echo Please enter some value between 1 & 3 ;;

esac

Sample Run

$sh prg74

Enter a number between 1 and 3

3

You have Entered 3

$sh prg74

Enter a number between 1 and 3

2

You have Entered 2

75. Write a menu driven program which has following option-(

a

)    Factorial of a number

(b)

Prime or not

(c)

Odd or even

(d)

Exit

$vi prg75

clear

ch=y

while test $ch = ‘y’

do

echo a. Factorial

echo b. Prime or not

echo c. Odd or even

echo d. Exit

echo Enter choice

read ch

case $ch in

a) echo Enter number

read num

i=1

j=1

while test $i -le $num

do

k=‘expr $i \* $j’

i=‘expr $i + 1’

j=$k

done

echo Factorial of $num is $j ;;

b) echo Enter number

read num

i=2

while test $i -lt $num

do

k=‘expr $num % $i’

if test $k -eq 0

then

echo number is not prime

break

fi

i=‘expr $i + 1’

done

if test $i -eq $num

then

echo number is prime ;;

fi ;;

c) echo enter number

read num

y=‘expr $num % 2’ if test $y -eq 0

then

echo number is even

else

echo number is odd

fi ;;

d) exit ;;

*) echo wrong choice ;;

esac

echo Do you want to continue press y/n

read $ch

done

Sample Run

$sh prg75

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

a

Enter number

4

Factorial of 4 is 24

Do you want to continue press y/n

y

a. Factorial

b. Prime or not

c. Odd or even

d. Exit

Enter choice

b

Enter number 6

number is not prime

Do you want to continue press y/n

y

a. Factorial

b. Prime or not

c. Odd or even