0% found this document useful (0 votes)
162 views6 pages

Shell Programming Examples

Unix shell programming with their output. Fibonacci series,prime number check,reverse a number,various pattern printing.

Uploaded by

osmansio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
162 views6 pages

Shell Programming Examples

Unix shell programming with their output. Fibonacci series,prime number check,reverse a number,various pattern printing.

Uploaded by

osmansio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

1. Write a shell program to generate Fibonacci series up to n terms. dsms@ubuntu:~$ gedit fibo.

sh echo "Enter the range:" read n f1=0 f2=1 echo " The Fibonacci Series is:" printf "$f1 " printf "$f2 " i=2 while [ $i -le $n ] do f3=`expr $f1 + $f2` f1=$f2 f2=$f3 printf "$f3 " i=`expr $i + 1` done

Output: dsms@ubuntu:~$ ./fibo.sh Enter the range: 6 The Fibonacci Series is: 0 1 1 2 3 5 8

2. Write a shell program to check whether a no. is prime or not. dsms@ubuntu:~$ gedit prime.sh echo "Enter a number:" read num i=2 while [ $i -lt $num ] do r=`expr $num % $i` if [ $r -eq 0 ] then echo "The number is not prime" exit fi i=`expr $i + 1` done echo "The number is prime" Output: dsms@ubuntu:~$ ./prime.sh Enter a number: 7 The number is prime dsms@ubuntu:~$ ./prime.sh Enter a number: 9 The number is not prime

Page | 1

3. Write a shell program to print 1,50,2,48,3,47,4,46. dsms@ubuntu:~$ gedit ans3.sh a=1 b=50 echo "Enter the range:" read n printf "$a " printf "$b " for((i=2;i<=n;i++)) do printf "$i " r=`expr $b - $i` printf "$r " done

Output: dsms@ubuntu:~$ ./ans3.sh Enter the range: 6 1 50 2 48 3 47 4 46 5 45 6 44

4. Write a shell program to print 1,50,2,48,3,45,4,41. dsms@ubuntu:~$ gedit ans4.sh a=51 printf "Enter the range: " read n for((i=1;i<=n;i++)) do printf "$i " a=`expr $a - $i` printf "$a " done 5. Write a shell program to print 1,9,25,49. dsms@ubuntu:~$ gedit ans5.sh printf "Enter the range: " read n x=1 while [ $n -gt 0 ] do a=`expr $x \* $x` printf "$a " x=`expr $x + 2` n=`expr $n - 1` done

Output: dsms@ubuntu:~$ ./ans4.sh Enter the range: 6 1 50 2 48 3 45 4 41 5 36 6 30

Output: dsms@ubuntu:~$ ./ans5.sh Enter the range: 5 1 9 25 49 81

Page | 2

6. Write a shell program to reverse a number. dsms@ubuntu:~$ gedit ans6.sh printf "Enter a number: " read n a=0 while [ $n -gt 0 ] do b=`expr $n % 10` a=`expr $a \* 10 + $b` n=`expr $n / 10` done echo "Reverse number is: $a" 7. Write a shell program to print the following pattern a. 1 2 3 4 5 7 8 Output: dsms@ubuntu:~$ ./ans5.sh Enter the range: 5 1 9 25 49 81

6 9 10 Output: dsms@ubuntu:~$ ./ans7a.sh Enter the line no: 5 1 23 456 7 8 9 10 11 12 13 14 15

dsms@ubuntu:~$ gedit ans7a.sh printf "Enter the line no:" read n x=1 for((i=1;i<=n;i++)) do for((j=1;j<=i;j++)) do echo -n "$x " x=`expr $x + 1` done echo " " done b. 1 0 0 1

1 1 0

0 1

dsms@ubuntu:~$ gedit ans7b.sh printf "Enter the line no.: " read n x=1 Page | 3

for((i=1;i<=n;i++)) do for((j=1;j<=i;j++)) do echo -n "$x " if [ $x -eq 1 ] then x=0 else x=1 fi done echo " " done c. 1 2 1 1 2

Output: dsms@ubuntu:~$ ./ans7b.sh Enter the line no: 5 1 01 010 1010 10101

3 2 2 3

3 3 1

1 2 3

dsms@ubuntu:~$ gedit ans7c.sh printf "Enter the line no.:" read n x=1 for((i=1;i<=n;i++)) do for((j=1;j<=i;j++)) do echo -n "$x " x=`expr $x + 1` if [ $x -eq 4 ] then x=1 fi done echo " " done

Output: dsms@ubuntu:~$ ./ans7c.sh Enter the line no: 5 1 23 123 1231 23123

Page | 4

* * * * * * * * * dsms@ubuntu:~$

d.

* * * * * * gedit ans7d.sh

printf "Enter the line no.: " read n for((i=1;i<=n;i++)) do for((j=1;j<=i;j++)) do echo -n " *" done echo " " done

Output: dsms@ubuntu:~$ ./ans7d.sh Enter the line no: 5 * ** *** **** *****

e.

* * * * *

* * * * * * * * * *

dsms@ubuntu:~$ gedit ans7e.sh printf "Enter the line no.:" read n for((i=n;i>=1;i--)) do for((j=1;j<=i;j++)) do echo -n " *" done echo " " done

Output: dsms@ubuntu:~$ ./ans7e.sh Enter the line no: 5 ***** **** *** ** *

8. Write at least 5 commands which can be executed by only system administrator. a. Du b. df c. Poweroff d. Login e. ifconfig Page | 5

9. Write an awk program to find the pattern engineer, having location=Kolkata or Durgapur. Show the employee details file.

dsms@ubuntu:~$ cat aa 1234|jayanta|professor|durgapur|38000 3456|tanmoy|engineer|kolkata|40000 4356|amit|programmer|durgapur|45000 2365|dsms|lecturer|mumbai|34000 5643|arvind|engineer|chennai|42000| 5387|suvojit|programmer|kolkata|56000 Ctrl+d

dsms@ubuntu:~$ awk -F"|" '$3 == "engineer" && $4 == "kolkata" || $4 == "durgapur" {print}' aa 1234|jayanta|professor|durgapur|38000 3456|tanmoy|engineer|kolkata|40000 4356|amit|programmer|durgapur|45000 10. Write an awk program to find the pattern programmer, having salary >20000, display their name, location, project name. Show the employee details file. dsms@ubuntu:~$ cat aa 1234|jayanta|professor|durgapur|38000 3456|tanmoy|engineer|kolkata|40000 4356|amit|programmer|durgapur|45000 2365|dsms|lecturer|mumbai|34000 5643|arvind|engineer|chennai|42000| 5387|suvojit|programmer|kolkata|56000 Ctrl+d

dsms@ubuntu:~$ dsms@ubuntu:~$ awk -F"|" '$3 == "programmer" && $5>20000 {print}' aa 4356|amit|programmer|durgapur|45000 5387|suvojit|programmer|kolkata|56000

Page | 6

You might also like