Вы находитесь на странице: 1из 22

#File : calendar_m

#Q1)Write a shell program to send you a reminder mail


#for upcoming events.
awk <$HOME/calendar '
BEGIN {
x = "Jan 31 Feb 28 Mar 31 Apr 30 May 31 Jun 30 " \
"Jul 31 Aug 31 Sep 30 Oct 31 Nov 30 Dec 31 Jan 31"
split(x,date)
for ( i = 1; i < 24; i += 2) {
days[date[i]] = date[i+1]
nextmon[ date[i]] = date[i+2]
}
split("'"`date`"'",date)
mon1 = date[2]; day1 = date[3]
mon2 = mon1; day2 = day1 + 1
if ( day1 >= days[mon1]) {
day2 = 1
mon2 = nextmon[mon1]
}
}
$1 == mon1 && $2 == day1 || $1 == mon2 && $2 == day2
' | mail $NAME

#File:fold_m
#Q2)Write a shell program that fold line of text beyond 30 characters.
sed 's/\t/
/g' $* |
#convert tabs to 8 spaces
awk '
BEGIN {
N = 30
# folds at column 30
for ( i =1 ; i <= N; i++) # make a string of blanks
blanks = blanks " "
}
{
if ( (n = length ( $0 ) ) <= N )
print
else {
for ( i = 1; n > N ; n -= N ) {
printf "%s\\\n", substr($0,i,N)
i += N;
}
printf "%s%s\n",substr(blanks,i,N-n), substr( $0,i)
}
}'

#File:cal_m
#Q3)Write a shell program to enhance the inbuild cal program.
case $# in
0) set `date`; m=$2: y=$6 ;; # no args :use today
1) m=$1; set `date`; y=$6 ;; # 1 args : use this year
*) m=$1; y=$2 ::
esac
case $m in
jan* | Jan* )
m=1
feb* | Feb* )
m=2
mar* | Mar* )
m=3
apr* | Apr* )
m=4
may* | Mar* )
m=5
jun* | Jun* )
m=6
jul* | Jul* )
m=7
aug* | Aug* )
m=8
sep* | Sep* )
m=9
oct* | Oct* )
m=10
nov* | Nov* )
m=11
dec* | Dec* )
m=12
[1-9] | 10 | 11 | 12 ) ;;
*)
y=$m;
esac
/usr/bin/cal $m $y

;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
m="" ;;

#numeric month
#plain year

#File:existance_test_m
#Q4)Write a shell program to test wheather a particular file exist and
#a directory or not.
ch=y
while [ $ch == y ]
do
printf "\nEnter a file/directory name to search in current directory:"
read name
if [ -e $name ]
then
if [ -d $name ]
then
printf "$name exists and is a directory.\n"
else
printf "$name exists and is not a directory.\n"
fi
else
printf "$name does not exist\n"
fi
echo -n "more(y/n):"
read ch
done

#File:user_search_m

#Q5)To test wheather a user logged in


echo -n Enter user not to search:
read user
who | grep -w "$user" >/dev/null
if [ $? = 0 ]
then
echo $user has logged in.
else
echo $user has not logged in.
fi

#File:overwrite

or not.

#Q6)Write a shell program that overwrites a file.


