i write the below script to make the user get to the directory that interesting. Now what I am trying is to check the permissions of the directory and if the directory exists to check the reading options.
echo "Please enter your desire folder directory ( \yourfolders) ?: \c"
read yourfolder
echo
# **** Check if the directory exists ******************
# **** find out if file has write permission or not ***
# *****************************************************
[ -w $yourfolder ] && W="Write = yes" || W="Write = No"
# *****************************************************
# *** find out if file has excute permission or not ***
# *****************************************************
[ -x $yourfolder ] && X="Execute = yes" || X="Execute = No"
# *****************************************************
# **** find out if file has read permission or not ***
# *****************************************************
[ -r $yourfolder ] && R="Read = yes" || R="Read = No"
echo "$yourfolder Permissions"
echo "$W"
echo "$R"
echo "$X"
if
[! -d "$yourfolder" ]; then
echo "That directory exists and below you can see the directory and files: \c"
echo
cd $yourfolder
echo
ls -ltr
echo
pwd
echo "Thank you very much!!!"
elif
[ -r "$yourfolder" ]; then
echo "That directory exists but not available for reading"
exit
# **** if the directory doesn't exists ****
else
echo "That directory doesn't exists !!!"
echo "Thank you very much"
fi
exit;;
Now as you can see I check the permissions but I don't know how to make the if statement. The first if statement it provide you the directory asap. In the second if checks if the directory exists. But for the permissions, HOUSTON we have a problem. I need to provide me a message that the folder is not accessible because of the permission " that " Any ideas?
What you're doing with X is a very bad idea. The value of $yourfolder will be subject to field splitting and pathname expansion after the unquoted expansion of $X. This could cause testing of an unintended pathname, or a syntax error in the resulting test/[ command.
[sea@dell ~]$ yourfolder=/root
[sea@dell ~]$ for V in r w x d;do X="-$V";[ $X "$yourfolder" ] && RET=yes||RET=no; echo "$V:$RET";done
r:no
w:no
x:no
d:yes
Would be better..
@ mikerousse: Whats not working?
EDIT: Yes that is a good idea.
Why not make the output at the same time you're checking the file?
Or, append it (the message [file not available etc], not the info [read = xy]) to an output variable and print that at the end?
@ Alister: I did have it without '$X', but then it failed upon missing argument...
@ mikerousse :
'That' is not a regular permission
You are / were checking both times for the same result, but YOUR message 'That directoryy exists...' was misleading, as it is only printed when user CAN READ it.
But it should only print that message when it cant read it. [ ! -r $yourfolder ] && echo "Cannot access $yourfolder!" && exit 1
EDIT:
elif
[ ! -r "$yourfolder" ]; then
echo "That directory exists but not available for reading"
exit 1 # If this is a 'failexit', use 'exit 1', otherwise, leave as is/was
# **** if the directory doesn't exists ****
else