In shell scripting, it is possible to test if a file exists using the \" e\" opt
ID: 3601805 • Letter: I
Question
In shell scripting, it is possible to test if a file exists using the " e" option. For example, if the file name is SF then a test such as if-e "SF .. is true if the file exist. 3 2/ It is required to write a shell script that asks the logged user to enter a file name. no such file, the script should display a messag the following: If the file type is executable, change the file permissions to grant the owner an execute and then run the file. If the file type is text, deny both owner and group from the write permission and then view the file contents. If the file type is directory, change its permissions to grant all users both read and execute and then view the directory contents recursively showing the type of each displayed item. For other file type, show the exact e saying that and terminate. If file exist, do type and give the user the choice to delete such file.Explanation / Answer
# function to recursively iterate through a folder
folder_recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
folder_recurse "$i"
elif [ -f "$i" ]; then
FILETYPE=`file $F1 | cut -d -f2`
echo "file: $i $FILETYPE"
if [ $FILETYPE == "Other" ]
then
echo "Do you want to remove $i(y/n)"
read CHOICE
if [ $CHOICE == "y" ]
then
rm $i
fi
fi
fi
done
}
echo "Please enter a filename:"
read F1
# Does the file exist
if [ ! -e $F1 ]
then
echo "Not exists"
fi
#Is the file executable
if [[ -x $F1 ]]
then
chmod u+x $F1
./$F1
fi
# Get file type
TYPE=`file $F1 | cut -d -f2`
if [ $TYPE == "ASCII" ]
then
chmod u-w $F1
chmod g-w $F1
cat $F1
fi
# Is it a directory
if [ -d $F1 ]
then
chmod +rx $F1
folder_recurse $F1
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.