Some basic Shell Scripting !

 

To master in Embedded Linux , One has to know about the basics of the shell /bash scripting. The shell scripting helps us to write a driver, patch, kernel update & many more to make a Strong, condensed embedded linux applications. Given below are the some examples of shell scripting to try it out. Comment below for your queries & discussions.

1. Shell script to print given numbers sum of all digit

#!/bin/bash

#

# Linux Shell Scripting

#

# Algo:

#       1) Input number n

#       2) Set sum=0, sd=0

#       3) Find single digit in sd as n % 10 it will give (left most

#          digit)

#       4) Construct sum no as sum=sum+sd

#       5) Decrment n by 1

#       6) Is n is greater than zero, if yes goto step 3, otherwise next step

#       7) Print sum

#

if [ $# -ne 1 ]

then

echo “Usage: $0   number”

echo ”       I will find sum of all digit for given number”

echo ”       For eg. $0 123, I will print 6 as sum of all digit (1+2+3)”

exit 1

fi

n=$1

sum=0

sd=0

while [ $n -gt 0 ]

do

sd=`expr $n % 10`

sum=`expr $sum + $sd`

n=`expr $n / 10`

done

echo  “Sum of digit for number is $sum”

#

===========================================================

$ ./script1.sh 291

Sum of digit for number is 12

===========================================================

 

#!/bin/bash
#
# Linux Shell Scripting
#
# 2. Write script to print contains of file from given line number to
# next given number of lines.
#
# To Execute scriptname <n1> <n2> <filename>
#
# Print error / diagnostic for user if no arg’s given
#
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 sufficent 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

#
#
===========================================================
$ ./script2.sh 3 7 s1.sh
# Linux Shell Scripting
#
# 2. Write script to print contains of file from given line number to
# next given number of lines.
#
# To Execute scriptname <n1> <n2> <filename>
#
===========================================================

 

3. Shell script to say Good morning/Afternoon/Evening as you log in

#!/bin/bash
#
# Linux Shell Scripting
#
# to system
#
#

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 “\
–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
#
===========================================================
$ ./script3.sh

U will see screen with a message.
===========================================================
4. Shell script to find whether entered year is Leap or not

#!/bin/bash
#
# Shell Scripting
#
#
year=0

echo -n “Enter the year,I will tell you whether its Leap year or Not? ”
read year

ans=$(($year % 4)) # or try ans=`expr $year % 2`

if [ $ans -eq 0 ]; then
echo “$year is Leap Year”
else
echo “$year is NOT Leap Year”
fi
#
===========================================================
$ ./script4.sh
Enter the year,I will tell you whether its Leap year or Not? 2000
2000 is Leap Year

$./script4.sh
Enter the year,I will tell you whether its Leap year or Not? 2002
2001 is NOT Leap Year
===========================================================
5. Sort the given five number in ascending order

#!/bin/bash
#
# Linux Shell Scripting Tutorial 1.05r3, Summer-2002
#
#
# Declare the array of 5 subscripts to hold 5 numbers
#
declare nos[5]=(4 -1 2 66 10)

#
# Prints the number befor sorting
#
echo “Original Numbers in array:”
for (( i = 0; i <= 4; i++ ))
do
echo ${nos[$i]}
done

#
# Now do the Sorting of numbers
#

for (( i = 0; i <= 4 ; i++ ))
do
for (( j = $i; j <= 4; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done

#
# Print the sorted number
#
echo -e “\nSorted Numbers in Ascending Order:”
for (( i=0; i <= 4; i++ ))
do
echo ${nos[$i]}
done

#
#

==========================================================
$ ./script5.sh
Original Numbers in array:
4 -1 2 66 10
Sorted Numbers in Ascending Order:
-1 2 4 10 66
===========================================================

 

6. Script to sum to nos

#!/bin/bash
#
# Linux Shell Scripting
#
#
#

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`”
#
===========================================================
$ ./script6.sh 12 4
Sum of 12 and 4 is 16
===========================================================

 

7. Calculating average of given numbers on command line args

#!/bin/bash
#
# Linux Shell Scripting
#
#
#

avg=0
temp_total=0
number_of_args=$#

#
# First see the sufficent cmd args
#
if [ $# -lt 2 ] ; then
echo -e “Opps! I need atleast 2 command line args\n”
echo -e “Syntax: $0: number1 number2 … numberN\n”
echo -e “Example:$0 5 4\n\t$0 56 66 34”
exit 1
fi

#
# now calculate the average of numbers given on command line as cmd args
#

for i in $*
do

# addition of all the numbers on cmd args
temp_total=`expr $temp_total + $i`

done

avg=`expr $temp_total / $number_of_args`
echo “Average of all number is $avg”
#
#

===========================================================
$ ./script7.sh 3 5 9 23
Average of all number is 10
===========================================================
8. Script to find out bigest number

#!/bin/bash
#
# Linux Shell Scripting
#
#
#
# Algo:
# 1) START: Take three nos as n1,n2,n3.
# 2) Is n1 is greater than n2 and n3, if yes
# print n1 is bigest no goto step 5, otherwise goto next step
# 3) Is n2 is greater than n1 and n3, if yes
# print n2 is bigest no goto step 5, otherwise goto next step
# 4) Is n3 is greater than n1 and n2, if yes
# print n3 is bigest no goto step 5, otherwise goto next step
# 5) END
#
#

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 Bigest number”
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo “$n2 is Bigest number”
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo “$n3 is Bigest 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 biger”
fi

#
#

===========================================================
$ ./script8.sh 2 8 0
8 is Bigest number
===========================================================
9. Shell script to implement getopts statement.

#!/bin/bash
#
# Linux Shell Scripting
#
#
#
# -c clear
# -d dir
# -m mc
# -e vi { editor }
#

#
# Function to clear the screen
#
cls()
{
clear
echo “Clear screen, press a key . . .”
read
return
}

#
# Function to show files in current directory
#
show_ls()
{
ls
echo “list files, press a key . . .”
read
return
}

#
# Function to start mc
#
start_mc()
{
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
}

#
# Function to start editor
#
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 -v {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 sufficent 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
# ===========================================================
# ./script9.sh –c
# ./script9.sh -d
# ./script9.sh -e vi
#===========================================================
10. Basic math Calculator

#!/bin/bash
#
# Linux Shell Scripting
#
#
#
#

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 invalied 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
#
#
#

===========================================================
$ ./script10.sh 6 + 3
Answer is 9
$ ./s1.sh 2 x 3
Answer is 6
===========================================================

 

 

Leave a comment

Your email address will not be published. Required fields are marked *