Thursday, January 19, 2006

some codes in shell programming

Some codes in shell programming
#this a test
ls
who
pwd
#this a copy test
cp $1 $2
cat $2
echo "I like work... \t I can sit and watch it for hours"
echo "enter your choice \c"
echo "\07"
echo "I like work... \t I can sit and watch it for hours"
echo "enter your choice \c"
echo "\07"
this is a test
tput bel
tput clear
echo "Total no. of colomns on screen = \c"
tput cols
echo "Total no. of rows = \c"
tput lines
echo "This is a normal message"
tput blink
echo "This is a blinking statement"
tput bold
echo "this is a bold message"
tput cup 10 20
echo "Testing of tput"
tput smso
echo "the bold has begun"
tput rmso
tput smul
echo "So also has the underline"
tput rmul
echo read source and target file names
read source target
if cp $source $target
then
echo File copied successfully
else
echo Failed to copy the file
fi
if test 1 -gt 3
then
echo File copied successfully
else
echo Failed to copy the file
fi
echo Enter a number from 1 to 10
read num
if test $num -lt 6
then
echo "jkfhj"
fi
echo Enter the basic salary
read bs
if [ $bs -lt 1500 ]
then
hra=`echo $bs \* 10/100 bc`
da=`echo $bs \* 90/100 bc`
else
hra=500
da=`echo $bs \* 98/100 bc`
fi
gs=`echo $bs + $hra +$da bc`
echo Gross salary = Rs.$gs
echo Enter file name
read fname
if [ -f $fname ]
then
echo You indeed entered a file name
else
echo what you entered is not a file name
fi
echo Enter filename
read filename
if [ -w $filename ]
then
echo Type matter to append. To stop type Ctrl -D
cat >>$filename
else
echo No write permission
fi
str1="Good"
str2="Bad"
str=
[ $str1 = $str2 ]
echo $?
[ $str1 != $str2 ]
echo $?
[ -n $str1 ]
echo $?
[ -z "$str3" ]
echo $?
[ -z $str3 ]
echo $?
["$str3" ]
echo $?
str1="good morning"
str2="good bye"
[ "$str1"="$str2" ]
echo $?
echo enter a number
read b
if [ $b -eq 1 ]
then
echo this is a test
else
if [ $b -eq 2 ]
then
echo not good
else
echo not bad
fi
fi
set -vx
echo enter a number
read a
if [ $a -lt 20 ]
then
echo test
fi
echo "enter marks in five subjects"
read m1 m2 m3 m4 m5
#per=`expr \($m1 + $m2 + $m3 + $m4 + $m5 \) / 5`
per=`expr \( $m1 + $m2 + $m3 + $m4 + $m5 \) / 5`
if [ $per -ge 60 ]
then
echo First division
fi
if [ $per -le 50 -a $per -lt 60 ]
then
echo Second Division
fi
if [ $per -ge 40 -a $per -le 50 ]
then
echo Third Division
fi
if [ $per -lt 40 ]
then
echo Fail
fi
echo " 5 numbers please"
read a b c d e
per=$[ $a+$b+$c+$d+$e ]
echo $per
read a b c d e
per=`expr \( $a + $b + $c + $d + $e \) / 5 `
echo $per
if [ $per -ge 60 ]
then
echo First Division
else
if [ $per -ge 50 -a $per -lt 60 ]
then
echo Second Division
else
if [ $per -ge 40 -a $per -lt 50 ]
then
echo Third Division
else
if [ $per -lt 40 ]
then
echo Fail
fi
fi
fi
fi
echo "Enter any file name "
read fname
if [ ! -z "$fname" ]
then
if [ -r $fname -a -w $fname -a -x $fname ]
then
echo You have read, write and execute permissions
else
echo Read, write and execute permissions are denied
fi
else
echo Improper filename
fi
echo Enter the adapter name
read ad
if [ "$ad" = MA ]
then
echo You have a monochrome adapter
elif [ "$ad" = CGA ]
then
echo "You have a color graphics adapter"
elif [ "$ad" = EGA ]
then
echo "You have an enhanced graphics adapter"
elif [ "$ad" = VGA ]
then
echo "You have a video graphics adapter"
else
echo "You have super video graphics adapter"
fi
echo Enter a number from 1 to 3
read num
case $num in
1) echo You entered 1
;;
2) echo You entered 2
;;
3) echo You entered 3
;;
*) echo Number bt 1 to 3
;;
esac
case $1 in
cat dog) echo You supplied cat at the command line
;;
parrot crow) echo You supplied dog at the command line
;;
whale shark) echo You supplied parrot at the command line
;;
*) echo You supplied a incorrect argument at the command line
;;
esac
echo Enter any charachter
read char
case $char in
[a-z]) echo You have entered a small case character
;;
[A-Z]) echo You have entered a capital character
;;
[0-9]) echo You have entered a digit
;;
?) echo You have entered a special character
;;
*) echo You entered more than one character
;;
esac
echo Enter any charachter
read char
case $char in
[aeiou]*) echo The word begins with a small case vowel
;;
[AEIOU]*) echo The word begins with a capital vowel
;;
*[0-9]) echo The word ends with a digit
;;
???) echo You have entered a three letter word
;;
esac
count=1
while [ $count -le 3 ]
do
echo "\nEnter values of p, n, and r \c"
read p n r
si=` echo $p \* $n \* $r / 100 bc `
echo Simple interest = Rs. $si
count=` expr $count + 1 `
done
count=1
while [ $count -le 10 ]
do
echo $count
count=` expr $count + 1 `
done
count=10
while [ $count -gt 1 ]
do
echo $count
count=` expr $count - 1 `
done
count=10
while [ $count -le 20 ]
do
echo "this is a test"
count=` echo $count + 0.1 bc `
done
if [ $# -lt 1 ]
then
echo "Improper Usage"
echo "Correct usage is: $0 username"
exit
fi
logname=$1
time=0
while true
do
who grep "$logname" > /dev/null
if [ $? = 0 ]
then
echo $logname has logged in
if [ $time -ne 0 ]
then
echo He is $time minutes late
fi
exit
else
time=` expr $time + 1 `
sleep 60
fi
done
if [ $# -lt 1 ]
then
echo "Improper usage: $0 username"
fi
logname=$1
line=` grep $logname /etc/passwd `
oldifs="$IFS"
IFS=:
set $line
clear
tput cup 10 20
echo User=$1
tput cup 11 20
echo User ID=$3
tput cup 12 20
echo Group ID=$4
tput cup 13 20
echo Comment about the user=$5
tput cup 14 20
echo Default working directory=$6
tput cup 15 20
echo Default working shell=$7
echo Enter a file name
read fname
terminal=` tty `
exec < $fname
while read line
do
echo $line
done
exec < $terminal
if [ $# -eq 1 ]
then
terminal=` tty `
exec < $1
flag=1
fi
nol=0
now=0
while read line
do
nol=` expr $nol + 1 `
set -- $line
now=` expr $now + $# `
done
echo "Number of lines = $nol"
echo "Number of words = $now"
if [ "$flag" -eq 1 ]
then
exec < $terminal
fi
echo Enter a filename
read fname
terminal=` tty `
exec < $fname
while read line
do
echo $line
echo "Press any key... "
read key < $terminal
done
exec < $terminal
i=1
until [ $i -gt 10 ]
do
echo $i
i=` expr $i + 1 `
done
name=$1
echo $1
set `who am i`
echo $1 $name
mv $name $name.$1
for word in High on a hill was a lovely mountain
do
echo $word
done
for entry
do
echo $entry
done
for entry in *
do
if [ -d $entry ]
then
echo $entry
fi
done
if [ $# -lt 1 ]
then
echo "Improper usage: $0 pathname"
fi
oldifs="$IFS"
IFS=/
for arg in $*
do
if [ -d $arg ]
then
cd $arg
else
if [ -f $arg ]
then
echo $arg is a file
exit
else
mkdir $arg
cd $arg
fi
fi
done
IFS="$oldifs"
r=1
while [ $r -le 3 ]
do
c=1
while [ $c -le 3 ]
do
sum=` expr $r + $c `
echo r = $r c = $c sum = $sum
c=` expr $c + 1 `
done
r=` expr $r + 1 `
done
pathname=$1
oldifs="$IFS"
IFS=/
flag=no
set $pathname
for dir in $*
do
if [ ! -d $dir ]
then
echo $dir is not a directory... hence breaking out
flag=yes
break
else
cd $dir
num=` ls wc -l `
echo "$dir has $num entries"
fi
done
if [ flag = yes ]
then
echo Abrupt end to the for loop
fi
IFS=$oldifs
i=1
j=1
while [ $i -le 100 ]
do
while [ $j -lt 200 ]
do
if [ $j -eq 150 ]
then
break
else
echo $i $j
fi
j=` expr $j + 1 `
done
i=` expr $i + 1 `
done
i=1
while [ $i -le 2 ]
do
j=1
while [ $j -le 2 ]
do
if [ $i -eq $j ]
then
j=` expr $j + 1 `
continue
fi
j=` expr $j + 1 `
echo $i $j
done
i=` expr $i + 1 `
done
cat $*
cat $@
old=` stty -g `
stty -echo iuclc
echo "Enter password: \c"
read pw
stty $old
if [ "$pw" = icit ]
then
echo You supplied the correct password
else
echo wrong password
fi
echo Total number of arguments = $#
for file in *
do
if [ -f $file -a ! -s $file ]
then
echo "Delete $file y/n \c"
ans=""
stty -icanon min 0 time 0
while [ -z "$ans" ]
do
read ans
done
if [ $ans = y -o $ans = Y ]
then
rm -f $file
echo "$file deleted... "
fi
fi
done
stty sane
mailopt=false
interval=60
if [ "$1" = m ]
then
mailopt=yes
fi
if [ "$2" = -t ]
then
interval=$3
fi
while true
do
who grep "$4" > /dev/null
if [ $? -eq 0 ]
then
if [ $mailopt = yes ]
then
echo $4 has logged in mail 'logname'
else
echo $4 has logged in
fi
break
else
sleep $interval
fi
done
getopts ab choice
case $choice in
a) echo You entered a
;;
b) echo You entered b
;;
?) echo Wrong choice
esac
while getopts abcd choice
do
case $choice in
a) echo You entered a
;;
b) echo You entered b
;;
c) echo You entered c
;;
d) echo You entered d
;;
?) echo Wrong choice
esac
done
while getopts ac:d choice
do
case $choice in
a) echo You entered a with argument $OPTARG
;;
c) echo You entered c with argument $OPTARG
;;
d) echo You entered d
esac
done
mailopt=false
interval=60
while getopts mt: choice
do
case $choice in
m) $mailopt=true
;;
t) interval="$OPTARG"
;;
?) echo Improper Usage
exit
;;
esac
done
if [ $OPTIND -ne $# ]
then
echo Improper usage
exit
fi
while true
do
who grep $logname > /dev/null
if [ $? -eq 0 ]
then
if [ $mailopt = true ]
then
set 'who am i'
echo $logname has logged in mail $1
else
echo $logname has logged in
fi
exit
else
sleep $interval
fi
done
while getopts mt: choice
do
case $choice in
m) echo This is m
;;
t) echo this is t with $OPTARG
;;
?) echo Improper Usage
;;
esac
done
while getopts mt: choice
do
case $choice in
m) mailopt=true
;;
t) interval=$OPTARG
;;
?) echo Improper Usage
exit
;;
esac
done
trap "" 1 2 3
echo terminal
echo locked
read key
while true
do
echo "Enter your password: \c"
stty -echo
read pw
stty sane
if [ "$pw" = icit ]
then
break
else
echo Wrong password. You are a illegal user
fi
done
dir()
{
ls
}
copy()
{
cp $1 $2
}
ren()
{
mv $1 $2
}
echo Hello!
echo Today is ` date `
./s17.sh
echo Now the control is on s17.sh
echo Returning back to the calling script
set If you are headed in the wrong direction. God allows U turns.
echo $*
shift 1
echo $*
shift 1
echo $*
shift 1
echo $*
shift
echo $*
cat opy
while read line
do
grep op $line
done
for var in merry had a little lamb
do
echo $var
done > little
option=${1--la}
ls $option
file=$1
cat ${file?File does not exist}
echo Through with the file!
y=x
z=y
eval echo \$$z
output="opy > gear"
eval cat $output
x=100
ptr=x
eval $ptr=50
echo $x
a=20 b=10
echo `expr $a + $b`
echo `expr $a - $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b`
a=10.5
b=3.5
c=`echo $a + $b bc`
d=`echo $a - $b bc`
e=`echo $a \* $b bc`
f=`echo $a / $b bc`
echo $c $d $e $f
echo Enter the values a, b and c
read a b c
echo $a $b $c
echo "I like work... \b I can sit and watch it for hours"
echo "I like work... \t I can sit and watch it for hours"
echo "enter your choice \c"
echo "\07"
#this a test
ls
who
pwd



nvoids.in

blog.nvoids.in

No comments: