Monday, June 18, 2012

Unix Programs

  1. WAP that accepts user name and reports if user logged in.
echo “Press or enter the user name”
read a
who >userlist
if grep $a userlist
then
echo “user logged on”
else
echo “user not logged on”
fi
  1. WAP that takes a filename as input and checks if it is executable, if not make it executable.
echo “Enter your file name”
read a
if [ ! –e $a ]
then
echo “file not exist”
elif [ ! –x $a ]
then
echo “file is executable”
else
echo “we made it executable”
chmod 777 $a
fi
5.  WAP to take string as command line argument and reverse it.
echo “ Enter the string you want to reverse”
read string
len=`echo –n $string |wc –c`
echo “No. of characteristics : $len”
while test $len –gt 0
do
rev =$rev `echo $string | cut –c $len`
len=`expr $len – 1`
done
echo “The reverse string is : $rev”
6. WAP which displays the following menu and executes the option selected by user:
1. ls
2. pwd
3. ls –l
4. ps -fe
printf “\n\t 1. ls \n\t 2. pwd \n\t 3. ls -l \n\t 4.ps -fe\n\t “
echo “Enter your choice”
read a
case $a in
1) ls ;;
2)pwd ;;
3)ls -l;;
4)ps -fe;;
*)echo “Enter correct value”
esac
7. WAP to print 10 9 8 7 6 5 4 3 2 1 .
for i in 10 9 8 7 6 5 4 3 2 1
do
echo “$i”
done
8. WAP that prompts user for a starting value & counts down from there.
echo “Enter any value for countdown”
read a
while test $a -gt 0
do
echo $a
a=`expr $a – 1`
done
9. WAP that accepts initial value as a command line argument for ex.         If user writes contdown2 12 it should display the value from 12 to 1.
a=$1
while test $a -gt 0
do
echo $a
a=`expr $a – 1`
done
10. WAP that replaces all “*.txt” file names with “*.txt.old” in the current.
for i in *.txt
do
mv “ $i ” “ $i”.old
done

  1. Create a data file called employee in the format given below:
a. EmpCode    Character
b. EmpName   Character
c. Grade           Character
d. Years of experience    Numeric
e. Basic Pay     Numeric
$vi employee
A001           ARJUN       E1      01      12000.00
A006           Anand         E1      01      12450.00
A010           Rajesh         E2      03      14500.00
A002           Mohan         E2      02      13000.00
A005           John             E2      01      14500.00
A009           Denial SmithE2      04      17500.00
A004           Williams      E1      01      12000.00
2.)  Sort the file on EmpCode.
$cut –f1 employee | sort
  1. Sort the file on EmpName.
$cut –f2 employee | sort
  1. Sort the file on
(i) Decreasing order of basic pay
(ii) Increasing order of years of experience.
(i) $cut –f5 employee | sort –r
(ii) $cut –f4 employee | sort
  1. Display the number of employees whose details are included in the file.
wc –l
  1. Display all records with ‘smith’ a part of employee name.
cut –f2 –d “ ” employee | grep ‘^[smith]’ | wc –l
  1. Display all records with EmpName starting with ‘B’.
$cut –f2 employee | grep ‘^[B]’ | wc –l
  1. Display the records on Employees whose grade is E2 and have work experience of 2 to 5 years.
$cut –f3 employee | grep ‘[*2] | cut –f4 employee | grep ‘[2-5]’
  1. Store in ‘file 1’ the names of all employees whose basic pay is between 10000 and 15000.
$cut –f2,5 employee | grep ‘[10000-15000]’ > rec
  1. Display records of all employees who are not in grade E2.
$cut –f3 employee | grep ‘[*1]’
  1. WAP that asks for the user’s favorite color and if it is not ‘red’ informs him that it is wrong answer, waits for 3 sec, clears the screen and asks the question again.
b=red
echo “enter your favorite color : ”
read a
c=`expr $a = $b`
if test $c –eq 1
then
echo “your favorite color is : $a”
else
while test $c –eq 0
do
echo “ your answer is wrong”
sleep 3
clear
echo “enter your favorite color :”
read a
c=`expr $a = $b`
done
echo “your favorite color is : $a”
fi
  1. WAP that reads each line of a target file, then writes the line back to stdout, but with an extra blank line following. This has the effect of a double spacing the file. Include all necessary code whether the script gets the necessary command line argument (a filename), and whether the specified file exists. When the script runs correctly, modify it to triple-space the target file. Finally, write a script to remove all blank lines from the target file, single-spacing it.
