Wednesday, 9 March 2016

Shell Script Programs For Beginners - SET 01

1). Writing your first script and getting it to work .
2). Script which shows a demo of command line arguments.
3). Script to print user login information with current date and time.
4). To read input from user and showing demo of user defined variable.
5). Script to show an example of user defined variable.
6). Script to sum 2 numbers given by user.
7). Script to show sum of numbers with decimal points
8). Script to find the biggest no from the 3 numbers
9). Script to print 1 to 10 numbers using while loop
10). Script to show a demo of for loop

1). Writing your first script and getting it to work .

Step 1 : writing the script
You can write script either on note pad or by using vi editor.Let us use vi editor. Open your Linux terminal and create a file helloWorld.sh for this type "vi helloWorld.sh" and hit enter . Now hit ' i ' to go in insert mode and write the following code in your file.
#!/bin/bash
#
# Script name : helloWorld.sh

echo "Hello world !"
To save helloWorld.sh first hit 'esc' to come out from insert mode and then type ':wq!' and hit enter. Now type 'ls' command to see your file.

Step 2 : Give the execute permission to the file helloWorld.sh ,run the following command
mukesh@ubuntu:~$ chmod 755 helloWorld.sh 
            (or)
mukesh@ubuntu:~$ chmod +x helloWorld.sh 
Step 3 : Run the helloWorld.sh
mukesh@ubuntu:~$ ./helloWorld.sh
Hello world !
             (or)
mukesh@ubuntu:~$ sh helloWorld.sh 
Hello world !
2).Script which shows a demo of command line arguments.
#!/bin/bash
#
#Script Name : commandLineArgument.sh
#Description : The Script will show ,command line argument demo
#
clear
if [ $# -le 0 ]
then
        echo "usage :- $0 arg1 arg2 arg3 ...argN"
        echo "where arg1,arg2...argN are command line argument"
else
        echo "Total no of command line argument are $#"
        echo "$0 is the Script Name"
        echo "$1 is the first argument"
        echo "$2 is the second argument"
        echo "all of them are $* or $@"
fi
3).Script to print user login information with current date and time.
#!/bin/bash
#
#Script name : userLoginDEtail.sh
#Author      : mukesh kumar
#Description : Script to print user information who currently login ,current date & time
#
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry is `pwd`"
echo "==========Another way==========="
clear
echo "Hello $USER"
echo -e "Today is \c ";date
echo -e "Number of user login :\c ";who | wc -l
echo "Calendar"
cal
exit 0 
4).To read input from user and showing demo of user defined variable.
#!/bin/bash
#
#Script Name : readUserInputandUDV.sh
#Description : To read input from user and showing demo of user defined variable.
#
clear
echo  "Please ! Enter your Name :"
read Name
echo "hello $Name ,How are you ?"
myname=Mukesh
myos=linux
echo "My name is $myname and my operating system is $myos"
5).Script to show an example of user defined variable.
#!/bin/bash
#
#Script Name : userDefinedVariable.sh
#Description : Script to demonstrate the UDV.
#
myname=Mukesh
myos=linux
nyno=242100
echo "My Name is $myname"
echo my os is $myos"
echo "My number is myno, can u see this number ?"
6).Script to show sum of two numbers.
#!/bin/bash
#
#Script Name : sumofNumber.sh
#Description : Script to sum of two numbers.
#
clear
if [ $# -ne 2 ]
then
        echo "usage - $0 x y"
        echo "          where x and y are 2 numbers"
        exit 1
else
        echo "sum of $1 and $2 is `expr $1 + $2`"
        exit 0
fi
7). Script to show sum of numbers with decimal points.
#!/bin/bash
#
#Script Name : sumDecimalPoint.sh
#Description : Script to show sum of numbers with decimal points
#

if [ $# -ne 2 ]
then
        echo "Usage : $0 num1 num2 "
        echo "give 2 nos with argument with decimal point,i will sum then"
        exit 1
fi
n1=$1
n2=$2
sum=0
sum=`echo $n1 + $n2 | bc`
echo "sum of $n1 + $n2 = $sum"
8). Script to find the biggest no from the 3 numbers.
#!/bin/bash
#
# Script Name : biggestNumber.sh
# Description : Script to find the biggest no from the 3 numbers
#
if [ $# -ne 3 ]
then
        echo "Usage - $0 n1 n2 n3"
        echo "          where n1 ,n2 and n3 are number among which biggest no has to find"
        exit 1
fi
n1=$1
n2=$2
n3=$3

if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
        echo "$n1 is the biggest number"

elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
        echo "$n2 is the biggest number"

elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
        echo "$n3 is the biggest number"

elif [ $n1 -eq $n2 ] && [ $n1 -eq $n3 ] && [ $n2 -eq $n3 ]
then
        echo "all numbers are equal"
else
        echo "I couldn't figure out which number is gretest"
fi
9). Script to print 1 to 10 numbers using while loop.
#!/bin/bash
#
#Script Name : whileLoop.sh 
#Description : while loop to print 1 to 10 numbers.
#

i=1
while [ $i -le 10 ]
do
        echo "$i"
        i=`expr $i + 1`
done
10). Script to show a demo of for loop.
#!/bin/bash
#
#Script Name : forLoop.sh
#Description : Script showing demo of for loop
#

for val in a b c d e f g h i j
do
        echo $val
done

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...