SHELL SCRIPTING

RECEIVE INPUT FROM USER

echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"

ADD TWO

if [ $# -ne 2 ]
then
    echo "Usage - $0   x    y"
    echo "        Where x and y are two nos for which I will print sum"
    exit 1
fi
    echo "Sum of $1 and $2 is `expr $1 + $2`"

 FIND BIGGEST NUMBERS

    if [ $# -ne 3 ]
    then
    echo "$0: number1 number2 number3 are not given" >&2
        exit 1  
    fi
    n1=$1
    n2=$2
    n3=$3
    if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
    then
    echo "$n1 is Biggest number"
    elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]       
    then
    echo "$n2 is Biggest number"
    elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]       
    then
        echo "$n3 is Biggest number"
    elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
    then
    echo "All the three numbers are equal"  
    else
        echo "I can not figure out which number is bigger"  
    fi

 CASE STATEMENT

 if test $# = 3
then
    case $2 in
     +) let z=$1+$3;;
     -) let z=$1-$3;;
     /) let z=$1/$3;;
     x|X) let z=$1*$3;;
     *) echo Warning - $2 invalid operator, only +,-,x,/ operator allowed
        exit;;
    esac
    echo Answer is $z
else
    echo "Usage - $0   value1  operator value2"
    echo "        Where, value1 and value2 are numeric values"
    echo "               operator can be +,-,/,x (For Multiplication)"
fi

PRINT REVERSE

i=5
while test $i != 0
do
    echo "$i
"
    i=`expr $i - 1`
done

 CHECK YOURSELF 

echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry `pwd`"

PRINT NUMBERS IN REVERSE ORDER

if [ $# -ne 1 ]
then
    echo "Usage: $0   number"
    echo "       I will find reverse of given number"
    echo "       For eg. $0 123, I will print 321"
    exit 1
fi

n=$1
rev=0
sd=0

while [ $n -gt 0 ]
do
    sd=`expr $n % 10`
    rev=`expr $rev \* 10  + $sd`
    echo  "Reverse number is $rev"
    n=`expr $n / 10`
done

THIRD VARIABLE


a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"

FILE EXISTS CWD

if [ $# -ne 1 ]
then
    echo "Usage - $0 file-name"
    exit 1
fi
if [ -f $1 ]
then
    echo "$1 file exists"
else
    echo "Sorry, $1 file does not exist"
fi

GREP LINES FROM TEXT FILE

if [ $# -eq 0 ]
then
    echo "$0:Error command arguments missing!"
    echo "Usage: $0 start_line   uptoline   filename"
    echo "Where start_line is line number from which you would like to print file"
    echo "uptoline is line number upto which would like to print"
    echo "For eg. $0 5 5 myfile"
    echo "Here from myfile total 5 lines printed starting from line no. 5 to"
    echo "line no 10."
    exit 1
fi

#
# Look for sufficient arg's
#

    if [ $# -eq 3 ]; then
    if [ -e $3 ]; then
            tail +$1 $3 | head -n$2
         else
            echo "$0: Error opening file $3"
        exit 2
    fi
    else
        echo "Missing arguments!"
    fi

GETOPTS From INPUT'S

cls()
{
    clear
    echo "Clear screen, press a key..."
    read
    return
}
show_ls()
{
    if which mc ? /dev/null ; then
    mc
    echo "Midnight commander, Press a key ..."
    read
    else
    echo "Error: Midnight commander not installed, Press a key ..."
    read
    fi
    return
}

start_ed()
{
    ced = $1
    if which $ced ? /dev/null ; then
    $ced
    echo "$ced, Press a key ..."
    read
    else
    echo "Error: $ced is not installed or no such editor exist, Press a key ..."
    read
    fi
    return
}

#
# Function to print help
#

print_help_uu()
{
    echo "Usage: $0 -c -d -m -e {editor name}";
    echo "Where -c clear the screen";
    echo "  -d show dir";
    echo "  -m start midnight commander shell";
    echo "  -e {editor}, start {editor} of your choice";
    return
}

#
# Main procedure start here
#
# Check for sufficient args
#

if [ $# -eq 0 ] ; then
    print_help_uu
    exit 1
fi

#
# now parse command line arguments
#

while getopts cdme: opt
do
    case "$opt" in
    c) cls;;
    d) show_ls;;
    m) start_mc;;
    e) thised = "$OPTARG"; start_Ed $thised;;
    \?) print_help_uu; exit 1;;
    esac
done

TIME DAY DATE

temph=`date | cut -c12-13`
dat=`date +"%A %d in %B of %Y (%r)"`

if [ $temph -lt 12 ]
then
    mess="Good Morning $LOGNAME, Have nice day!"
fi

if [ $temph -gt 12 -a $temph -le 16 ]
then
    mess="Good Afternoon $LOGNAME"
fi

if [ $temph -gt 16 -a $temph -le 18 ]
then
    mess="Good Evening $LOGNAME"
fi

if which dialog > /dev/null
then
    dialog --backtitle "Linux Shell Script Tutorial"\
    --title "(-: Welcome to Linux :-)"\
    --infobox "\n$mess\nThis is $dat" 6 60
    echo -n "                            Press a key to continue. . .                        "
    read
    clear
else
    echo -e "$mess\nThis is $dat"
fi

HIGHLIGHTS TEXT

clear
echo -e "\033[1m Hello World"
 # bold effect
echo -e "\033[5m Blink"
       # blink effect
echo -e "\033[0m Hello World"
 # back to normal

echo -e "\033[31m Hello World"
 # Red color
echo -e "\033[32m Hello World"
 # Green color
echo -e "\033[33m Hello World"
 # See remaining on screen
echo -e "\033[34m Hello World"
echo -e "\033[35m Hello World"
echo -e "\033[36m Hello World"

echo -e -n "\033[0m "
  # back to normal

echo -e "\033[41m Hello World"
echo -e "\033[42m Hello World"
echo -e "\033[43m Hello World"
echo -e "\033[44m Hello World"
echo -e "\033[45m Hello World"
echo -e "\033[46m Hello World"
echo -e "\033[0m Hello World"
  # back to normal

 DIGITAL CLOCK

echo
echo "Digital Clock for Linux"
echo "To stop this clock use command kill pid, see above for pid"
echo "Press a key to continue. . ."
while :
do
ti=`date +"%r"`
echo -e -n "\033[7s" #save current screen position & attributes
#
# Show the clock
#
tput cup 0 69 # row 0 and column 69 is used to show clock
echo -n $ti # put clock on screen
echo -e -n "\033[8u" #restore current screen position & attributes
#
#Delay fro 1 second
#
sleep 1
done

Comments