I have a script where i am counting the number of files that the user has read permissions to in a folder . They are asked for this folder at the start
if they dont enter anything then i want to be able to do the search on the current directory, can you help me with that part please ?
I am just using find commands for the permissions bit
echo " choose the dir "
read dir
fileread=$(find "$dir" -type f -perm u+r | wc -l)
echo "you have read permissions to $fileread files
Please add "code" tags around your code!
According to your description you want at least u+r permissions (other permissions are allowed), so it must be -u+r
echo " choose the dir "
read dir
fileread=$(find "${dir:-.}" -type f -perm -u+r | wc -l)
echo "you have read permissions to $fileread files"
Or with parameters
fileread=$(find "${@:-.}" -type f -perm -u+r | wc -l)
NB the $@ expands within " " (unlike $* ). This makes it possible to pass a dirname with embedded space (for exampe "this dir" ).
I am just trying to find read, not "at least read"
Ok i have added the following at the top of the script
case $# in
0) dir=${dir:-.}
esac
echo $dir
fileread....
When i run this it tells me i have read permissions to 112 files and the echo $dir shows . so im presuming this command sets the location at root ? if the user starts at Templates for example c:\templates and runs the script and dosent enter any directory then i want the command to run on Templates
case $# in
(0) printf 'Enter directory: '
read dir;;
(1) dir="$1";;
esac
printf 'Using directory "%s"\n' "$dir"
Or, if, when no operands are given to your script, you want to use the current directory specified as an absolute pathname instead of prompting for it:
case $# in
(0) dir="$PWD";;
(1) dir="$1";;
esac
printf 'Using directory "%s"\n' "$dir"
thanks for your help. I have it all working now except for if the user specifies a directory. The script still runs on the current directory. Any idea why $1 isnt being interpreted ?
#/bin/bash
echo "What directory do you want to check your permissions on ?"
read dir
case $# in
(0) dir="$PWD";;
(1) dir="$1";;
esac
echo $dir
fileread=$(find "$dir" -type f -perm /u+r | wc -l)
echo "You have read permissions to $fileread files"
filewrite=$(find "$dir" -type f -perm /u+w | wc -l)
echo "You have write permissions to $fileread files"
filecombi=$(find "$dir" -type f -perm /u+rw | wc -l)
echo "You have read and write permissions to $fileread files"
ok . i must be missing something here. i thought that the case statement would change dir to $pwd if there was no entry by the user and if there was 1 entry then it would change dir to that entry $1 ?
parameter count, yes so if 0 then set dir to pwd and if 1 (the user enters a dir) then set dir to that value ?
---------- Post updated at 05:04 AM ---------- Previous update was at 04:53 AM ----------
ok i looked at using the method you suggested and it works ok on hte current directory now (if no user input) but still doesnt take the user input into $1 if they enter it
if [ -z $1 ]
then
dir="$PWD"
elif [ -n $1 ]
then
dir="$1"
fi
yeah i thought that myself that it should be $dir so last time now i promise, here is my code
If i enter a dir then it works fine but its now not working on the current dir (if user enters no value)
#/bin/bash
echo "What directory do you want to check your permissions on ?"
read dir
case $dir in
(0) dir="$PWD";;
(1) dir="$dir";;
esac
printf 'Using directory "%s"\n' "$dir"
fileread=$(find "$dir" -type f -perm /u+r | wc -l)
echo "You have read permissions to $fileread files"
filewrite=$(find "$dir" -type f -perm /u+w | wc -l)
echo "You have write permissions to $fileread files"
filecombi=$(find "$dir" -type f -perm /u+rw | wc -l)
echo "You have read and write permissions to $fileread files"