Saturday, August 25, 2007

basic linux shell scripts

basic linux shell scripts - basic examples
================================


# Ques. No : 1a

# Script to find the largest of 3 numbers passed from the command line. The
# program displays an error (exit status=2) if exactly 3 numbers are not
# provided.


#!/bin/sh

if [ $# -ne 3 ]
then
echo "ERROR : Exactly 3 parameters are required !"
exit 2
else
max=$1

if [ $2 -gt $max ]
then
max=$2
fi

if [ $3 -gt $max ]
then
max=$3
fi
fi

echo "The largest number is $max"
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 1b

# Sctipt to copy file(s) with confirmation

#!/bin/sh

echo -e "Enter source filename(s) : \c"
read source

echo -e "Enter target filename (directory) : \c"
read target

cp -i $source $target

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 2a

# Script that accepts 5 command line arguments and prints them in reverse order

#!/bin/sh

if [ $# -ne 5 ]
then
echo "ERROR : Exactly 5 parameters are required !"
exit 1
else
echo "$5 $4 $3 $2 $1"
exit 0
fi

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 2b

# Script to delete file(s) with confirmation

#!/bin/sh

echo -e "Enter filename(s) to be deleted : \c"
read filenames

rm -i $filenames

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 3a

# Script to display contents of 3 files one after the other

#!/bin/sh

cat names1 names2 names3

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 3b

# Shell Script to move file(s) to selected destination with confirmation

#!/bin/sh

echo -e "Enter filename(s) to be moved : \c"
read source

echo -e "Enter destination : \c"
read destination

mv -i $source $destination

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 4a

# Script to accept 3 numbers from the user during execution and determine
# largest of the 3 numbers. If all the 3 numbers are equal an appropriate
# message is displayed
.

#!/bin/sh

echo -e "Enter Number 1 : \c"
read no1

echo -e "Enter Number 2 : \c"
read no2

echo -e "Enter Number 3 : \c"
read no3

max=$no1

if [ $no2 -gt $max ]
then
max=$no2
fi

if [ $no3 -gt $max ]
then
max=$no3
fi

if [ $no1 -eq $no2 -a $no2 -eq $no3 ]
then
echo "All the 3 numbers are equal"
else
echo "The largest among the three numbers is $max"
fi


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 4b

# Script to display calendar of the current month. Current month is decided
# from the system date.


#!/bin/sh

cal `date +%h`

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 5a

# Script to display contents of 3 files sorted in descending orders.

#!/bin/sh

cat names1 names2 names3 sort -r

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 5b

# Script to display all files in the directory in an ascending and columnar
# fashion.


#!/bin/sh

ls -x

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 6a

# Script that prints "Invalid no. of arguments" if more then 5 command line
# arguments are passed to it. Otherwise it prints "Valid no. of arguments"


#!/bin/sh

if [ $# -eq 5 ]
then
echo "Valid no. of arguments"
else
echo "Invalid no. of arguments"
fi

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 6b

# Script to display contents of .profile along with the number of lines in the
# file.


cd
cat .profile
echo
echo "The number of lines in .profile is " `cat .profile wc -l`

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 7a

# Script to display first n lines of 3 files, n being passed as a command
# line parameter.

#!/bin/sh

if [ $# -ne 1 ]
then
echo "ERROR : Invalid number of parameters !"
exit 1
else
head -$1 -q names1 names2 names3
exit 0
fi

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 7b

# Script to display home directory of user and the primary system prompt.

#!/bin/sh

echo "Home Directory : $HOME"
echo "Primary System Prompt : $PS1"

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 8a

# Script that displays date, time, username and current directory each in a
# separate line. Date is in MM/DD/YY format.


#!/bin/sh

echo "Date : " `date +"%m/%d/%y"`
echo "Time : " `date +"%H:%M:%S"`
echo "Username : $LOGNAME"
echo "Current Directory : " `pwd`

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 8b

# Script to display all the files in the directory in an ascending and
# columnar fashion.

#!/bin/sh

ls -xr

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 9b

# Script the displays the number of users logged on.

#!/bin/sh

echo "There are " `who wc -l` "users logged into the system."

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 10b

# Script the displays the number of files in the home directory of the user

#!/bin/sh

cd
echo "Number of files in your home directory : " `ls wc -w`

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 11a

# Script to display contents of 3 files sorted in ascending orders.

#!/bin/sh

cat names1 names2 names3 sort

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 11b

# Script to print a banner of the user's login names.

set `who am i`
banner $1

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 12a

# Script the calculates the simple interest for which the principal,
# time period and rate of interest are passes as command line arguments.


#!/bin/sh

if [ $# -lt 3 ]
then
echo "ERROR : Insufficient number of parameters !"
exit 1
elif [ $# -gt 3 ]
then
echo "ERROR : Excess parameters !"
exit 2
else
interest=`expr $1 \* $2 \* $3 / 100`
echo "Principal : Rs. $1"
echo "Time Period : $2 Years"
echo "Rate of Interest : $3 %"
echo "Simple Interest : Rs. $interest"
exit 0
fi

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 13a

# Script to serach for a word in 3 files and determine the line number
# in which the word is present.


#!/bin/sh

echo -e "Enter Search Keyword : \c"
read keyword

cat names1 names2 names3 grep "$keyword" > /dev/null

if [ $? -eq 0 ]
then
set `cat names1 names2 names3 grep -n "$keyword" cut -d ":" -f1`
echo "Keyword found on line $1"
exit 0
else
echo "Keyword not found !"
exit 1
fi


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 14a

# Script to list users currently logged on to the system and pipe the
# output through grep to search for the given username.


#!/bin/sh

set `who`
no_of_users=`who wc -l`
userlist=""

echo -e "Enter username : \c"
read username

while [ $no_of_users -gt 0 ]
do
userlist="$userlist $1"
no_of_users=`expr $no_of_users - 1`
shift 5
done

echo "User(s) currently logged on : $userlist"

echo $userlist grep $username > /dev/null

if [ $? -eq 0 ]
then
echo "$username is logged in."
exit 0
else
echo "$username is not logged in."
exit 1
fi


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 15a

# Script to find factorial of the number passed as a command line argument

if [ $# -ne 1 ]
then
echo "ERROR : Invalid number of parameters !"
exit 1
else
no=$1
fact=1

while [ $no -gt 1 ]
do
fact=`expr $fact \* $no`
no=`expr $no - 1`
done

echo "Factorial of $1 is $fact"
exit 0
fi


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 16a

# Script that checks whether the username given as argument is logged on.
# If the user is logged on then the script checks every 10 seconds
# and displays "Logged out" when the user logs out.


if [ $# -ne 1 ]
then
echo "ERROR : Exactly one user has to be specified !"
exit 1
else
username=$1
who grep $username > /dev/null

if [ $? -ne 0 ]
then
echo "User has not logged in."
exit 2
else
echo "$username is logged in."

while true
do
who grep $username > /dev/null

if [ $? -ne 0 ]
then
echo "$username has logged out."
exit 0
else
sleep 3
fi
done
fi
fi


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 17a

# Script to find the largest of 3 numbers passed from the command line. The
# program displays an error (exit status=2) if exactly 3 numbers are not
# provided. The average and total of the 3 numbers is also displayed.


#!/bin/sh

total=0
avg=0

if [ $# -ne 3 ]
then
echo "ERROR : Exactly 3 parameters are required !"
exit 2
else
max=$1

if [ $2 -gt $max ]
then
max=$2
fi

if [ $3 -gt $max ]
then
max=$3
fi
fi

total=`expr $1 + $2 + $3`
avg=`expr $total / 3`

echo "Largest Number : $max"
echo "Total : $total"
echo "Average : $avg"
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 18a

# Script that greets the user when he/she logs in and displays the system date.
# This script has to be added to the .profile file so that it is executed
# whenever the user logs in.


#!/bin/sh

echo "Hello $LOGNAME"
echo "Date : " `date +"%m/%d/%y"`

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 19a

# Script to display multiplication table of number passed from command line.

#!/bin/sh

if [ $# -ne 1 ]
then
echo "ERROR : Insufficient/Excess parameters."
exit 1
else

no=1
prod=0

while [ $no -le 10 ]
do
prod=`expr $1 \* $no`
echo "$1 X $no = $prod"
no=`expr $no + 1`
done
fi
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 20a

# Script to check whether the number passed from the command line is even or odd

#!/bin/sh

if [ $# -ne 1 ]
then
echo "ERROR : Insufficient/Excess parameters"
exit 1
else
rem=`expr $1 % 2`

if [ $rem -eq 0 ]
then
echo "$1 is even"
else
echo "$1 is odd"
fi
fi
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 21a

# Script to display the date and print appropriate message...

#!/bin/sh

echo "Date : " `date +"%m/%d/%y"`

if [ `date +%H` -lt 12 ]
then
echo "Good Morning !"
elif [ `date +%H` -ge 12 -a `date +%H` -le 16 ]
then
echo "Good Afternoon !"
else
echo "Good Evening !"
fi

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 22a

# Script that accepts a number and finds the sum of 1+2+3+...+number

#!/bin/sh

echo -e "Enter a number : \c"
read n
number=$n

sum=0

while [ $n -gt 0 ]
do
sum=`expr $sum + $n`
n=`expr $n - 1`
done

echo "The sum of first $number numbers is $sum"
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 23a

# Accept marks in 3 subjects, calculate the average and display appropriate
# remark.
#!/bin/sh
average=0

echo -e "Enter marks in maths : \c"
read maths
echo -e "Enter marks in science : \c"
read science
echo -e "Enter marks in english : \c"
read english

average=`expr $maths + $science + $english`
average=`expr $average / 3`

echo "Average is $average"

if [ $average -gt 60 ]
then
echo "Grade : First Class"
elif [ $average -gt 50 ]
then
echo "Grade : Second Class"
elif [ $average -gt 40 ]
then
echo "Grade : Third Class"
else
echo "Grade : Fail"
fi

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 24a

# Script that accepts a number and finds the sum of 2+4+6+...+number

#!/bin/sh

echo -e "Enter a number : \c"
read n
number=$n

sum=0
no=0

while [ $no -le $n ]
do
sum=`expr $sum + $no`
no=`expr $no + 2`
done

echo "The sum is $sum"
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 25a1

# Script that prints today's date in MM/DD/YY format

#!/bin/sh

echo "Today is " `date +"%m/%d/%y"`

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 25a2

# Script to display count of users logged in.

#!/bin/sh

number=`who wc -l`

echo "Number of users logged in : $number"


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 25a3

# Display personal stats such as LOGNAME, PORTNUMBER, DATE and LOGIN TIME

#!/bin/sh

set `who am i`

echo "Login Name : $LOGNAME"
shift 1
echo "Port Number : $1"
shift 1
echo "Date : $1 $2"
shift 2
echo "Time : $1"


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 26a

# Script to find average of n numbers where n is accepted from the user

#!/bin/sh

echo -e "Enter n : \c"
read n

avg=0
c=1
no=0
sum=0

while [ $c -le $n ]
do
echo -e "Number $c : \c"
read no
sum=`expr $sum + $no`
c=`expr $c + 1`
done

avg=`expr $sum / $n`
echo "Average is $avg"


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 27a

# Script that accepts a number and finds the sum of 1+3+5+...+number

#!/bin/sh

echo -e "Enter a number : \c"
read n
number=$n

sum=0
no=1

while [ $no -le $n ]
do
sum=`expr $sum + $no`
no=`expr $no + 2`
done

echo "The sum is $sum"
exit 0

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 28a

# Menu Program...


#!/bin/sh

clear
choice=0

while [ $choice != "4" ]
do
clear
echo " Menu "
echo -e "------\n"

echo "[ 1 ] Change to Home Directory"
echo "[ 2 ] Change System Prompt"
echo "[ i3 ] Set Path"
echo -e "[ 4 ] Quit\n"

echo -e "Choice : \c"
read choice

case $choice in

1) cd
sleep 2;;

2) echo -e "\nEnter New Prompt : "
read PS1
export PS1
echo -e "\nSystem Prompt Changed..."
sleep 2;;

3) echo -e "\nEnter New Path : "
read $PATH
sleep 2;;

4) echo -e "\nBye Bye !"
sleep 2
exit;;

*) echo -e "\nInvalid Choice..."
sleep 2;;

esac
done


#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 29a

# This script does the following :
# 1) Displays names of all users currently logged in.
# 2) Displays names of all users in the system
# 3) Exits from the script


while true
do
clear
echo " MENU "
echo ------

echo "[ 1 ] Display names of all users currently logged in."
echo "[ 2 ] Display names of all users who are in the system."
echo "[ 3 ] Exit"

echo -e "\nEnter Choice : \c"
read choice

case $choice in

1) set `who`
no_of_users=`who wc -l`
n=1
echo -e "List of Users"

while [ $no_of_users -gt 0 ]
do
echo "[ $n ] $1 "
shift 5
no_of_users=`expr $no_of_users - 1`
n=`expr $n + 1`
done

sleep 4;;

2) echo -e "Users in the system : \c"
cat /etc/passwd cut -d ":" -f1
sleep 4;;

3) echo "Bye Bye !"
sleep 1
exit;;

*) echo "Invalid Choice !"
sleep 2;;

esac
done





#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 31a

# Accepts a 5-10 digit no. from the user, calculates the sum of the digits
# and determines if the sum is odd or even.


#!bin/sh

no_of_digits=0
i=0
rem=0
sum=0

while [ $no_of_digits -lt 5 -o $no_of_digits -gt 10 ]
do
echo -e "\nEnter a 5-10 digit number : \c"
read number
no_of_digits=`echo $number wc -c`
no_of_digits=`expr $no_of_digits - 1`


if [ $no_of_digits -lt 5 -o $no_of_digits -gt 10 ]
then
echo -e "\nNumber does not have 5-10 digits"
fi

done

i=$no_of_digits

while [ $i -gt 0 ]
do
rem=`expr $number % 10`
sum=`expr $sum + $rem`
number=`expr $number / 10`
i=`expr $i - 1`
done

echo -e "\nSum of the digits of the number is $sum"

#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 32a

# Script that takes a filename from the command line and counts number
# of lines. It also displays an appropriate message depending on the
# number of words in the file.

if [ $# -ne 1 ]
then
echo "ERROR : Exactly 1 filename has to be specified"
exit 1
fi

if [ ! -e $1 ]
then
echo "ERROR : File not found"
exit 2
fi

if [ ! -s $1 ]
then
echo "File is empty !"
exit 0
else
echo "Number of lines : " `cat $1 wc -l`
no_of_words=`cat $1 wc -w`

if [ $no_of_words -lt 100 ]
then
echo "It is a small file"
exit 0
else
echo "It is a large file"
exit 0
fi
fi


fi



#====#====#====#====#====#====#====#====#====#====#====#

# Ques. No : 33a

# Script that accepts exactly 3 numbers from the command line and
# determines the smallest number among them. If 3 numbers are not
# passes from the command line then the script terminates with exit
# status 2


#!/bin/sh

if [ $# -ne 3 ]
then
echo "ERROR : Exactly 3 numbers have to be specified !"
exit 2
fi

min=$1

if [ $2 -lt $min ]
then
min=$2
fi

if [ $3 -lt $min ]
then
min=$3
fi

echo "Smallest number is $min"
exit 0

No comments: