## Posts

Showing posts with the label Shell Programming

### Fibonacci Range [Shell Program]

echo "Enter the range" read r a = 0 b = 1 c = 1 echo "Fibonacci series:\n" echo "\$a \$b \c" while [ \$c - lt \$r ] do c = `expr \$a + \$b` if [ \$c - le \$r ] then echo "\$c \c" fi a = \$b b = \$c done echo "\n"

### Fibonacci Series generator [Shell Program]

echo "Enter the Number of Terms" read n a = 0 b = 1 count = 1 n1 = `expr \$n - 2` if [ \$n - eq 1 ] then echo "The Fibonacci series is:" \$a elif [ \$n - eq 2 ] then echo "The Fibonacci series is:" \$a \$b else echo "The Fibonacci series is:" echo "\$a \$b \c" while [ \$count - le \$n1 ] do c = `expr \$a + \$b` echo "\$c \c" a = \$b b = \$c count = `expr \$count + 1` done fi echo "\n"

### Factorial of a Number [Shell Program]

echo "Enter a number" read n f = 1 if [ \$n - eq 0 ] then echo "Factorial is: 1" else while [ \$n - ge 1 ] do f = `expr \$f \* \$n` n = `expr \$n - 1` done echo "Factorial is:" \$f fi

### To the Power [Shell Program]

echo "Enter the Value of x" read x echo "Enter the Value of y" read y r = 1 while [ \$y - gt 0 ] do r = `expr \$x \* \$r` y = `expr \$y - 1` done echo "The value of x^y is:" \$r

### Frequency of a Character in a String [Shell Program]

clear echo "Enter the String:\c" read str echo "Enter the character:\c" read ch n = 0 i = `expr "\$str" : ".*"` while [ \$i - ne 0 ] do  m = `echo \$str|cut -c \$i`   if [ "\$m" = "\$ch" ]; then   n = `expr \$n + 1`   fi  i = `expr \$i - 1` done echo "The frequency is :" \$n

### Display the Calendar [Shell Program]

clear echo " Enter the Year: \c" read year echo " Enter the Month: \c" read month m = 0 case "\$month" in 1 | jan ) m = 1 ;; 2 | feb ) m = 2 ;; 3 | mar ) m = 3 ;; 4 | apr ) m = 4 ;; 5 | may ) m = 5 ;; 6 | jun ) m = 6 ;; 7 | jul ) m = 7 ;; 8 | aug ) m = 8 ;; 9 | sep ) m = 9 ;; 10 | oct ) m = 10 ;; 11 | nov ) m = 11 ;; 12 | dec ) m = 12 ;; *) echo \$month   "is not a valid Month! Showing Calendar for " \$year ;; esac if [ \$m - ne 0 ] then cal \$m \$year else cal \$year fi

### Find a Pattern from a File [Shell Program]

echo "Enter pattern to be searched:\c" read pname echo "Enter file name :\c" read fname echo "Search for \$pname from file \$fname:" grep "\$pname" \$fname

### Resource Test [Shell Program]

echo "Enter the Resource name" read fname if ( test -s \$fname ) then ls -l \$fname if ( test -d \$fname ) then echo "It is a directory" else echo "It is a file" fi if ( test -r \$fname ) then echo "It is a readable" fi if ( test -w \$fname ) then echo "It is writable" fi if ( test -x \$fname ) then echo "It is executable" fi else echo "No such resource present" fi

### Find the greatest among two numbers [Shell Program]

echo "Enter 1st number :\c" read a echo "Enter 2nd number :\c" read b if [ \$a - gt \$b ] then echo "\$a greater than \$b" else echo "\$b less than \$a" fi