Welcome

UNIX & SHELL PROGRAMMING LAB ASSIGNMENT 4TH SEM SOLUTIONS

1. Use the echo command to display the line UNIX is fun to learn
Ans :- echo "UNIX is fun to learn"

Redirect the displayed line to a file. Append the outputs of the commands to show the users logged into your system, your login account and the current date in the dd-mm-yyyy format to the file.
Ans :-who -u | cat > myfile
date +'%d-%m-%Y' | cat > myfile

2. Assuming you have a directory containing the following files:
sprite, cola, choco, orange, book, lemon, lotus, apple
Use the ls command to list only those files that
a) consist of four letters
b) begin with the letter c
c) end with the letter e
d) have the second letter o
a)Ans :- ls ????
b)Ans :- c*.*
c)Ans :- *c.*
d)Ans :- ?o.*

3. For a file named myfile in the working directory, do the following:
a) give everyone permission to read myfile. No other privilege is to be changed
b) allow the owner and group members to read and write the file. All privileges are to be removed for everyone else.
c) remove write privileges from everyone except the owner
d) allow the owner and group members to execute myfile and give only the owner permission to read or write to it.
a)Ans :- chmod a+r myfile
b)Ans:- chmod 660 myfile
c)Ans:- chmod go-w myfile
d)Ans:- chmod 710 myfile , chmod ug=x,u+rw myfile

4. For a directory myfolder in your working directory, do the following:
a) allow everyone to list files in myfolder. No other privileges are to be changed.
b) allow the owner to and group members to list, remove or add files. All privileges are to be removed from everyone else.
c) give write privileges to everyone except the owner
d) allow the owner and group members to execute myfolder. Only the owner gets read or write permission.
a)Ans:- chmod a+rx myfolder
b)Ans:-chmod 770 myfolder
c)Ans:- chmod go+w myfolder
d)Ans:- chmod 710 my folder

5. Put a long-running process in the background and check the accuracy of the sleep command.
Ans:- date;sleep10;date$

6. The ls command with the –R option lists the files in the specified directory and also subdirectories, if any. Use this with a suitable pip-and-filter arrangement to determine whether a file exists in the account logged in.
Ans:- echo “Enter the name to search :”
read file
dat =’ls-R $ Home 1 grep $ file’
if [-s “$ dat”]
then
echo “$file File exist”
else
echo “$file File not exist”
fi

7. Write an interactive shell using I/O redirection to accept input from file1, put stdout in file2 and stderr in file3.
Ans:- echo “Enter input file name:”
read f1
echo “Enter output file name:”
read f2
echo “Enter error file name:”
read f3
cat <$f1 , cat >$f2 , cat >$f3

8. Write a shell program to extract and sort all the users in the file system. Use the /etc/passwd file as input.
Ans:- first (with pipe and filter)
Cut –d ”:” –f1/etc/passwd | sort
Second (without pipe and filter)
Cut –d “:” –f1/etc/passwd>tmp sort<temp

9. Write a shell program to translate the /etc/passwd file into uppercase and translate
the “:” delimiter into tab characters.
Ans:- tr ”[a-z:]” “[A-Z:]” </etc/passwd

10. Write a shell program using if-the-else to test whether a variable name is a directory or a file.
Ans:- echo “Enter a name”
read name
if [-f $name]
then
echo “$name is a file”
then
echo “$name is a directory”
else
echo “$name is not a file nor a directory.”
fi

11. Write a shell program in which an infinite loop prompts a user for file names to be removed and remove the files. Use trap to exit when finished.
Ans:- trap ‘exit’ INT
While [true]
do
echo “Enter filename to be deleted (ctr+c to exit)”
read file
rm $file
done

12. Write a background command to edit all the files in the directory replacing the word while with until.
Ans:- replace while until __*s

13. Write a shell program to test for arguments arg1, arg2 and arg3. If they are not present, prompt the user for them.
Ans:- if [$ # -lt 3]
then
arg=’expr $ # +1’
while [$ arg –le 3]
do
echo “Enter argument number $arg:”
read val
set $* $val
arg=’expr $arg +1’
done
fi
echo “Argument on command line entered by you are:”
echo $*

14. Write a shell program that adds the integers 1 to 100 and displays the result with a suitable message.
Ans:- s=0
c=1
while [$c-le 100]
do
s=’expr $s + $c’
c=’expr $c + 1’
done
echo “Sum of 1 to 100 is $s”

15. Using awk, sum the file sizes in your working directory and print the results.
Ans:- awk ’ {total += length ($0)}
END ‘{Print total}’ * (sameline)

16. Using a case statement, write a shell program to read a command (eg., who, cal, ls, ps) from the user and execute it.
Ans:- echo “Enter any command:”
read com
case $com in
who ) who;;
cal) cal;;
ls) ls;;
ps) ps;;
*) echo “Invalid command”
esac

1 comment:

Leave Comments