Você está na página 1de 35

Q10.

VI EDITOR, INODE
Q11. CREATING
ARCHIVES

GZIP,GUNZIP,BZIP2,BUNZIP2,TAR
Q 12 Practice
Exercise 3

1. Write a shell script to print addition of two numbers

2. Write a shell script to print subtraction of two numbers
3. Write a shell script to print multiplication of two numbers
4. Write a shell script to print division of two numbers
5. Write a shell script to perform all the functions together over
two numbers entered by user.
1-5.)

6. Perform following function by shell script : D=b+c/a

#!/bin/bash
hello()
{ a=`expr \$c / \$a`
D=`expr \$b + \$a`
echo "\$D"
}
a=2
b=15
c=2
echo "a=\$a b=\$b c=\$c"
hello

7. Perform following function by shell script: D=(b+c/a) * b

#!/bin/bash
hello()
{ a=`expr \$c / \$a`
e=`expr \$b + \$a`
D=`expr \$e \* \$b`
echo "\$D"
}
a=3
b=10
c=9
echo "a=\$a b=\$b c=\$c"
hello

8. Write the shell script to calculate average &amp; percentage of

marks entered by user.
#!/bin/bash
m1=84.5
m2=75.6
m3=50

sum=`echo \$m1 + \$m2 + \$m3 | bc -l`

avg=`echo \$sum / 3 | bc -l`
echo "percentage= \$sum"
echo "average= \$avg"

9. Write a shell script to print all the users who have currently
logged in

#!/bin/bash
echo "the users logged in are ";w

10. Write a shell script to display only the size of files from the
current working directory

#!/bin/bash
ls -lah|awk '{print \$5,\$9}'|column –t

Q 13. Practice
Exercise 4
1. Write a shell script to create a directory and files and
subdirectories

#!/bin/bash
mkdir dummy
mkdir -pv dummy/demo/file.txt

2. To find the largest of two numbers

#!/bin/bash
echo "enter two numbers"
if [ "\$a" -gt "\$b" ] #((\$a>\$b))
then
echo "\$a is greater"
else
echo "\$b is greater"
fi

3. To check whether a number is even or odd

#!/bin/bash
echo "enter a number"
if((\$a%2 == 0))
then
echo "\$a is even"

else
echo "\$a is odd"
fi

4. To print the values of environment variables

#!/bin/bash
echo "enter a environment variable in caps";read a
printenv \$a

#!/bin/bash
Str=`echo \$path`
F=\${str:0:7}
Echo \$F

6. To find largest of three given number.

#!/bin/bash
echo "enter 3 number"
if [ \$a -gt \$b -a \$a -gt \$c ];
then
echo "\$a is greatest"
elif [ \$b -gt \$c ];
then
echo "\$b is greatest"

else
echo "\$c is greatest"

fi

7. To greet the user according to the week of the day.

#!/bin/bash
var=\$(date +%A)
echo \$var
case "\$var" in
("Monday") echo "Oh! its monday";;
("Tuesday") echo "3 more days to go";;
("Wednesday") echo "eagerly waiting";;
("Thursday") echo "just day left for an adventure";;
("Saturday") echo "fly away";;
("Sunday") echo "last day to have fun";;
esac

8. To display the number of vowels and consonants in a string

#!/bin/bash
echo "enter a word"

vowels=\$(echo \$a|sed 's/[^aeiou]//g')

consonants=\$(echo \$a |sed 's/[aeiou]//g')

echo "\${#vowels} vowels"

echo "\${#consonants} consonants"

9. To perform arithmetic operation using case-- -- esac.

#!/bin/bash
echo "enter two numbers"
echo "enter your choice: \n 1.add \n 2.subtract \n 3. multiply \n 4. divide"

case \$c in
1) echo `expr \$a + \$b` ;;
2) echo `expr \$a - \$b` ;;
3) echo `expr \$a \* \$b` ;;
4) echo `expr \$a / \$b` ;;
esac

10. To count words, lines and characters in a file

#!/bin/bash
echo "enter filename or directory"
wc -lwm "\$a"

#!/bin/bash
echo \$SHELL

#!/bin/bash
who|sort -k 3,3n

13. To check whether the year entered is leap or not.

#!/bin/bash
echo "enter a year"
while [ \$a -ne 0 ]
do
if ((\$a%4==0));
then
echo "its a leap year"
else
echo "not a leap year"
fi
break;
done

14. To find factorial of a number.

#!/bin/bash
echo "enter a number"
fact=1
while [ \$a -ge 1 ]
do
fact=`expr \$fact \* \$a`
a=`expr \$a - 1`
done
echo "factorial is \$fact"

15. To generate a multiplication table

#!/bin/bash
echo "enter a number"
n=1
while [ \$n -ne 11 ]
do
echo "\$a * \$n = "`expr \$a \* \$n`
n=`expr \$n + 1`
done
Q14. ENVIRONMENT
VARIABLES AND LOCAL
VARIABLES

HOME,LANG,SHELL,USER,DISPLA
Y,VISUAL
Q 15. Concept of etc
dir. and special
permissions
/etc/passwd :-

Su :-
Suid :-

Sgid :-

Sticky bit :-
Q16. FILTER
COMMANDS
Aspell :-

Cut :-
Diff :-

Sort :-
Tail :-
Tr :-

Uniq :-
Wc :-

Q 17. Filter Commands

Grep :-
Sed :-

Awk :-
Q18.PRACTICLE
EXERCISE 5
1. Write a shell script to show following(Using Case construct)

option.

c. User name

3. To print the corresponding day using case control structure.

4. write 2a shell script to check whether the entered character
is in small case, capital case, digit or special symbol.
5. write a shell script to check whether the entered character

6. write a shell script to check whether the word start with

digit or alphabet.

7. Write a shell script to check whether the entered word is

one or two or three letter word.
8. Write a shell script to check whether the entered word
starts with small case vowel or capital vowel or ends with a
digit.

#!/bin/bash

echo "enter a string or file name"

ch=`echo \$a | cut -c 1`

len=`expr \$a|wc -c`

length=`expr \$len - 1`

then

case \$ch in

esac

case \$dh in

esac

else

case \$dh in

esac

fi

9. write a shell script to calculate simple interest for 3 sets of

principle, no. of year and rate of interest.
10. Write a shell script to find LCM and HCF of two
numbers.

11. Write a shell script to check whetherthe number

entered is prime or not.
#!/bin/bash
echo "enter a number"
flag=0
for (( i=2; i <=a/2; i++ ))
do
r=`expr \$a % \$i`
if [ \$r -eQ 0 ];
then
flag=1
break
fi
done
if [ \$flag -eQ 0 ] ;
then
echo "prime number"

else
echo "not a prime number"
fi

12. Write a shell script to count even and odd digit of a

number.
#!/bin/bash
echo "enter a number"
e=0
f=0
while [ \$a -gt 0 ]
do
d=`expr \$a % 10`
a=`expr \$a / 10`
if((\$d%2 == 0));
then
e=`expr \$e + 1`

else
f=`expr \$f + 1`

fi
done
echo "even count = \$e"
echo "odd count = \$f"

13. write a shell script to print prime number between 1

and 20.
14. write a shell script to check whether the given number
is Armstrong or not.
15. write a shell script to display number of vowels in a
string.
16. Write a shell script to count of files in a directory.
17. Write shell script to show various system configuration like
1) Currently logged user and his logname