question in shell script - Shell Scripting
This is a discussion on question in shell script - Shell Scripting ; Hello guys,
I need a little help to solve this question
Write a Bourne shell script which:
• Has one command line argument.
• If the command line argument is a directory then the script should output the number of ...
-
question in shell script
Hello guys,
I need a little help to solve this question
Write a Bourne shell script which:
• Has one command line argument.
• If the command line argument is a directory then the script should output the number of files in the directory.
• If the command line argument is an ordinary file then the script should output whether or not the file has execute permission for the file owner.
• If the command line argument is neither a file or directory then the script should output an appropriate error message.
• If no command line argument is supplied then the script should output an appropriate error message.
-
Re: question in shell script
What do you think of the following:
#!/usr/bin/env bash
if [[ $# -gt 1 ]]
then
echo "to many args"
exit 1
elif [[ $# -eq 0 ]]
then
echo "No args were supplied"
exit 2
fi
if [[ -d $1 ]]
then
total=$(ls $1|wc -l)
echo -e "Total files in folder = $total"
elif [[ -f $1 ]]
then
permissions=$(ls -al $1|awk '{print substr($1,0,4)}')
if [[ $permissions =~ x ]]
then
echo "File is executable"
else
exit 3
fi
else
echo "The command line arg is neither a file nor folder or doesn't exist"
exit 4
fi