if [ $# -lt 1 ]
then
echo "No argument is passed."
else
for file in $*
do
if [ -f $file ]
then
echo "$file is exist."
echo "Enter data to overwrite it:-"
cat >$file
else
echo "$file does not exist or directory."
fi
done
fi

#File:permission_m

#Q7)WAP to check all permission of files.


if [ $# -lt 1 ]
then
echo 'Usage:q6 file';exit
else
for file in $*
do
if [ -e $file ]
then
echo -e "\n$file exists."
ls -l $file |
awk '
{
printf "File Name:%s",$NF
for( i=1; i<=10;i++)
a[i]=substr($1,i,1)
printf "\nFile Permission:-"
if(a[1]=="d")
printf "\nIt is a directory"
else
printf "\nIt is a normal file."
printf "\nFor user:-"
if(a[2]=="r")
printf "\nIt is readable"
else
printf "\nIt is not readable."
if(a[3]=="w")
printf "\nIt is writeable"
else
printf "\nIt is not writeable."
if(a[4]=="x")
printf "\nIt is executeable"
else
printf "\nIt is not executeable."
printf "\nFor Group:-"
if(a[5]=="r")
printf "\nIt is readable"
else
printf "\nIt is not readable."
if(a[6]=="w")
printf "\nIt is writeable"
else
printf "\nIt is not writeable."
if(a[7]=="x")
printf "\nIt is executeable"
else
printf "\nIt is not executeable."
printf "\nFor Others:-"
if(a[8]=="r")
printf "\nIt is readable"
else
printf "\nIt is not readable."
if(a[9]=="w")
printf "\nIt is writeable"
else
printf "\nIt is not writeable."
if(a[10]=="x")

printf "\nIt is executeable"


else
printf "\nIt is not executeable.\n"

done

}'
else
echo -e
fi

fi

#File:dis_arg_sep_m

"\n$file does not exits"

#Q8)WAP thet read the user input and display each argument separately.
for word in $*
#to read a argument at a time
do
echo $word
#to display it.
done

#File:gross_salary_m

#Q9)To calculatees the gross salary of an employee


printf "\nEnter Name and Gross salary of employees:-"
printf "\nNAME OF EMPLOYEE\tBASIC SALARY(Rs.)\n"
cat >temp
#tempory file to contain employee list and salary
printf "\n\n\n\t\t########EMPLOYEE SALARY TABLE#########\n"
awk '
BEGIN { printf "\nNAME OF EMPLOYEE\tBASIC SALARY\t HRA\t\t DA\t\tGROSS
SALARY"
printf "\n
\t ( in Rs.)\t(Rs.)\t\t(Rs.)\t\t(Rs.)\n"
}
{

if ( $NF < 3500 )


printf
"\n%s\t\t%.2f\t\t%.2f\t\t%.2f\n",
$0,.1*$NF,.9*$NF,($NF+.1*$NF+.9*$NF)
else if ( $NF < 5000 && $NF >= 3500 )
printf "\n%s\t\t%.2f\t\t%.2f\t\t%.2f\n",
$0,.12*$NF,.93*$NF,($NF+.12*$NF+.93*NF)
else
printf "\n%s\t\t%.2f\t\t%.2f\t\t%.2f\n",
$0,.15*$NF,.95*$NF,($NF+.15*$NF+.95*$NF)
}' temp
# taking input from temp file
rm temp
# removing temp for forther use

#File:grade_m

#Q10)WAP to calculate the grade obtained by a student


#depending upon marks
ch=y
while [ $ch == y ]
do
echo -n Enter name of student:
read name
echo "Enter marks obtained in a paper(total marks 100)"
echo -n Paper1: #read marks in eac paper
read p1
echo -n Paper2:
read p2
echo -n Paper3:
read p3
echo -n Paper4:
read p4
echo -n Paper5:
read p5
sum=`expr $p1 + $p2 + $p3 + $p4 + $p5 ` #sum of marks
if [ $sum -lt 200 ] #spacifing grade of student
then
echo "$name obtained $sum and he/she failed"
elif [ $sum -lt 250 -a $sum -ge 200 ]
then
echo "$name obtained $sum and grade is D"
elif [ $sum -lt 300 -a $sum -ge 250 ]
then
echo "$name obtained $sum and grade is C"
elif [ $sum -lt 350 -a $sum -ge 300 ]
then
echo "$name obtained $sum and grade is B"
elif [ $sum -lt 400 -a $sum -ge 350 ]
then
echo "$name obtained $sum and grade is A"
elif [ $sum -lt 450 -a $sum -ge 400 ]
then
echo "$name obtained $sum and grade is A+"
elif [ $sum -ge 450 ]
then
echo "$name obtained $sum and grade is A++"
if
echo -n "more(y/n)?:" #exicute again or not
read ch
done

#File:linear_search_m

#Q11)WAP to implement Linear search.


awk '
BEGIN {
print "Enter your data (each data in a seprate line),"
print "Use \"search data\" to search a data in inputed
data:"
}
{
if( $1 != "search")
arr[NR] = $0
else
search = substr($0,8)
}
END
{
printf "\nThis is your input data:"
for(i=1; i<NR; i++ )
printf "\n\t%s", arr[i]
flag=0
for(i=1; i<NR; i++ )
if(search== arr[i])
{
printf "\n\"%s\" is found at possion %d.\n",search,i
flag=1
}
if(flag==0)
printf "\n%s is not found in inputed data.\n",search
}'

#File:insertion_sort_m

#Q12)WAP to implement Insertion sort.


awk '
BEGIN {
print "Enter each data in seprate line."
}
{
arr[NR]=$0 #read each input and save in a array
}
END {
#sorting array using insertion sort
for( j = 2; j <= NR; j++)
{
key=arr[j]
i=j-1
while(i > 0 && arr[i] > key )
{
arr[i+1]=arr[i]
i-}
arr[i+1]=key
}
print "This is my sorted input data"
for( i = 1; i <= NR; i++ )
print arr[i] #print sorted array
}'

#File:prime_test_m
#Q13)To check whether a no is prime or not
if [ $# -lt 1 ]
then
echo No command line argument is passed. #if no argument is passed
else
for num in $* #for each argument
do
flag=0
i=2
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` == 0 ]
then
#checking a no is divigable or not
flag=1
fi
i=`expr $i + 1`
done
if [ $flag == 0 ]
then
echo $num is a prime.
else
echo $num is not a prime.
fi
done
fi

#File:sorted_user_m

#Q14)To display users in sorted order and no of users


#and store it in file FILE1
echo -e '\tTHE SORTED OUTPUT OF "who"' >FILE1
who | sort
>> FILE1 #to write in file FILE1
cat FILE1
#display on screen.
echo "No of user = `who | wc -l`" #to count no of user

#File:table_m

#Q15)To print muliplaction table.


ch=y
while [ $ch == y ]
do
echo -n Enter a no to print its multiplaction table:
read val
echo -e " Multiplaction table of $val\n"
i=1
while [ $i -le 12 ]
do
echo -e "\t$val*$i=`expr $val \* $i`"
i=`expr $i + 1`
done
echo -n "more(y/n)?:"
read ch
done

#File:file_com_m

#Q16)To check content of two file is same or not,


#if same then remove second
echo -n Enter your first file name:
read file1
echo -n Enter your second file name:
read file2
if [ -f $file1 -a -f $file2 ]
then
if [ $file1 = $file2 ]
then
echo Both files are same.
else
cmp $file1 $file2 >/dev/null
if [ $? = 1 ]
then
echo Both files do not have same content
else
echo Both file has same content.
rm $file2
echo File $file2 has been removed.
fi
fi
else
echo File is not exist.
fi

#File:sum_m

#Q17)Find the sum of digit of the number entered at command line.


if [ $# -lt 1 ]
then
echo No no is entered at command line. #if agument is not passed
else
for num in $* #for each argument
do
num1=$num
sum=0
while [ $num gt 0 ]
do
sum=`expr $sum + $num % 10` #add each digit
num=`expr $num / 10 `
done
echo sum of digit of $num1:$sum
done
fi

#File:sorted_print_m

#Q18)To print sorted contents of three file.


printf "\nNAME OF FILES TO PRINT"
printf "\nEnter fitst file:"
read file1
printf "Enter second file:"
read file2
printf "Enter third file:"
read file3
if [ -f $file1 a f $file2 a f $file3 ]
then
cat $file1 $file2 $file3 | sort | more
else
echo "File does not exist or directory
fi

#File:cal
#Q22)To modify "cal" command
for m in $@
do
case $m in
jan* | Jan* )
m=1
feb* | Feb* )
m=2
mar* | Mar* )
m=3
apr* | Apr* )
m=4
may* | Mar* )
m=5
jun* | Jun* )
m=6
jul* | Jul* )
m=7
aug* | Aug* )
m=8
sep* | Sep* )
m=9
oct* | Oct* )
m=10
nov* | Nov* )
m=11
dec* | Dec* )
m=12
esac
set `date` ; y=$6
/usr/bin/cal $m $y
done

;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;

#File:user_search_m
#Q24)To test a name is user or not.
echo -n Enter user not to search:
read user
cat /etc/passwd | grep -w "$user" >/dev/null
if [ $? = 0 ]
then
echo "$user is a valid name."
else
echo "$user is not a validname."
fi

#File:date_m
#Q25)To display date in the mm/dd/yyyy format.
echo -n "Today date is `date +%m`/`date +%d`" #to get mm/dd/
set `date`;echo $6 #to get yyyy

Вам также может понравиться