Ilovepdf Merged
Ilovepdf Merged
Unix Journal
1) Write a shell script to scans the name of the command and executes it.
Script:
echo "Enter command
name" read cmd $cmd
O/P:-
Enter command name
cal
February 2016
Su Mo Tu We Th Fr Sa
1 2 3 456
78 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
2) Write a shell script Which works like calculator and performs below
operations Addition , Subtract ,Division ,Multiplication. Script : i="y"
while [ $i = "y" ] do
echo " Enter one no."
read n1
echo "Enter second no."
read n2 echo
"1.Addition" echo
"2.Subtraction" echo
"3.Multiplication" echo
"4.Division" echo
"Enter your choice" read
ch case $ch in
1) sum=`expr $n1 +
$n2` echo "Sum
="$sum;; 2)sum=`expr
$n1 - $n2` echo "Sub =
"$sum;; 3)sum=`expr
$n1 \* $n2` echo
"Mul = "$sum;;
4)sum=`expr $n1 /
echo "The given string has $vowCount vowels, $consCount consonants and
$numCount numbers in it." O/P:
Enter a line of text: eeva sh1
The given string has 3 vowels, 3 consonants and 1 numbers in it.
8) Write a shell script to display name and size of the files on the given
path.
Script: echo "Enter the full path to
the file :" read file
filesize=$(ls -lh $file | awk '{print $5 " " $9}')
echo "$file has a size of $filesize"
O/P:
Enter the full path to the file :
/home/hp/
/home/hp/ has a size of
70 cmb_file.txt
1.7K cmb_file1.txt
210 cmb_file2.txt
39 cmp1.txt
9) Write a menu driven shell script to create and delete a file which will
accept two command line arguments (file name and create / delete option).
Script:
case $1 in
"--create") echo "Creating new
file $2"
#echo
touch $2
;;
10) Write a shell script to count number of lines words and characters of a
string and of a file.
Script: echo -n "Enter a
String : " # Taking input
from user read text
# Counting words
word=$(echo -n "$text" | wc -
w) echo "No of Word :"$word #
Counting characters char=$
(echo -n "$text" | wc -c)
O/P:
Enter a String : count characters
No of Word :2
no of char :16
File name : /home/hp/demo.txt
Number of lines: 17
Number of words: 16
10) Write a shell script to which represents the ways to declare and access
array. Script:
# To declare static Array
arr=(prachi poonam 1 richa ronak roocha)
# To print particular
element echo ${arr[3]}
echo ${arr[1]}
# To print elements in
range echo ${arr[@]:1:4}
echo ${arr[@]:2:3}
echo ${arr[5]:1:3}
# Size of an Array
echo ${#arr[@]}
echo ${#arr[*]}
# Search in Array
echo ${arr[@]/*[aA]*/}
# Replacing Substring
Temporary echo ${arr[@]//a/A}
echo ${arr[@]}
echo ${arr[0]//r/R}
Script:
# Take input as binary number echo
"Enter Binary Number -"
read n
local i=0
local num=0
# Function Call
binaryCon
O/P:
Enter Binary Number
101
Resultant Decimal Number
5
11) Execute commands for below listed tasks.
Create a file named eg_grep.sh. Write the content related to UNIX in the
same and use that file to perform following command.
a) Display list of all the files which have word “UNIX” in it.
$grep -l "UNIX" *
b) Search for the patter “UNIX” in a file and display the lines which
does not have the given pattern.
$grep -v "UNIX" eg_grep.txt
c) Display the lines of a file which ends with “labs.”
$grep "labs.$" eg_grep.txt
d) Parenthesize first letter of such words which have first capital letter
in that word.
$sed 's/\(\b[A-Z]\)/\(\1\)/g' eg_grep.txt
e) Duplicate the line in which string/word is replaced.
$sed 's/is/IS/p' eg_grep.txt
f) Delete 2 to 4 line of the given file. $sed '2,4d' eg_grep.txt
5) -v Inverting the pattern match: This prints out all the lines
that do not matches the pattern
grep -v "unix" eg_grep.txt
To print the first item along with the row number(NR) separated with ” –
“
awk '{print NR "- " $1 }' employee.txt