Showing posts with label unixlab. Show all posts
Showing posts with label unixlab. Show all posts

Sunday, 10 August 2014

Write an awk script to compute gross salary of an employee accordingly to rule given below. If basic salary is < 10000 then HRA=15% of basic & DA=45% of basic. If basic salary is >=10000 then HRA=20% of basic & DA=50% of basic.


BEGIN{
printf"enter the basic salary:Rs"
getline bp<"/dev/tty"
if(bp<10000)
{
hra=.15*bp
da=.45*bp
}
else
{
hra=.2*bp
da=.5*bp
}
gs=bp+hra+da
printf"gross salary=Rs.%.2f\n",gs

Write an awk script to find out total number of books sold in each discipline as well as total book sold using associate array down table as given below. Electrical 34 Mechanical-1 67 Electrical-1 80 Computer Science 43 Mechanical 65 Civil-1 198 Civil 198 Computer Science-1 64


BEGIN{
printf "enter the mechanical books:"
getline mc <"/dev/tty"
printf "enter the electrical books:"
getline ec <"/dev/tty"
printf "enter the computer books:"
getline cs <"/dev/tty"
printf "enter the civil books:"
getline ci <"/dev/tty"
}
{
if(NR==1)
tot1=$2-mc
if(NR==2)
tot2=$2-ec
if(NR==3)
tot3=$2-cs
if(NR==4)
tot4=$2-ci
}
{
if(NR==4)
{
printf "total mechanical books present:%d\n",tot1
printf "total electrical books present:%d\n",tot2
printf "total computer books present:%d\n",tot3
printf "total civil books present:%d\n",tot4
}
}

Write an awk script to delete duplicated line from a text file. The order of the original Lines must remain unchanged.


BEGIN {i=1}
{
flag=1;
for(j=1;j<i && flag ; j++)
{
if(x[j]==$0)
flag=0;
}
if (flag)
{
x[j]=$0;
printf("\n%s",x[i]);
i++;
}
}

Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in the form if day, month, and year. The script should check the validity of the argument and in the case of error, display a suitable message.


BEGIN {
FS="-"
f=1;
printf("enter date with (mm-dd-yy) format:");
getline < "/dev/tty"
if(((($3%4!=0) && ($1==2) && ($2>28)) || (($3%4==0) && ($1==2) && ($2>29))) ||
((($1==1) || ($1==3) || ($1==5) || ($1==7) || ($1==8) || ($1==10) || ($1==12)) && ($2>31)) ||
((($1==4) || ($1==6) || ($1==9) || ($1==11)) && ($2>30)) || ($1<1) || ($2<1) || ($3<1) || ($1>12))
f=0;
if(f==0)
printf("you have entered an invalid date\n");
else
printf("\n \tThe date= %d\nThe month=%d\nThe year is=%d\n is valid date\n",$2,$1,$3);
}

Write a shell script that accept the file name, starting and ending line number as an argument and display all the lines between the given line number.


if [ $# -ne 3 ]
then
echo "Pass minimum three arguments"
exit
fi
c=`cat $1 | wc -l`
if [ $2 -le 0 -o $3 -le 0 -o $2 -gt $3 -o $3 -gt $c ]
then
echo "Invaid Input"
exit
fi
sed -n "$2,$3p" $1

Write a shell script that accepts two integers as its argument and compute the value of first number raised to the power of second number.


if [ $# -ne 2 ]
then
echo "Invalid number of arguments"
exit
fi
pwr=`echo $1^$2 |bc`
echo "$1 raised to $2 is : $pwr"

Write a shell script that reports the logging in of a specified user within one minute after he/she log in. The script automatically terminate if specified user does not log in during a specified period of time.


a=`date +%M`
b=`expr $a + 1`
while [ $b -gt `date +%M` ]
do
who | grep $1
if [ $? -eq 0 ]
then
echo "$1 has logged in 1 minute"
exit
fi
done
echo "$1 has not looged in within 1 minute"

Write a shell script that determine the period for which a specified user is working on system.


echo -e "enter the user name :\c"
read usr
tuser=`who | tr -s " " | head -1 | cut -d " " -f1`
if [ "$tuser" = "$usr" ]
then
tm=`who | tr -s " " | head -1 | cut -d " " -f4`
uhr=`echo $tm | cut -d ":" -f1`
umin=`echo $tm | cut -d ":" -f2`
shr=`date "+%H"`
smin=`date "+%M"`
if [ $smin -lt $umin ]
then
shr=`expr $shr - 1`
smin=`expr $smin + 60`
fi
h=`expr $shr - $uhr`
m=`expr $smin - $umin`
echo "user name : $usr"
echo "login period : $h : $m"
else
echo "Invalid User"
fi

Write a shell script that accept a list of filenames as its argument, count and report occurrence of each word that is present in the first argument file on other argument files.


if [ $# -ne 2 ]
then
echo "Error :invalid no of arguments "
exit
fi
str=`cat $1 | tr '\n' ' '`
for a in $str
do
echo "word=$a , count=`grep -c "$a" $2` "
done

Write a shell script that gets executed displays the message either “Good Morning” or “Good Afternoon” or “Good Evening” depending upon time at which the user logs in.


echo "Greetings of the day"
echo `date`
h=`date |cut -c 12-13`
if [ $h -ge 0 -a $h -lt 12 ]
then
echo "Good Morning"
elif [ $h -ge 12 -a $h -lt 18 ]
then
echo "Good Afternoon"
elif [ $h -ge 18 -a $h -lt 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi

Write a shell script that delete all lines containing a specific word in one or more file supplied as argument to it.


if [ $# -eq 0 ]
then
echo "Please enter one or more filenames as argument"
exit
fi
echo "Enter the word to be searched in files"
read word
for file in $*
do
sed "/$word/d" $file | tee tmp
mv tmp $file
done

Write a shell script that compute gross salary of an employee, accordingly to rule given below.


If basic salary is < 15000 then HRA=10% of basic 7 DA=90% of basic.
If basic salary is >=15000 then HRA=500 of basic & DA=98% of basic.
Echo ―enter Basic salary‖
read basic
if [ $basic -lt 15000 ]
then
hra=`expr 10 \* $basic / 100`
da=`expr 90 \* $basic / 100`
else
hra=`expr 50 \* $basic / 100`
da=`expr 98 \* $basic / 100`
fi
gs=`expr $basic + $hra + $da`
echo "Gross salary : Rs. $gs"

Write a shell script to compute the sum of number passed to it as argument on command line and display the result.


sum=0
for i in $*
do
sum=`expr $sum + $i`
done
echo "Sum of Number is : $sum"

Write a shell script using expr command to read in a string and display a suitable message if it does not have at least 10 characters.


echo "Enter a String"
read str
c=`expr "$str" : '.*'`
if [ $c -lt 10 ]
then
echo "The String has Less than 10 charecter"
else
echo "The String has $c charecters"
fi

Write a shell script to find smallest of three numbers that are read from keyboard. echo "Enter the Three Numbers"


read a b c
if [ $a -le $b -a $a -le $c ]
then
echo "$a is Smallest"
elif [ $b -le $c -a $b -le $a ]
then
echo "$b is Smallest"
else
echo "$c is Smallest"
fi 

Write a shell script to display the calendar for current month with current date replaced by * or ** depending on whether the date has one digit or two digits.


set `date`
y=$3
if [ $y -le 9 ]
then
cal |sed "s/$3/*/"
else
cal |sed "s/$3/**/"
fi

Write a shell script that accepts as filename as argument and display its creation time if file exist and if it does not send output error message

.
if [ $# -eq 0 ]
then
echo "No argument"
exit
fi
if [ -f $1 ]
then
time=`ls -l $1|cut -c 33-59`
echo "File $1 has created on $time"
else
echo "File $1 does not exist"
fi

Write a shell script that accept one or more filenames as argument and convert all of them to uppercase, provided they exist in current directory.


for f in $*
do
if [ -e $f ]
then
cat $f|tr "[a-z]" "[A-Z]">tmp
mv tmp $f
else
echo "File $f does not exist"
fi
done

 
- |