Write a bash shell script that emulates the basic functionality of the ls comman
ID: 3590298 • Letter: W
Question
Write a bash shell script that emulates the basic functionality of the ls command for the present working directory. Specifically for each object in the directory your script must:
determine & display whether it is a directory, file, or other
determine & display the file owner's permissions for directories and files
utilize one or more Linux functions to minimize redundancy.
add error handling for SIGINT
hint - Use test commands for testing files within the loop (man test)
Example of the script output:
[jalcorn@blanca]$ ./quiz1
/home/jalcorn/hwfile is a file with owner permissions: rw-
/home/jalcorn/hwscript is a file with owner permissions: rwx
/home/jalcorn/testdir is a directory with owner permissions: r—
Explanation / Answer
# Hello World Program in Bash Shell
mkdir a
echo "Current directory is $PWD."
echo "HOME is $HOME."
#loop for checking all files and directory
for name in $(ls $HOME)
do
#Check file or directory using if else ladder
if [[ -d $name ]]; then
readlink -f $name #command to get absolute path
echo "$name is a file with owner permissions:"
stat -c %A $name | cut -c1-4 #command to get owner permisions
elif [[ -f $name ]]; then
readlink -f $name #command to get absolute path
echo "$name is a file with owner permissions:"
stat -c %A $name | cut -c1-4 #command to get owner permisions
else
echo "$name is not valid"
exit 1
fi
done
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.