Читаем 100 Shell Programs in Unix полностью

Calculator-bc (base conversion): You can call it best calculator. Once you type bc at the prompt, you are in the calculator mode. The prompt disappears.

$bc

10+5

15

3.8*2-4

3.6

quit

bc also supports functions like sqrt.

$bc

sqrt(16)

4

for(j=1,j<=5;j++)j

1

2

3

4

5

quit

$

Math related command (factor)

$factor 15

15: 3 5 24

24: 2 2 2 3

press ctrl+c for quiting

$

Taking Decisions

The if else Statement: The general format of this statement is :

if test

then

command(s)

else

command(s)

fi

The case…esac Statement: This is another decision making statement provide by UNIX. It is also known as multi conditional statement.

The general format of this statement is:

case $variable in

value 1) command(s);;

value 2) command(s);;

*) command ;;

esac

Here, the command(s) given under *) will execute when nothing matches.

The loop control structure:

The FOR loop: The FOR loop in UNIX is different from the FOR loop in other languages. Here lists of items are specified, instead of a range, say 1 to 50.

Format of this loop may be given as:

for variable in list

do

command

——————

——————

——————

done

Example:

for i in 1 2 3 4 5 6 7 8 9 10 11 12

do

cal $i 2000

done

The WHILE loop: UNIX supports while… statements to perform repetitive tasks. The format of this statement may be given as:

while test

do

command(s)

done

or

while [ ]

do

command(s)

done

Example:

a=0

while test $a – le 10

do

echo $a

a=‘expr $a + 1’

done

The Until loop: This is another type of looping statement supported by UNIX. It has following format:

until [ ]

do

command(s)

done

The functioning of UNTIL loop is inverse of WHILE loop. Here, the set of specified command(s) are executed as long as the condition is false.

Break and continue statements: Both of these commands are used with WHILE, UNTIL and FOR loops. The break statement terminates the inner-most loop and executes the statements, written after it.

On the other hand, ‘continue’ does not terminate the loop, but continues the execution of the next cycle of the loop. Both of these can be illustrated as:

The break Statement:

while true

   do

————

————

if—————

then

     break

else

     ————

     ————

fi

————

————

done

command(s)

The continue Statement:

while true

   do

————

————

if—————

then

     continue

fi

   done

command(s)

VII. PORTABILITY WITH C

To write a ‘C’ program in UNIX, type your program in vi editor. Give the file name with extension .c (prg1.c). cc is the compiler of ‘C’ in UNIX.

Example:

#include

#include

int main()

{

int a=10,b=20,c;

c=a+b;

printf(“sum of a & b=%d”,c);

return 0;

}

Procedure:

Let’s start by compiling the program prg1.c. Type:

cc prg1.c

If you list files in the current directory, you will find that this command has produced a file called a.out. You can then run it by typing:

./a.out

Now let’s suppose that you want to give your executable program the name “sumofab”, rather than the default (and not very informative) name “a.out”. Of course, you could just rename the file, but it is better to use the “-o” option to tell cc what filename to create.

cc -o sumofab prg1.c

And now, to run it you would type:

./sumofa

PART II

IPROGRAMS

1. Write a shell script to find whether an input integer is even or odd.

$vi prg1

clear

echo “enter a number”

read x

y=‘expr $x % 2’

if test $y –eq 0

then

echo “Number is even” else

echo “Number is odd” fi

Sample Run

$sh prg1

enter a number

11

Number is odd

$sh prg1

enter a number

12

Number is even

2. Write a shell script to find out the greatest among three inputs.

$vi prg2

clear

echo “enter the value of a b c”

read a

read b

read c

if test $a –gt $b –a $a –gt $c

then

echo “a is greatest”

else

if test $b –gt $c

then

echo “b is greatest”

else

echo “c is greatest”

fi

fi

Sample Run

$sh prg2

enter the value of a b c

23

33

44

c is greatest

$sh prg2

enter the value of a b c

23

55

44

b is greatest

$sh prg2

enter the value of a b c

78

33

44

a is greatest

3. Write a shell script to calculate the net salary of an employee in a particular month considering various allowances (TA, DA, HRA) and deductions (INCOME TAX, PROVIDEND FUND) as:

(

a

)

TA=15 percent of basic salary

(

b

)

DA=2 percent of basic salary

(

c

)

HRA=10 percent of basic salary

(

d

)

INCOME TAX=5 percent of salary

(

e

)

PROVIDEND FUND=10 percent of salary

$vi prg3

clear

echo “enter basic salary”

read bs

hra=‘echo $bs \* 10 / 100 | bc’

ta=‘echo $bs \* 15 / 100 | bc’

da=‘echo $bs \* 2 / 100 | bc’

tax=‘echo $bs \* 5 / 100 | bc’

pf=‘echo $bs \* 10 / 100 | bc’

Похожие книги

Книги не найдены