In: Computer Science
Write a short bash program that aligns with the below functionality.
$ sh ~/cs210_list.sh Error: missing argument $ echo $? 1 $ sh ~/cs210_list.sh /non-exist Error: ‘/non-exist’ is an invalid directory $ echo $? 1 $ sh ~/cs210_list.sh /etc File: aavark File: aabbb Size: 16 File: aaacccc File: aaaaddd Size: 1585 File: aliases~ Size: 1686 File: aliases.db Size: 12288 ... |
Notice:
Grading Criteria:
In my check, I will ask you to do the following checks:
Copy and paste your program below
i) # Shell script for missing argument
if [ $# -eq 0 ]
then
echo " Error: Missing argument"
else
echo " There are $# arguments and they are: $* "
fi
*************************************************
# Script to pass an arument through command line and check whether it is a valid directory or not
if [ $# -eq 0 ] # $# always holds the number of arguments passed, this checks for arguments absence
then
echo " No arguments are passed"
elif [ $# -eq 1 -a -d $1 ] # This checks for 1 argument and must be a valid directory.
then
echo "It is a valid directory"
else
echo "It is not a directory"
fi
**************************************************
iii)
Note : I am considering the argument passed is a directory and it will print only the ordinary file name along with it size.
# Script to display the name of a file along with its size
if [ $# -eq 0 ]
then
echo " Pass a valid directory as an argument .........."
elif [ $# -eq 1 -a -d $1 ]
then
ls -lR $1 | tr -s ' ' | cut -d ' ' -f 9 > a
ls -lR $1 | tr -s ' ' | cut -d ' ' -f 5 > b
paste a b
fi
Note : $? is a special parameter used to check exit status of a command ...