Linux PGM
Linux PGM
not.
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
f
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
f
------------------------------------------------------------------------------------------------------------Enter a number:
5
The number is Prime
Write a shell script to check whether a given number is an
Armstrong number or not.
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
15000
Gross Salary is: Rs.30200
Details:
Basic: Rs.15000
HRA: Rs.500
DA: Rs.14700
Write a Shell Script to generate Fibonacci Series.(Example : 0 1 1
2 3 5 8 13 ...)
clear
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
Code for Calculate the gross salary in Unix / Linux / Ubuntu
echo -e "Enter ur basic salary \c"
read sal
if [ $sal -ge 1000 ]
then
da=`expr $sal \* 40 / 100`
ha=`expr $sal \* 20 / 100`
Nsal=`expr $sal + $da + $ha`
echo "ur Basic Salary
$sal "
echo "ur Dearness Allowance $da "
echo "Ur House rent
$ha "
echo "
------------"
echo "Ur Net Salary is Rs. $Nsal "else
echo "Pls enter basic salary greater than 1000 "
f
--------------------------------------------------------------------------------$ q1
Enter ur basic salary 2000
ur Basic Salary
2000
#!/bin/bash
fle=$1
v=0
if [ $# -ne 1 ]
then
echo "$0 fleName"
exit 1
fi
if [ ! -f $fle ]
then
echo "$fle not a fle"
exit 2
fi
while read -n 1 c
do
l=$(echo $c | tr [:upper:] [:lower:])
[[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "o" || "$l" == "u" ]]
&& (( v++ ))
done < $fle
echo
echo
echo
echo
"Vowels : $v"
"Characters : $(cat $fle | wc -c)"
"Blank lines : $(grep -c '^$' $fle)"
"Lines : $(cat $fle|wc -l )"
#!/bin/bash
fle=$1
a=0
if [ $# -ne 1 ]
then
echo "$0 fleName"
exit 1
fi
if [ ! -f $fle ]
then
echo "$fle not a fle"
exit 2
fi
while read line
do
l=$(echo $line | tr [:upper:] [:lower:])
for word in $l
do
[[ $word == "a" || $word == "an" || $word == "the" ]] && ((a+
+))
done
done < $fle
echo "articles : $a"
#!/bin/bash
fle=$1
counter=0
if [ $# -ne 1 ]
then
echo "$0 fleName"
exit 1
fi
if [ ! -f $fle ]
then
echo "$fle not a fle"
exit 2
fi
while read line
do
((counter++))
EvenNo=$(( counter%2 ))
if [ $EvenNo -eq 0 ]
then
echo $line >> evenfle
else
echo $line >> oddfle
fi
done < $fle
#!/bin/bash
ADMIN="me@somewher.com"
# set alert level 90% is default
ALERT=90
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' |
while read output;
do
#echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as
on $(date)" |
mail -s "Alert: Almost out of disk space $usep" $ADMIN
fi
done
#!/bin/bash
echo -n "Enter number : "
read n
# store single digit
sd=0
# store number in reverse order
rev=""
# store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
#!/bin/bash
#store the no
num=$1
#store the value of sum
sum=0
if [ $# -ne 1 ]
then
echo "$0 number"
exit 1
fi
while [ $num -gt 0 ]
do
digit=$(( num%10 ))
num=$(( num/10 ))
sum=$(( digit+sum ))
done
echo "Sum of digits = $sum"
#!/bin/bash
#store the no
num=$1
#store the reverse number
rev=0
if [ $# -ne 1 ]
then
echo "$0 number"
exit 1
fi
while [ $num -gt 0 ]
do
digit=$(( num%10 ))
num=$(( num/10 ))
rev=$(( digit + rev*10 ))
done
echo "Reverse of number = $rev"
fle1
fle2
[DIR] test/
Total regular fles : 7
Total directories : 4
Total symbolic links : 0
Total size of regular fles : 2940
#!/bin/bash
#copying the out of ls -l command to a file
ls -l > /tmp/tmp.tmp
#initilizing values
sum=0
dir=0
fle=0
link=0
#reading the file
while read line
do
#getting the first character of each line to check the type of file
read -n 1 c <<< $line
#checking if the file is a directory or not
if [ $c == "d" ]
then
((dir++))
echo "[DIR] ${line}/" | cut -d" " --felds="1 9" >>
/tmp/dir.tmp
elif [ $c == "-" ] #true if the file is a regular file
then
((file++))
echo $line | cut -d" " -f8 >> /tmp/fle.tmp
elif [ $c == "l" ] #true if the file is a symbolic link
then
((link++))
fi
size=$( echo $line | cut -d" " -f5 ) #getting the size of the file
sum=$(( sum+size )) #adding the size of all the files
done < /tmp/tmp.tmp
cat /tmp/fle.tmp #output the name of all the files
cat /tmp/dir.tmp #output the name of all the directory
echo
echo
echo
echo
"Total
"Total
"Total
"Total
#!/bin/bash
START=$HOME
# change your directory to command line if passed
# otherwise use home directory
[ $# -eq 1 ] && START=$1
if [ ! -d $START ]
then
echo "$START not a directory!"
exit 1
fi
# use find command to get all subdirs name in DIRS variable
DIRS=$(find "$START" -type d)
# loop thought each dir to get the number of files in each of subdir
for d in $DIRS
do
[ "$d" != "." -a "$d" != ".." ] && echo "$d dirctory has $(ls -l $d | wc -l)
fles"
done
#!/bin/bash
SRC="$1"
DST="$2"
if [ $# -ne 2 ]
then
echo "$(basename $0) dir1 dir2"
exit 1
fi
if [ ! -d $SRC ]
then
echo "Directory $SRC does not exists!"
exit 2
fi
if [ ! -d $DST ]
then
echo "Directory $DST does not exists!"
exit 2
fi
for f in $DST/*
do
#echo Processing $f
if [ -f $f ]
then
tFile="$SRC/$(basename $f)"
if [ -f $tFile ]
then
echo -n "Deleting $tFile..."
/bin/rm $tFile
[ $? -eq 0 ] && echo "done" || echo "failed"
fi
fi
done
#!/bin/bash
# Shell script for search for no password entries and lock all accounts
# Set your email
ADMINEMAIL="admin@somewhere.com"
### Do not change anything below ###
#LOG File
LOG="/root/nopassword.lock.log"
STATUS=0
TMPFILE="/tmp/null.mail.$$"
echo "-------------------------------------------------------" >>$LOG
echo "Host: $(hostname), Run date: $(date)" >> $LOG
echo "-------------------------------------------------------" >>$LOG
# get all user names
USERS="$(cut -d: -f 1 /etc/passwd)"
# display message
echo "Searching for null password..."
for u in $USERS
do
# find out if password is set or not (null password)
passwd -S $u | grep -Ew "NP" >/dev/null
if [ $? -eq 0 ]; then # if so
echo "$u" >> $LOG
passwd -l $u #lock account
STATUS=1 #update status so that we can send an email
fi
done
echo
"============================================
============" >>$LOG
if [ $STATUS -eq 1 ]; then
echo "Please see $LOG fle and all account with no password are
locked!" >$TMPFILE
echo "-- $(basename $0) script" >>$TMPFILE
mail -s "Account with no password found and locked" "$ADMINEMAIL" <
$TMPFILE
# rm -f $TMPFILE
fi
#!/bin/bash
# Shell program to read two numbers and display all the odd
echo -n "Enter frst number : "
read n1
echo -n "Enter second number : "
read n2
if [ $n2 -gt $n1 ];
then
for(( i=$n1; i<=$n2; i++ ))
do
# see if it is odd or even number
test=$(( $i % 2 ))
if [ $test -ne 0 ];
then
echo $i
fi
done
else
echo "$n2 must be greater than $n1, try again..."
fi
Write a shell script to arrange numbers in ascending or
descending order as per the user choice
echo enter the no of element
read n1
for (( i=0; i<$n1; i++ ))
do
echo enter `expr $i + 1` the element.
read a[$i]
done
for (( i=0; i<$n1; i++ ))
do
for (( j=`expr $i + 1`; j<$n1; j++ ))
do
if [ ${a[$i]} -gt ${a[$j]} ]
then
x=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$x
f
done
done
echo 1.Ascending 2.Descending
echo enter your choice...
read c
if [ $c = 1 ]
then
echo the ascending order is....
for (( i=0; i<$n1; i++ ))
do
echo ${a[$i]}
done
elif [ $c = 2 ]
then
echo the descending order is...
for (( i=$n1; i>0; i-- ))
do
echo ${a[`expr $i - 1`]}
done
else
echo wrong choice......
f
Code for menu driven shell program which have the options
change the term, change the prompt and user list of unix
system. in Unix / Linux / Ubuntu
count = 0
while [ $count -eq 0 ]
do
tput clear
echo "1. Change the term "
echo "2. Change the prompt "
echo "3. User list of unix system "
echo "4. Quit "if [ $choice -eq 1 ]
then
elif [ $choice -eq 2 ]
elif [ $choice -eq 3 ]
$who
else
count = 1
exit
f
done
Program 3
Write a menu driven program in shell script to check all the
numeric comparison between the variables for each and every
option
echo "Numeric Comparison"
echo "Enter two numbers to do numeric comparison"
read a
read b
echo
echo
echo
echo
echo
echo
echo
""
"Menu"
"1.Equal to"
"2.Not equal to"
"3.Greater than"
"4.Greater than or equal to"
"5.Lesser than"
;;
7) exit ;;
esac
Program 4
Write a shell script to get the pattern, input filename and output
filename and retrieve the lines of matching pattern and store it in
the output file, check the conditions like input strings are not null
and not equal
echo " program to store the output in a fle "
echo "Get the input from command line"
if test $# -ne 3
then
echo "you have not keyed in 3 arguments"
echo "syntax is sh fle.sh pattern inputfle outputfle"
exit 3
else
if grep "$1" $2 > $3
then
echo "Pattern found"
else
echo "Pattern not found"
f
echo "Contents of output fle"
cat $3
f
Program 5
Write a shell script to get a filename and check all file related
comparisons like file exists, readable and writable etc
echo " File related exist status"
echo "Enter the fle name"
read fname
echo
echo
echo
echo
""
"File related test are listed below"
"1.Existence"
"2.Regular fle"
echo
echo
echo
echo
echo
"3.Readable fle"
"4.Writable fle"
"5.executable fle"
"6.directory"
"7. fles size greater than zero"
done
Program 7
Write a shell script to that retrieve the information from the shell
command to display the full format of date. ( no. of days present
in the current month using case )
echo Display date in full format
d = `date +%d`
m = `date +%m`
y = `date +%y`
echo $d - $m 20$y
echo the no. of days in each month
case $m in
1) mm = January
nn = 31 ;;
2) mm = February
nn=28 ;;
3) mm = March
nn=31 ;;
4) mm = April
nn=30 ;;
5) mm = May
nn=31 ;;
6) mm = June
nn=30 ;;
7) mm = July
nn=31 ;;
8) mm = August
nn=31 ;;
9) mm = September
nn=30 ;;
10) mm = October
nn=31 ;;
11) mm = November
nn=30 ;;
12) mm = December
nn=31 ;;
esac
echo the current month is $mm the no. of days in this month is $nn
Program 8
Write a menu driven program using shell script to do all the
arithmetic operations
echo Menu driven program to do all arithmetic operations
while true; do
echo Enter two numbers a and b
read a
read b
echo
echo
echo
echo
echo
echo
echo
Program 9
Write a menu driven program to do the following :
a. copy one file to another
b. move one file to another
c. remove a file
d. create a file
e. compare two files
f. difference between two files
g. link two files
h. exit to shell
echo Menu fle comparisons
while true ; do
echo Name of frst fle
read f1
echo Name of second fle
read f2
echo Menu
echo "1. copy one fle to another
echo "2. move one fle to another
echo "3. remove a fle
echo "4. create a fle
echo "5. compare two fles
echo "6. difference between two fles
echo "7. link two fles
echo "8. exit to shell
echo "Enter the choice"
read choice
case $choice in
1) echo "copy one fle to another"
echo The source fle - $f1 and destination fle - $f2
cp $f1 $f2
cat $f1
cat $f2 ;;
2) echo "move one fle to another"
echo The source fle - $f1 and destination fle - $f2
mv $f1 $f2
cat $f1
cat $f2 ;;
3) echo "remove a fle "
echo The fle to remove - $f1
rm $f1
cat $f1;;
4) echo "create a fle"
echo The fle to be created- $f1
cat > $f1 ;;
5) echo "compare two fles "
echo The 1st fle - $f1 and 2nd fle - $f2
cmp $f1 $f2 ;;
6) echo "difference between fles"
echo The 1st fle - $f1 and 2nd fle - $f2
diff $f1 $f2 ;;
7) echo "link 2 fles"
echo The 1st fle - $f1 and 2nd fle - $f2
ln $f1 $f2
cat >>$f1
cat $f2 ;;
8) exit ;;
esac
done
Program 10
Write a shell script to list all the files greater than 250 bytes.
for fle in *
do
if [ -f $fle ] ; then
x=`ls -l $fle | awk '{print $5}' `
if [ $x -gt 250 ]
then
echo the fle is $fle and bytes : $x
f
f
done
Program 11.
Give salutation like good morning, good afternoon depends
upon the current time.
Program 13.
Write a shell script to check whether the input is the number or
the character.
echo Check for number or a character/string
echo Enter the input string or number
read ip
if [ -z $ip ] ; then
echo You have not entered the String ; exit 1
f
if [ -n $ip ] ; then
echo The entered input is a string/character
else
echo The entered input is a numerical character
f
Program 14.
Write a shell script to check whether the person is logged in or
not
echo "login name of ur friend"
read l
until who | grep $l
do sleep 60;
done
echo $1 is now logged in
Program 15.
Write a shell script to print the number of lines and words in a
file.
echo print the number of lines and words in a fle
echo enter the name of the fle
read fname
l = wc l $fname
w = wc w $fname
c = wc c $fname
echo Number of lines present in the fle $fname - $l
echo Number of words present in the fle $fname - $w
echo Number of characters present in the fle $fname - $c
Program 16.
Write a shell script to give the count and display the number of
subdirectories, ordinary files present in the current directory. Do
it separately.
echo "Display the directories and fles seperately"
for fle in *
do
if [ -f $fle ] ; then
echo $fle >> flist
elif [ -d $fle ] ; then
echo $fle >>dirlist
f
done
echo "the list of fles and their count"
cat flist
echo "Count - `wc -l flist`"
Program 18.
Write a shell script to find out the sum of digits in a five digit
number
echo Sum of digits in a fve digit number
echo enter the fve digit number :
read n
i=0
a = $n
while [ $a ge 0 ] ; do
$i = ` expr $a % 10 `
echo "List of fles which contain the pattern in its fle content"
for fle in *
do
if grep "$pattern" $fle > /dev/null
then
echo $fle >> $flist
f
done
cat flist
echo "Count - `wc -l $flist`"
Program 21
Write a shell script to print the Fibonacci series
echo Fibonacci Series
echo Enter the number :
read n
i=2
a=0
b=1
echo Fibonacci Series upto $n terms :
echo $a
echo $b
while [ $i le $n ] ; do
c = ` expr $a + $b `
echo $c
$i = ` expr $i + 1 `
done
Program 22
Write a shell script that takes a command line argument of option
and kilometer. If option is m ( convert kilometers to meters) and
option is d ( convert kilometer to decimeter) , finally if option is
c (convert kilometer to centimeter)
Ex: sh file.sh m 23
Result should be : 23000 meters
echo "Conversion Program"
k=$2
if [ $# -ne 2 ]
then
echo "You have not keyed in 2 arguments"
exit 3
else
if [ $1 = "-m" ]
then
ans=`echo $k \* 1000 | bc`
echo "$ans meters"
elif [ $1 = "-d" ]
then
ans=`echo $k \* 10000 | bc`
echo "$ans decimeters"
elif [ $1 = "-c" ]
then
ans=`echo $k \* 100000 | bc`
echo "$ans centimeters"
f
f
~
Program 23
Write a shell script to check whether the input number is odd or
even
echo Check for odd or even
echo Enter the number :
read n
i = ` expr $n % 2 `
if [ $i eq 0 ] ; then
echo the entered number $n is an even number
else
echo the entered number $n is an odd number
f
Program 24
Write a shell script to comparison of 3 numbers and print the
greatest and the smallest
echo "Comparison of 3 nos"
Program 26
Write a shell script to get the basic salary of a person and
calculate his gross salary, whose da is 40% of basic, and hra is
20% of basic, pf is 10% of basic. Hint: gs = basic + da + hra pf
echo Calculation of Gross Salary
echo Enter the basic salary of a person
read bp
esac
done
Program 28.
Write a shell script to calculate the simple interest for three sets
for p, n, r using loops
echo Simple interest for 3 set
i=1
while [ $i le 3]
do
echo enter the
echo enter the
read p
echo enter the
read n
echo enter the
read r
details of person $i
principle amount:
no, of years :
rate of interest: