Saturday, 21 January 2012

Linux Bash Scripting ,Bash Scrip examples


Lab13: Bash Scripting
Lab Tasks:
1. Write a bash script to find out the biggest number from given three numbers. The numbers are supplied as command line argument to the script. Print an error message if sufficient arguments are not supplied.

#!/bin/bash
if(($#!=3));
then
    echo ERROR: Please specify exactly three number as argument;
    exit 1;
else
{
    if [ $1 -gt $2 ]; then
    if [ $2 -gt $3 ]; then
    echo $1 is biggest;
    elif [ $1 -gt $3 ]; then
    echo $1 is biggest;
else
echo $3 is biggest;
fi
else
    if [ $2 -gt $3 ]; then
    echo $2 is biggest;
    else
    echo $3 is biggest;
fi
fi
}
fi


2. Write a bash script to find out the volume of a pizza. Both the radius and height of the pizza should be input by the user and can be real numbers i.e numbers like 3.14259 (Hint: Use read and bc utilities).

#! /bin/bash
echo "Please Enter Height of Pizza"
read height
echo "Please Enter Radius of Pizza"
read radius
result=$(echo 3.14259*$radius*$radius*$height | bc)
echo "Volume of Pizza is =$result"


3. Write a bash script that calculates the factorial of a number given as the argument, using a while or until loop.

#! /bin/bash

if [ $# -ne 1 ]; then
echo 'Only one argument is required'
else
num=$1
fac=1
while [ $num -gt 1 ]
 do
 fac=$(( $fac*$num ))
 num=$(( $num - 1 ))
 done
echo "Factorial is $fac"
fi

4. Write a bash script that checks if the apache and cron daemons are running on your system. If apache/cron is running, the script should print a message like, "Apache/Cron is Alive!!!" (Hint: Use ps to check on processes).
SERVICE='cron'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "Cron is Alive!!!!!!!!!"
else
   echo "$SERVICE is not running"
fi
apachee='apache'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "Apache is Alive!!!!!!!!!"
else
   echo "$SERVICE is not running"
fi



Bonus: Write a script that calculates the factorial of a number given as the argument, using a recursive function.



#!/bin/bash
factorial()
{
 if [ $# -ne 1 ];then
 echo "Error: Only one argument is required "
 exit 1
 fi
 local i=$1
 local f
 declare -i i
 declare -i f
 [ $i -le 2 ] && echo $i || { f=$(( i - 1 ));f=$(factorial $f);f=$(( f * i ));echo $f;}
}
factorial $1

1 comments:

Anonymous said...

Shuaa Digest November 2018 Free Download
Shuaa Digest Download

Read Online Books And Online Results
https://booksresult.com/

Read Online Books Online Library for Urdu Novels Famous Urdu Novels Download PDF Urdu Novels.Board of Intermediate & Secondary Education Results from all Pakistani boards prize bond draw list Prize Bond results.

Read Online Books
Read Online Digest
Read Online Novels
Read Online Magazine

Post a Comment

 
Free Host | lasik surgery new york