Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
0 of .
Results for:
P. 1
OSLAB

# OSLAB

Ratings: (0)|Views: 15|Likes:

Published by: api-3701797 on Oct 14, 2008

### Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

ASSIGNMENT I
Problem 1:-
Write a shell script that will assign the value Hi! to a variable called all. Then print
the value of all with no quotes, with single quotes and with double quotes.
Solution:

all=Hi!
echo -e "With no quotes :\$all\n"
echo -e "With single quotes :'\$all'\n"
echo -e "with double quotes :\"\$all\"\n"

Output:
[root@localhost ~]# ./prob1.sh

With no quotes :Hi!
With single quotes :'Hi!'
with double quotes :"Hi!"

Problem 2:-

Write a shell script that has two user created variables, var1 and var2.Ask for
values of the variable from the user and take in any values of the two variables and
then print them as:-
i)values of var1 followed by values of var2 separated by a comma
ii) values of var2 followed by values of var1 separated by the word \u201cand\u201d.

Solution:

echo -e "Enter variable 1 :\c"
if [ -z var1 ] ; then

echo -e "You did not entered variable. Exit\n" ; exit 1
fi
echo -e "Enter variable 2 :\c"
if [ -z var2 ] ; then

echo -e "You did not entered variable. Exit\n" ; exit 2
fi
echo -e "i) \$var1 , \$var2 "
echo -e "ii)\$var2 and \$var1 "

Output:

[root@localhost ~]# ./prob2.sh
Enter variable 1 :var1
Enter variable 2 :var2
i) var1 , var2

ii)var2 and var1

Problem 3:-
Write a shell script that reads command line arguments and prints the number of
arguments. Given this sequence of words as command line arguments \u201cTo the OS
Laboratory at Alpha Lab Welcome\u201d, the script should echo back the information that
there are 8 arguments. Additionally it should echo back some of the arguments in the
following order:
\u201cWelcome To the alpha Lab Os Laboratory\u201d.

Solution:
echo -e "Number of arguments :\$#"
echo -e "\$8 \$1 \$2 \$6 \$7 \$3 \$4"
Output:
[root@localhost ~]# ./prob3.sh To the OS Laboratory at Alpha Lab Welcome

Welcome To the alpha Lab Os Laboratory

Problem 4:
Write a shell script that accepts a filename as an argument and performs the following
tasks for that file:
a. searches all possible directories and prints the pathname if the file exists otherwise
prints an error message and exits.
b. prints the access rights of the file.
c. prints the size of the file.
d. prints the date of creation and the last date of modification of the file.

Solution:
if [ \$# -eq 0 -o \$# -gt 1 ] ; then

echo -e "This program takes only one argument" ; exit 1
fi
if [ -z "\$1" ] ; then

echo -e "Filename Null. Exit\n" ; exit 2
fi
echo -e "Searching all possible directories....\n"
find . -name "\$1" 2>/dev/null >temp
if [ `cat temp | wc -l ` -eq 0 ] ; then

echo -e "No such file exists.\n" ; exit 3
fi
echo -e "

Path
Access Rights Size Modification Date\n"

for var in `cat temp` ; do
aright=`ls -l \$var | tr -s ' ' |cut -d" " -f1`
size=`ls -l \$var | tr -s ' ' |cut -d" " -f5`
mdate=`ls -l \$var | tr -s ' ' |cut -d" " -f6,7`
printf "%19s %13s %6d %16s\n" \$var \$aright \$size "\$mdate"

done
Output:
./prob4.sh soumya

Searching all possible directories....
Path
Access Rights Size Modification Date
./soumya -rw-r--r-- 45
Feb 7

Problem 5:- Write a shell script that accepts a file name as input .The program then
asks for a string of characters(i.e. any word ) from the user .The file will be
searched to find whether it contains the given word .If it does, the program will
display the no. of occurrences of the word. The program should also display the line
numbers where the word occurred .If the file does not contain the word, and then an
appropriate error message will be displayed.

Solution:

echo Enter a File Name
if [ -f \$fname ]

then
echo Enter a Pattern to be Recognized
if ! grep \$pattern -n \$fname

then echo Pattern NOT Exists
fi
else
echo File Does NOT Exist
fi
Output:

[root@localhost ~]# cat soumya
chloroform
aerobic
olfactory
caesious
Euonia
[root@localhost ~]# ./prob5.sh
Enter a File Name
soumya
Enter a Pattern to be Recognized
chloroform
1:chloroform

## Activity (1)

You've already reviewed this. Edit your review.