if test –e $1 –a –f  $1
then
t=’tty’
exec < $1
while read line
do
echo $line
echo
done
exec > $2
exec < $1
while read line
do
echo $line
echo
echo
done
fi
  1. WAP that echoes itself to stdout, but backwards.
echo “enter your text”
cat > f1
rev f1 | cat > f2
cat f2
  1. Write a script function that determines if an argument passed to it is an integer or a string. The function will return TRUE(0) if passed an integer, and FALSE(1) if passed a string.
[HINT: what does the following expression returns when $1 is not an integer? expr $1 + 0
function check()
{
expr $1 + 0
if [ $? –eq 0 ]
then
echo “argument is integer”
else
echo “not integer”
fi
}
check
7.  A “lucky number” is one whose individual digits add upto 7, in successive additions. For ex. 62431 is a lucky no. (6+4+2+3+1=16, 1+6=7). Find all “lucky numbers” between 1000 and 10000.
echo “lucky number between 1000 to 10000:”
a=1001
while [ $a –lt 10000]
do
n=$a
sd=0
sum=0
while [ $n –gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10))
sum=$(( $sum + $sd))
done
n=$sum
sumdig=0
while [ $n –gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10))
sumdig=$(( $sumdig + $sd))
done
if [ $sumdig –eg 10 ]
then
n=$sumdig
sd=$(( $n % 10 ))
n=$(( $n / 10))
sumdig=$(( $sumdig + $sd))
fi
if [ $sumdig –eq 7]
then
echo “lucky Number : $a”
fi
a=$(( $a + 1))
done
8. Solve a “quadratic” equation of the form Ax^2+Bx +C=0. Have a script take as arguments the coefficients A, B and C and returns the solutions to four decimal places.
[HINT: pipe the coefficients to bc, using the well known formula, x= (-B + /- sqrt (B^2 – 4AC))/2A].
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main
{
int a, b, c, deter;
float r1,r2;
clrscr();
printf(“Enter coefficients A,B and C (Integers)”);
scanf(“%d%d%d”, &a,&b,&c);
deter=(b*b)-(4*a*c);
if(deter<0)
{
printf(“Roots are imaginary”);
}
if(deter>0)
{
printf(“Roots are real as follows”);
r1=(-b+sqrt(deter))/2*a;
r2=(-b-sqrt(deter))/2*a;
printf(“% .4f \n % .4f”, r1,r2);
}
if(deter=0)
{
printf(“Roots are equal”);
r2=-b/2*a;
printf(“% .4f\n% .4f”,r1,r2);
}
getch();
}
1) Write a shell script to ask your name, program name and enrollment number and print it on the screen.
Echo “Enter your name:”
Read Name
Echo “Enter your program name:”
Read Prog
Echo “Enter your enrollment number:”
Read Enroll
Clear
Echo “Details you entered”
Echo Name: $Name
Echo Program Name: $Prog
Echo Enrolment Number: $Enroll
2) Write a shell script to find the sum, the average and the product of the four integers entered
Echo “Enter four integers with space between”
Read a b c d
Sum =`expr $a + $b + $c + $d`
Avg =`expr $sum / 4`
Dec =`expr $sum % 4`
Dec =`expr \ ($dec \* 1000 \) / 4`
Product =`expr $a \* $b \* $c \* $d`
Echo Sum = $sum
Echo Average = $avg. $dec
Echo Product = $product
3) Write a shell program to exchange the values of two variables
Echo “Enter value for a:”
Read a
Echo “Enter value for b:”
Read b
Clear
Echo “Values of variables before swapping”
Echo A = $a
Echo B = $b
Echo Values of variables after swapping
a = `expr $a + $b`
b = `expr $a – $b`
a = `expr $a – $b`
Echo A = $a
Echo B = $b
4) Find the lines containing a number in a file
Echo “Enter filename”
Read filename
Grep [0-9] $filename
5) Write a shell script to display the digits which are in odd position in a given 5 digit number
Echo “Enter a 5 digit number”
Read num
n = 1
while [ $n -le 5 ]
do
a = `Echo $num | cut -c $n`
Echo $a
n = `expr $n + 2`
done
6) Write a shell program to reverse the digits of five digit integer
Echo “Enter a 5 digit number”
Read num
n = $num
rev=0
while [ $num -ne 0 ]
do
r = `expr $num % 10`
rev = `expr $rev \* 10 + $r`
num = `expr $num / 10`
done
Echo “Reverse of $n is $rev”
7) Write a shell script to find the largest among the 3 given numbers
Echo “Enter 3 numbers with spaces in between”
Read a b c
1 = $a
if [ $b -gt $l ]
then
l = $b
fi
if [ $c -gt $l ]
then
l = $c
fi
Echo “Largest of $a $b $c is $l”
8) Write a shell program to search for a given number from the list of numbers provided using binary search method
Echo “Enter array limit”
Read limit
Echo “Enter elements”
n = 1
while [ $n -le $limit ]
do
Read num
eval arr$n = $num
n = `expr $n + 1`
done
Echo “Enter key element”
Read key
low = 1
high = $n
found = 0
while [ $found -eq 0 -a $high -gt $low ]
do
mid = `expr \( $low + $high \) / 2`
eval t = \$arr$mid
if [ $key -eq $t ]
then
found = 1
elif [ $key -lt $t ]
then
high = `expr $mid – 1`
else
low = `expr $mid + 1`
fi
done
if [ $found -eq 0 ]
then
Echo “Unsuccessful search”
else
Echo “Successful search”
fi
9) Write a shell program to concatenate two strings and find the length of the resultant string
Echo “Enter first string:”
Read s1
Echo “Enter second string:”
Read s2
s3 = $s1$s2
len = `Echo $s3 | wc -c`
len = `expr $len – 1`
Echo “Concatenated string is $s3 of length $len ”
10) Write a shell program to find the position of substring in given string
Echo “Enter main string:”
Read main
l1 = `Echo $main | wc -c`
l1 = `expr $l1 – 1`
Echo “Enter sub string:”
Read sub
l2 = `Echo $sub | wc -c`
l2 = `expr $l2 – 1`
n = 1
m = 1
pos = 0
while [ $n -le $l1 ]
do
a = `Echo $main | cut -c $n`
b = `Echo $sub | cut -c $m`
if [ $a = $b ]
then
n = `expr $n + 1`
m = `expr $m + 1`
pos = `expr $n – $l2`
r = `expr $m – 1`
if [ $r -eq $l2 ]
then
break
fi
else
pos = 0
m = 1
n = `expr $n + 1`
fi
done
Echo “Position of sub string in main string is $pos”
11) Write a shell program to display the alternate digits in a given 7 digit number starting from the first digit
Echo “Enter a 7 digit number”
Read num
n = 1
while [ $n -le 7 ]
do
a = `Echo $num | cut -c $n`
Echo $a
n = `expr $n + 2`
done
12) Write a shell program to find the gcd for the 2 given numbers
Echo “Enter two numbers with space in between”
Read a b
m = $a
if [ $b -lt $m ]
then
m = $b
fi
while [ $m -ne 0 ]
do
x = `expr $a % $m`
y = `expr $b % $m`
if [ $x -eq 0 -a $y -eq 0 ]
then
Echo “gcd of $a and $b is $m”
break
fi
m = `expr $m – 1`
done
13) Write a shell program to check whether a given string is palindrome or not.
Echo “Enter a string to be entered:”
Read str
Echo
len = `Echo $str | wc -c`
len = `expr $len – 1`
i = 1
j = `expr $len / 2`
while test $i -le $j
do
k = `Echo $str | cut -c $i`
l = `Echo $str | cut -c $len`
if test $k != $l
then
Echo “String is not palindrome”
exit
fi
i = `expr $i + 1`
len = `expr $len – 1`
done
Echo “String is palindrome”
14) Write a shell program to find the sum of the series sum=1+1/2+…+1/n
Echo “Enter a number”
Read n
i = 1
sum = 0
while [ $i -le $n ]
do
sum = `expr $sum + \ ( 10000 / $i \)`
i = `expr $i + 1`
done
Echo “Sum n series is”
i = 1
while [ $i -le 5 ]
do
a = `Echo $sum | cut -c $i`
Echo -e “$a\c”
if [ $i -eq 1 ]
then
Echo -e “.\c”
fi
i = `expr $i + 1`
done
15) Write a shell script to find the smallest of three numbers
Echo “Enter 3 numbers with spaces in between”
Read a b c
s = $a
if [ $b -lt $s ]
then
s = $b
fi
if [ $c -lt $s ]
then
s = $c
fi
Echo “Smallest of $a $b $c is $s”
16) Write a shell program to add, subtract and multiply the 2 given numbers passed as command line arguments
add = `expr $1 + $2`
sub = `expr $1 – $2`
mul = `expr $1 \* $2`
Echo “Addition of $1 and $2 is $add”
Echo “Subtraction of $2 from $1 is $sub”
Echo “Multiplication of $1 and $2 is $mul”
17) Write a shell program to convert all the contents into the uppercase in a particular file
Echo “Enter the filename”
Read filename
Echo “Contents of $filename before converting to uppercase”
Echo —————————————————-
cat $filename
Echo —————————————————-
Echo “Contents of $filename after converting to uppercase”
Echo —————————————————
tr ‘[a-z]‘ ‘[A-Z]‘ < $filename Echo ————————————————— 18) Write a shell program to count the characters, count the lines and the words in a particular file Echo “Enter the filename” Read file w = `cat $file | wc -w` c = `cat $file | wc -c` l = `grep -c “.” $file` Echo “Number of characters in $file is $c” Echo “Number of words in $file is $w” Echo “Number of lines in $file is $l” 19) Write a shell program to concatenate the contents of 2 files Echo “Enter first filename” Read first Echo “Enter second filename” Read second cat $first > third
cat $second >> third
Echo “After concatenation of contents of entered two files”
Echo —————————————————-
cat third | more
Echo —————————————————-
20) Write a shell program to count number of words, characters, white spaces and special symbols in a given text
Echo “Enter a text”
Read text
w = `Echo $text | wc -w`
w = `expr $w`
c = `Echo $text | wc -c`
c = `expr $c – 1`
s = 0
alpha = 0
j = ` `
n = 1
while [ $n -le $c ]
do
ch = `Echo $text | cut -c $n`
if test $ch = $j
then
s = `expr $s + 1`
fi
case $ch in
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) alpha=`expr $alpha + 1`;;
esac
n = `expr $n + 1`
done
special = `expr $c – $s – $alpha`
Echo “Words = $w”
Echo “Characters = $c”
Echo “Spaces = $s”
Echo “Special symbols = $special”
24) Write a shell program to find factorial of given number
Echo “Enter a number”
Read n
fact = 1
i = 1
while [ $i -le $n ]
do
fact = `expr $fact \* $i`
i = `expr $i + 1`
done
Echo “Factorial of $n is $fact”
25) Write a shell script to find the average of the numbers entered in command line
n = $#
sum = 0
for i in $*
do
sum = `expr $sum + $i`
done
avg = `expr $sum / $n`
Echo “Average=$avg”
26) Write a shell script to sort the given numbers in descending order using Bubble sort
Echo
i = 1
k = 1
Echo “Enter no. of integers to be sorted”
Read n
Echo “Enter the numbers”
while [ $i -le $n ]
do
Read num
x[$k] = `expr $num`
i = `expr $i + 1`
k = `expr $k + 1`
done
x[$k] = 0
k = 1
Echo “The number you have entered are”
while [ ${x[$k]} -ne 0 ]
do
Echo “${x[$k]}”
k = `expr $k + 1`
done
k = 1
while [ $k -le $n ]
do
j = 1
while [ $j -lt $n ]
do
y = `expr $j + 1`
if [ ${x[$j]} -gt ${x[$y]} ]
then
temp = `expr ${x[$j]}`
x[$j] = `expr ${x[$y]}`
x[$y] = `expr $temp`
fi
j = `expr $j + 1`
done
k = `expr $k + 1`
done
k = 1
Echo “Number in sorted order…”
while [ ${x[$k]} -ne 0 ]
do
Echo “${x[$k]}”
k = `expr $k + 1`
done
27) Write a shell program to find the sum of all the digits in a given 5 digit number
Echo “Enter a 5 digit number”
Read num
sum = 0
while [ $num -ne 0 ]
do
r = `expr $num % 10`
sum = `expr $sum + $r`
num = `expr $num / 10`
done
Echo “sum = $sum”
28) Write a shell script to generate fibonacci series
Echo “how many fibonacci numbers do u want “
Read limit
a = 0
b = 1
d = 1
Echo “————————————————————-”
Echo -n $a
Echo -n ” “
while test $d -le $limit
do
c = `expr ${a} + ${b}`
Echo -n $c
Echo -n ” “
b = $a
a = $c
d = `expr $d + 1`
done
29) Shell Script to check whether given year is leap year or not
Echo -n “Enter the year(yyyy) to find leap year :- “
Read year
d = `expr $year % 4`
b = `expr $year % 100`
c = `expr $year % 400`
if [ $d -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
Echo “year is leap year”
else
Echo “not leap year”
fi
30) Shell Script to print alternate digit when a 7 digit number is passed
Echo -n “Enter a 7 digit number:- “
Read number
len = `echo $number | wc -c`
flag = 1
while test $flag -le $len
do
Echo $number | cut -c$flag
flag = `expr $flag + 2`
done
31) Shell script to find average of number at given command line
total = 0
count = $#
for i #or u can append ( in $*) to get same result.
do
total = `expr $total + $i`
done
avg1 = `expr $total / $count`
avg2 = `expr $total % $count`
avg2 = `expr $avg2 \* 100 / $count`
Echo “The Average is :- $avg1.$avg2″
32) Shell Script to reverse a inputted string and show it
Echo -n “enter the string u want to reverse:-”
Read string
len = `Echo -n $string |wc -c`
Echo “no of character is:- $len”
while test $len -gt 0
do
rev = $rev`Echo $string |cut -c $len`
len = `expr $len – 1`
done
Echo “the reverse string is:-$rev “
33) Shell script to find occurrence of particular digit in inputted number
Echo -n “enter any number:-”
Read number
Echo -n “which digit number do u want to count:-”
Read digit
len = `echo -n $number |wc -c`
Echo “the length of number is:-$len”
count = 0
while test $len -gt 0
do
flag = `Echo -n $number |cut -c $len`
if test $flag -eq $digit
then
count = `expr $count + 1`
fi
len = `expr $len – 1`
done
Echo “|$digit| occurred |$count| times in number ($number)”
34) Shell Script to find whether number given is even or odd
Echo -n “enter any integer number to find even and odd :-”
Read number
rem = `expr $number % 2`
if test $rem -eq 0
then
Echo “number is even”
else
Echo “number is odd”
fi
35) write shell script to generate fibonacci series
#!/bin/bash
#shell script to generate fibonacci series
echo “how many fibonacci numbers do u want “
read limit
a=0
b=1
d=1
echo “————————————————————-”
echo -n $a
echo -n ” “
while test $d -le $limit
do
c=`expr ${a} + ${b}`
echo -n $c
echo -n ” “
b=$a
a=$c
d=`expr $d + 1`
done
36) write shell script to find wheather a particular year is a leap year or not
#!/bin/bash
#shell script to find wheather a particular year is a leap year or not
echo -n “Enter the year(yyyy) to find leap year :- “
read year
d=`expr $year % 4`
b=`expr $year % 100`
c=`expr $year % 400`
if [ $d -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
echo “year is leap year”
else
echo “not leap year”
fi
37) write shell script to print alternate digits when a 7 digit number is passed as input
#!/bin/bash
#shell script to print alternate digits when a 7 digit number is passed as input
echo -n “Enter a 7 digit number:- “
read number
len=`echo $number | wc -c`
flag=1
while test $flag -le $len
do
echo $number | cut -c$flag
flag=`expr $flag + 2`
done
38) write shell script to find average of numbers given at command line
#Tue May 28 11:12:41 MUT 2002
total=0
count=$#
for i #or u can append ( in $*) to get same result.
do
total=`expr $total + $i`
done
avg1=`expr $total / $count`
avg2=`expr $total % $count`
avg2=`expr $avg2 \* 100 / $count`
echo “The Average is :- $avg1.$avg2″
39) write shell script to find wheather a given word is palindrome or not
#Sun Jun 9 12:06:14 MUT 2002 –By Mukesh
clear
echo -n “enter the name :-”
read name
len=`echo -n $name | wc -c`
echo “Length of the name is :-”$len
while [ $len -gt 0 ]
do
rev=$rev`echo $name | cut -c$len`
len=`expr $len – 1`
done
echo “Reverse of the name is :-”$rev
if [ $name = $rev ]
then echo “It is a palindrome”
else
echo “It is not a palindrome”
fi
40) write shell script to reverse a inputed string and show it.
#!/bin/bash
#shell script to reverse a inputed string and show it.
echo -n “enter the string u want to reverse:-”
read string
len=`echo -n $string |wc -c`
echo “no of character is:- $len”
while test $len -gt 0
do
rev=$rev`echo $string |cut -c $len`
len=`expr $len – 1`
done
echo “the reverse string is:-$rev “
41) write shell script to count occurence of a perticular digit in inputted number.
#!/bin/bash
#shell script to count occurence of a perticular digit in inputted number.
echo -n “enter any number:-”
read number
echo -n “which digit number do u want to count:-”
read digit
len=`echo -n $number |wc -c`
echo “the length of number is:-$len”
count=0
while test $len -gt 0
do
flag=`echo -n $number |cut -c $len`
if test $flag -eq $digit
then
count=`expr $count + 1`
fi
len=`expr $len – 1`
done
echo “|$digit| occured |$count| times in number ($number)”
42) write shell script to find even or odd.
#!/bin/bash
#shell script to find even or odd.
echo -n “enter any integer number to find even and odd :-”
read number
rem=`expr $number % 2`
if test $rem -eq 0
then
echo “number is even”
else
echo “number is odd”
fi


No comments:

Post a Comment