Experiment 1 (Os)
Experiment 1 (Os)
Experiment 1 (Os)
SITE
Experiment: 1
Shell programming
a. Identify the command to print the home directory of each user.
b. Develop an interactive grep script that asks for a word and a file name and then finds the
number of occurrences of that word in the file.
c. Write a shell script that takes a command line argument and reports on whether it is
directory, a file, or something else.
d. Write a shell script that determines the period for which a specified user is working on the
system.
e. Write an interactive file-handling shell program. Let it offer the user the choice of copying,
removing, renaming, or linking files. Once the user has made a choice, have the program ask
the user for the necessary information, such as the file name, new name and so on.
f. Write a shell script that displays a list of all the files in the current directory to which the user
has read, write and execute permissions.
a.)
CODE:
OUTPUT:
b.)
CODE:
echo "Enter file name with extension:";
read file;
echo "Enter word to be counted:";
read word;
grep -o $word $file | wc -l;
OUTPUT:
c.)
CODE:
echo "Enter arguement:";
read PASSED;
if [ -d "${PASSED}" ] ;
then
echo "$PASSED is a directory";
else
if [ -f "${PASSED}" ];
then
echo "${PASSED} is a file";
else
echo "${PASSED} is not valid";
exit 1
fi
OUTPUT:
d.)
CODE:
t1=`who | grep "$1" | tr -s " " | cut -d " " -f 5 | cut -d ":" -f 1 `
t2=`who | grep "$1" | tr -s " " | cut -d " " -f 5 | cut -d ":" -f 2 `
t1=`expr $t1 \* 60 `
min1=`expr $t1 + $t2`
d1=`date +%H`
d2=`date +%M`
d1=`expr $d1 \* 60`
min2=`expr $d1 + $d2`
sub=`expr $min2 - $min1`
p=`expr $min2 - $min1`
p=`expr $p / 60`
p1=`expr $min2 - $min1`
p1=`expr $p1 % 60`
echo " The user $1 has been working since : $pr Hrs $pr1 minutes "
OUTPUT:
e.)
CODE:
while true
do
echo "*******MENU*********"
echo "
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files.
press [CTRL+C] TO EXIT"
echo "enter your choice ";
read ch;
case "$ch" in
1 )
echo "The list of file names."
ls -l || echo "These are file";;
2 ) echo "Enter the old filename."
read ofile
echo "Enter the new file name."
read nfile
cp $ofile $nfile && echo "Copied sucessfully." || echo
"Copied is not possible." ;;
3 ) echo "Enter the file name to remove."
read rfile
rm -f $rfile && echo "Successfully removed." ;;
4 ) echo "Enter the old file name."
read ofile
echo "Enter the new file name."
read nfile
mv $ofile $nfile && echo "The file $ofile name renamed to
$nfile." || echo "You can't Rename the file.".;;
5 ) echo "Enter the original filename."
read ofile
echo "Enter the new filename to link a file."
read lfileln $ofile $lfile && echo "Creat the linking file
Sccessfully." || echo "You can't Link the file.";;
* )
echo "Invalid option."
echo " Enter correct choice."
esac
done
OUTPUT:
f.)
CODE:
echo "List of Files which have Read, Write and Execute
Permissions in Current Directory"
for file in *
do
if [ -r $file -a -w $file -a -x $file ]
then
echo $file
fi
done