If no input then set directory to current

Hi GUys

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
find "${dir:-.}" -type f -perm u+r | wc -l

See Bash Reference Manual: Shell Parameter Expansion.

1 Like

They will have to press "enter" to terminate the read . Did you consider read 's -t option (timeout) to terminate without ANY input?

im getting bad substitution on that

case $# in
0) dir=${dir.-.}
esac

Use a colon instead dot (exactly copying neutronscott's proposal).

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" ).

Manually longhand using OSX 10.7.5, default bash terminal...
Always initialise the current directory before letting the timeout continue...

Last login: Thu Sep 24 20:02:19 on ttys000
AMIGA:barrywalker~> mydir=$(pwd)
AMIGA:barrywalker~> echo $mydir
/Users/barrywalker
AMIGA:barrywalker~> ls "$mydir"
03080.sh		03240.sh.txt		Downloads
03083.sh		03241.sh.txt		Library
03090.sh		2015			Movies
03100.sh		AAWG			Music
03120.sh		AudioScope.sh		Pictures
03140.sh		AudioScope.sh.txt	Public
03180.sh		AudioScope.sh~		Scope
03197.sh		AudioScope.txt		URL
03220.sh		BW_HELP			hexstring
03220.txt		Desktop			listing.txt
03240.sh		Documents		sox-14.4.0
AMIGA:barrywalker~> read -t5 mydir
/tmp/
AMIGA:barrywalker~> echo $mydir
/tmp/
AMIGA:barrywalker~> ls "$mydir"
launch-Az884J		launch-qrFcE6		launchd-119.lqY2J3
launch-PSV50N		launch-zRFZQJ		launchd-253.YZiA7D
AMIGA:barrywalker~> mydir=$(pwd)
AMIGA:barrywalker~> read -t5 mydir
AMIGA:barrywalker~> echo $mydir
/Users/barrywalker
AMIGA:barrywalker~> ls "$mydir"
03080.sh		03240.sh.txt		Downloads
03083.sh		03241.sh.txt		Library
03090.sh		2015			Movies
03100.sh		AAWG			Music
03120.sh		AudioScope.sh		Pictures
03140.sh		AudioScope.sh.txt	Public
03180.sh		AudioScope.sh~		Scope
03197.sh		AudioScope.txt		URL
03220.sh		BW_HELP			hexstring
03220.txt		Desktop			listing.txt
03240.sh		Documents		sox-14.4.0
AMIGA:barrywalker~> _

thanks guys

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

Try something more like:

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"

or, more simply:

dir="${1:-$PWD}"
printf 'Using directory "%s"\n' "$dir"
1 Like

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"

"specifies" means what? Supplying a single parameter to the script or entering some answer to be read into "dir"?

entering the answer to be read into dir

user is asked what dir - if they type in a dir then the script isnt acting on it, its still acting on pwd

and it should - according to your script, $dir isn't used any more but overwritten in the case statement.

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 ?

No. $# is the parameter count to the script, nothing to do with the variable read. Try sth like [ -z $dir ] to check for an empty variable.

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

The user does not enter a parameter when typing into a read statement. Parameters are supplied when calling the script (e.g.scname):

./scname  A   B   C
=    $0   $1  $2  $3  
$#: 3
1 Like

but i dont want to supply parameters calling the script. I just want the script to execute and ask the user what dir they want to check

if the user just presses enter without entering a dir then the script will use the current dir

if the uses enters a dir then the script will use that one

Then don't use $1 nor $# , but $dir .

I must be annoying you so apologies for that

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"