If no input then set directory to current


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If no input then set directory to current
# 8  
Old 09-25-2015
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

Code:
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

Last edited by Don Cragun; 09-25-2015 at 05:30 AM.. Reason: Add CODE and ICODE tags.
# 9  
Old 09-25-2015
Try something more like:
Code:
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:
Code:
case $# in
(0)	dir="$PWD";;
(1)	dir="$1";;
esac
printf 'Using directory "%s"\n' "$dir"

or, more simply:
Code:
dir="${1:-$PWD}"
printf 'Using directory "%s"\n' "$dir"


Last edited by Don Cragun; 09-25-2015 at 05:50 AM.. Reason: Add 3rd alternative.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 09-29-2015
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 ?

Code:
#/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"

# 11  
Old 09-29-2015
"specifies" means what? Supplying a single parameter to the script or entering some answer to be read into "dir"?
# 12  
Old 09-29-2015
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
# 13  
Old 09-29-2015
and it should - according to your script, $dir isn't used any more but overwritten in the case statement.
# 14  
Old 09-29-2015
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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

2. UNIX for Dummies Questions & Answers

How to email the current directory?

Hi, I'm very new to Unix, but have been given a command to type in which is : mail -s <email subject goes here> <my email address> <success.txt this command is quite a basic one and sends an email containing the contents of the file "success.txt" to whatever email I put in with the subject of... (2 Replies)
Discussion started by: rnmuk
2 Replies

3. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

4. UNIX for Advanced & Expert Users

current directory in awk

Hello, I want to use the string with the current directory in my awk command. I tried: 'pwd=system("pwd")' but it doesn't work. can please help somebody? (2 Replies)
Discussion started by: daWonderer
2 Replies

5. HP-UX

Unable to Set Prompt to current working DIR

HPUX does not recognise \h,\w,\u to display the hostname,working directory and username respectively. So how do i set the PS1 variable to display my current working Directory as my prompt? I also tried PS1=$PWD, But it keeps showing the same directory path as prompt which PWD was holding at... (3 Replies)
Discussion started by: Amit Kulkarni
3 Replies

6. Shell Programming and Scripting

tarball of current directory

I wanna make a backup tarball. I wanna write a script that makes tarball of the current directory. There are lots of files so I cant type all files, I wanna make the tarball by excluding few files. Like there 1000 files in a directory I wanna create a tarball containing 98 files of that... (1 Reply)
Discussion started by: nishrestha
1 Replies

7. UNIX for Dummies Questions & Answers

How to compare current time with the input to variant?

Hi all, I have a simple script follow: ------------- #!/bin/bash echo -n " Enter the date of today: " read dateoftoday ------------- Now I want to compare the variant $dateoftoday with date of the system (now) in order to prevent user inputs the past date to $dateoftoday. I want to make... (3 Replies)
Discussion started by: axn_boy
3 Replies

8. Shell Programming and Scripting

Finding files in current directory when 100,000's files in current directory

Hi All I was wondering what is the most efficient way to find files in the current directory(that may contain 100,000's files), that meets a certain specified file type and of a certain age. I have experimented with the find command in unix but it also searches all sub directories. I have... (2 Replies)
Discussion started by: kewong007
2 Replies

9. Shell Programming and Scripting

filename in current directory

I want to perform a task on all the files in the current directory but I'd like to loop through them one at a time. How do I tell it to give me the first filename? (2 Replies)
Discussion started by: calgone337
2 Replies

10. UNIX for Advanced & Expert Users

cannot determine current directory

Hi, when I execute some simple commands on my solaris system, I am getting the following warning message: Could anybody tell me what could be the reason Ex:- If I give the command, which ls Warning: cannot determine current directory ... (15 Replies)
Discussion started by: axes
15 Replies
Login or Register to Ask a Question