[go: up one dir, main page]

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

Shell Prog1

The document contains several shell script programs with sample inputs and outputs. The programs perform tasks like displaying environment variables, getting user input, performing calculations like area of shapes and conversions, running commands and checking outputs, finding sums and maximums of numbers, calculating factorials, and checking if a number is prime or Armstrong.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views14 pages

Shell Prog1

The document contains several shell script programs with sample inputs and outputs. The programs perform tasks like displaying environment variables, getting user input, performing calculations like area of shapes and conversions, running commands and checking outputs, finding sums and maximums of numbers, calculating factorials, and checking if a number is prime or Armstrong.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

V.

chandrasekharan Mech 024

Program: To display the shell environment variables


#shell script #!/bin/sh echo the user name is $USER echo the log name is $LOGNAME echo the present working directory is $PWD echo the os type is $TERM_PROGRAM

sample input and output:


the user name is mech14 the log name is mech14 the present working directory is /user/mech14 the os type is Apple_terminal

V.chandrasekharan Mech 024 Program: to display the name and time in the given format #shell script
#!/bin/sh echo enter your name read x echo welcome $x echo now the time is [`date +%H:%M:%S`]

sample input and output: enter your name shyam Welcome shyam Now the time is [12:00:18]

V.chandrasekharan Mech 024 Program: to find the area of circle and rectangle #shell script #!/bin/sh echo enter the radius read x c =`echo 22/7 \* $x \* $x |bc l` echo the area of circle is $c echo enter the length read l echo enter the breadth read b r =`echo $l \* $b |bc -l` echo the area of rectangle is $r sample input and output: enter the radius 5 The area of circle is 78.50 Enter the length 4 Enter the breadth 3 The area of rectangle is 12

V.chandrasekharan Mech 024 Program: to write a program to convert Fahrenheit to Celsius #shell script #!/bin/sh echo enter the Fahrenheit value read f p=`echo $f - 32 | bc -l`

cel=`echo $p / 1.8 | bc -l` echo the Celsius value is $cel sample input and output: enter the Fahrenheit value 212 Celsius value is 100.00000

V.chandrasekharan Mech 024 Program: to write a shell program to display the output of given commands #shell script #!/bin/sh echo the list of directories available echo `ls` echo list of login users echo `who` echo todays date echo `date` echo count of *.sh files echo `ls *.sh |wc` sample input and output: the list of directories of directories available -b desktop documents library movies music pictures public college

f1.txt the current working directory /users/mech14 The list of login users Mech14 Mech14 console ttyp1 jan 1 jan 1 05:30

05:30

todays date is thu jan 1 05:30:26 IST 1970 count of *.sh files 0

V.chandrasekharan Mech 024 Program: to find the sum of n numbers #shell script #!/bin/sh echo enter the number of terms read n s=0 i=1 if [ $N ] while [ $i -le $N ] do s=`expr $s + $i ` i=`expr $i +1` done fi echo the sum is $s sample input and output: enter the number of terms 5 The sum is 15

V.chandrasekharan Mech 024 Program: to find maximum of three numbers #shell script #!/bin/sh echo enter three numbers read a read b read c if [ $a -gt $b -a $a -gt $c ] then echo $a is big elif [ $b -gt $a -a $b -gt $c ] then echo $b is big else echo $c is big fi sample input and output: 1)enter three numbers 10 25 45 45 is big

2)enter three numbers 10 4 3 10 is big 3)enter three numbers 3 8 4 8 is big

V.chandrasekharan Mech 024 Program: to find the factorial of given number #shell script #!/bin/sh Echo enter the number Read n s=1 i=n while [ $i -ge 1 ] do s=`expr $i \* $f` i=`expr $i - 1` done echo the factorial of $n is $s sample input and output: enter the number 5 The factorial of 5 is 120

V.chandrasekharan Mech 024 Program: to check whether the given number is prime or not #shell script #!/bin/sh echo enter a number read n b=0 for((i=2;i<$n;i++)) do c=`expr $n % $i` if [ $c -eq 0 ] then b =1 fi done if [ b -eq 0 ] then echo $n is prime else echo $n is not prime fi

sample input and output:

1)enter a number 3 3 is prime 2)enter a number 4 4 is not prime

V.chandrasekharan Mech 024 Program: to check if the given number is Armstrong or not #shell script #!/bin/sh echo enter the number read n a=$n sum=0 while [ $n -ne 0 ] do r=`expr $n %10` s=`expr $r \*r \*r` sum=`expr $sum +$s` n=`expr $n /10` done if [ $a -eq $s ] then echo the number is Armstrong else echo the given number is not Armstrong fi

sample input and output:

enter the number 153 the number is Armstrong

enter the number 123 the number is not Armstrong

You might also like