Saturday, January 12, 2013

Some shell script examples

#!/bin/sh
# Kiran Sharma
#
echo "Hello, World !!!"


#!/bin/sh
# Kiran Sharma
#
echo "Hi there"
echo "what is your name? (Enter your name and press Enter)"
read name
echo "Hello $name"


#!/bin/sh
# Kiran Sharma
# This program will calculate the mathmetic operation
echo "Please enter the first value?"
read fvalue
echo "Please enter the 2nd value?"
read svalue
echo "fvalue*svalue = $fvalue*$svalue = $[fvalue*svalue]"
echo "fvalue+svalue = $fvalue+$svalue = $[fvalue+svalue]"

$ echo $[2*2*2]
$ echo $[2*3+4*2+7]


#!/bin/sh
# Kiran Sharma
# This program is a while loop example


echo "The cycle is going through the loop"
x=1
while test "$x" -le "10"
do
#       echo "The cycle is going through the loop"
        echo "$x round"
        x=$[x+1]
done

Note: You can use the same script to test the untile by changing the value of test "$x" -le "10" to test

"$x" -gt "10".

#!/bin/sh
# Kiran Sharma
# This program is a until loop example

echo "The cycle is going through until loop"
x=1
until test "$x" -gt "10"
do
    echo "$x cycle"
    x=$[x+1]
done

#!/bin/sh
# Kiran Sharma
# Compare two numeric values
x=5
y=6
if test "$x" -gt "$y"
then
    echo "$x is greater than $y"
else
    echo "$x is less than $y"
fi

#!/bin/sh
# Kiran Sharma
# Compare two numeric values
x=5
y=6
if test "$x" -gt "$y"
then
        echo "$x is greater than $y"
elif test "$x" -eq "$y"
then
        echo "Passed values are equal"
else
        echo "$x is less than $y"
fi


#!/bin/sh
# Kiran Sharma
# Check positional parameter

if test "$1" = "" ; then
        echo "Usage: script_name.sh <value>"
        exit


#!/bin/sh
# Kiran Sharma
# for loop example
for i in *.txt ; do
        echo "found a file:" $i
done


$ /sbin/ifconfig ${1:-eth0} | awk '/inet addr/ {print $2}' | awk -F: '{print $2}';
$ /sbin/ifconfig ${a-eth0} | awk '/inet addr/ {print $2}'| awk -F: '{ print $2 }';

#!/bin/sh
# Kiran Sharma
#
$ function getip()
{
/sbin/ifconfig ${1:-eth0} | awk '/inet addr/ {print $2}' | awk -F: '{print $2}';
}

$ getip


touch -- -stupid_file_name
rm -- -stupid_file_name


The case Statement

#!/bin/sh
# Kiran Sharma
#
case $1 in
        --test|-t)
                echo "you used the --test option"
                exit 0
        ;;
        --help|-h)
                echo "Usage:"
                echo "        myprog.sh [--test|--help|--version]"
                exit 0
        ;;
        --version|-v)
                echo "myprog.sh version 0.0.1"
                exit 0
        ;;
        -*)
                echo "No such option $1"
                echo "Usage:"
                echo "        myprog.sh [--test|--help|--version]"
                exit 1
        ;;
esac

echo "You typed \"$1\" on the command-line"

#!/bin/sh
# Kiran Sharma
#
while test "$1" != "" ; do
        echo $1
        shift
done

$ sh mywhsh.sh 3 4 5 6 7

#!/bin/sh
# Kiran Sharma
#
a=`cat myfile`
echo $a

a=`expr 50 + 10 '*' 2`
echo $a

or

$ a=$[50+10*2]
echo $a

No comments:

Post a Comment