In: Computer Science
Write a C Unix shell script called showperm that accepts two command line parameters. The first parameter should be an option flag, either r, w or x. The second parameter should be a file name. The script should indicate if the specified file access mode is turned on for the specified file, but it should display an error message if the file is not found. For example, if the user enters: showperm r thisfile the script should display “readable” or “not readable” depending on the current status of thisfile.
Please find the script to identify whether the file is not executbale/readable/writable.
Script:
#verify the arguments
if [ $# == 2 ]
then
#verify whether the first argument is
r/w/x
if [[ $1 == 'r' || $1 == 'w' || $1 == 'x' ]]
then
#verify the file is present
if [ -f $2 ]
then
if [ $1 == 'r'
]
then
#verify the file is readable
if [ -r $2 ]
then
echo "File is readable"
else
echo "File is unreadable"
fi
#verify the file is writable
elif [ $1 == 'w'
]
then
if [ -w $2 ]
then
echo "File is writable"
else
echo "File is unwritable"
fi
else
#verify the file is executable
if [ -x $2 ]
then
echo "File is executable"
else
echo "File is not executable"
fi
fi
else
echo "File: $2 not
found"
fi
else
echo "usgae: $0 [r|w|x]
<file-name>"
fi
else
echo "usgae: $0 [r|w|x]
<file-name>"
fi
Output:
USER> sh showperm.sh x dcler
File: dcler not found
USER> sh showperm.sh x
usgae: showperm.sh [r|w|x] <file-name>
USER> sh showperm.sh
usgae: showperm.sh [r|w|x] <file-name>
USER> sh showperm.sh r reg1
File is readable
USER> sh showperm.sh w reg1
File is writable
USER> sh showperm.sh x reg1
File is not executable