8A) Shell script that accepts two file names as arguments, checks if the permissions for these files are identical and displays their permissions.[VTU SS&OS]

vtu ss & os 8a
Code:

Explanation:
This program reads two valid filenames as input. check permissions of both files. if equal, then we print "EQUAL" Otherwise, we print both permissions seperately.

echo "Enter the name of first file: "
read file1
echo "Enter the name of second file: "
read file2

Read two file names from terminal using read command of shell scipting.
store it in two variables file1 and file2.

fp1=`ls -l $file1|cut -c 2-10`
fp2=`ls -l $file2|cut -c 2-10`

we List the file attributes using 'ls -l' command. this will create 7 fields, starting from (file id, permissions, owner, group owner, links...etc) we only want to cut permissions out of it. (remember permission was the second field in "ls -l, (byte 2-10)" since there are 9 characters in permission field. (rwxrwxrwx).
So we cut permissions field only, from ls -l's output. and store it in two variables, fp1 and fp2.
remmber its ` symbol used above. not single quotes ' symbol.

if [ $fp1 = $fp2 ]
then
echo "Files $file1 and $file2 have same permission $fp1"
else
echo "Permission of first file $file1:"
echo $fp1
echo "Permission of second file $file2:"
echo $fp2
fi

if file permissions of both files match, we print the appropriate message,
else print both pemrissions seperately.
fi ends if condition.

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!