| |||
| Bash script glitch To check for the presence of files in a dir before processing, I've been using: if [ ! -f ~/some/dir/*.file ]; then echo echo " No files found." echo exit else echo echo " Files found. Proceeding." echo fi This works, but reports "too many variables" for the line with '*'. I've tried to decypher the bash man page, but can't seem to get the clue as to how to deploy the '*' without upsetting the shell enough to complain. Anybody know what I've missed? -- Yellow Submarine? Nah. Its a TeaPot! www.tinyurl.com/382gmp |
| |||
| Re: Bash script glitch Mike wrote: > if [ ! -f ~/some/dir/*.file ]; then > ... > This works, but reports "too many variables" for the line with '*'. > > Anybody know what I've missed? The [ man page should show that the -f option takes 1 argument. However, if there is more than one file in the directory, then the shell globbing will expand *.file into multiple arguments, thus "too many variables". You could replace this with the following line. if [ $(find ~/home/dir -maxdepth 1 -type f | wc -l) -eq 0 ]; then This uses find to output a list of files in ~/home/dir, but not subdirectories. The list is piped to wc -l, which outputs a count of the lines. The count is substituted for $(...). If the count is equal to 0, then there are no files, and it tests positive. What follows is a less precise replacement. ls ~/home/dir/* >/dev/null 2>&1 if [ $? -ne 0 ]; then If there are no files in ~/home/dir, then shell globbing matches nothing and ~/home/dir/* is the argument for the ls command. The ls command will fail to find a file named "*" and will have a non-zero exit code. If the exit code is not equal to zero, then the directory is empty. Cheers, Ben |
| |||
| Re: Bash script glitch On 2007-08-09, Mike wrote: > To check for the presence of files in a dir before processing, I've > been using: > > if [ ! -f ~/some/dir/*.file ]; then > echo > echo " No files found." > echo > exit > else > echo > echo " Files found. Proceeding." > echo > fi > > This works, but reports "too many variables" for the line with '*'. You are getting the error because the wildcard expands to more than one file name. Try using this function to determine the existence of a file: is_file () { for f in "$@"; do [ -f "$f" ] && return; done; return 1 } The function is fast because it doesn't use any external commands. Use it like this: if is_file ~/some/dir/*.file then .... fi -- Chris F.A. Johnson, author | <http://cfaj.freeshell.org> Shell Scripting Recipes: | My code in this post, if any, A Problem-Solution Approach | is released under the 2005, Apress | GNU General Public Licence |
| |||
| Re: Bash script glitch Responding to Ben Collver... > Mike wrote: >> if [ ! -f ~/some/dir/*.file ]; then >> ... >> This works, but reports "too many variables" for the line with '*'. >> >> Anybody know what I've missed? > > The [ man page should show that the -f option takes 1 argument. > However, if there is more than one file in the directory, then the shell > globbing will expand *.file into multiple arguments, thus "too many > variables". > > > You could replace this with the following line. > > if [ $(find ~/home/dir -maxdepth 1 -type f | wc -l) -eq 0 ]; then > > This uses find to output a list of files in ~/home/dir, but not > subdirectories. The list is piped to wc -l, which outputs a count of > the lines. The count is substituted for $(...). If the count is equal > to 0, then there are no files, and it tests positive. > > > What follows is a less precise replacement. > > ls ~/home/dir/* >/dev/null 2>&1 > if [ $? -ne 0 ]; then > > If there are no files in ~/home/dir, then shell globbing matches nothing > and ~/home/dir/* is the argument for the ls command. The ls command > will fail to find a file named "*" and will have a non-zero exit code. > If the exit code is not equal to zero, then the directory is empty. > > > Cheers, > > Ben Thats me educated! Thanks for that, but I also need to pin down if there are any *.file files present, rather than just any files at all. IOW, extension (or other detail) specific dir probing. -- Yellow Submarine? Nah. Its a TeaPot! www.tinyurl.com/382gmp |
| |||
| Re: Bash script glitch Responding to Chris F.A. Johnson... > On 2007-08-09, Mike wrote: >> To check for the presence of files in a dir before processing, I've >> been using: >> >> if [ ! -f ~/some/dir/*.file ]; then >> echo >> echo " No files found." >> echo >> exit >> else >> echo >> echo " Files found. Proceeding." >> echo >> fi >> >> This works, but reports "too many variables" for the line with '*'. > > You are getting the error because the wildcard expands to more > than one file name. Try using this function to determine the > existence of a file: > > is_file () > { > for f in "$@"; > do > [ -f "$f" ] && return; > done; > return 1 > } > > The function is fast because it doesn't use any external commands. > Use it like this: > > if is_file ~/some/dir/*.file > then > .... > fi > As I just mentioned to Ben, I'd no idea what was going on, and his explanation filled in the gaps there. This one covers my need to look for specific extensions etc. Much appreciated. -- Yellow Submarine? Nah. Its a TeaPot! www.tinyurl.com/382gmp |
| |||
| Re: Bash script glitch Mike wrote: > Thats me educated! Thanks for that, but I also need to pin down if > there are any *.file files present, rather than just any files at > all. IOW, extension (or other detail) specific dir probing. My examples would become if [ $(find ~/home/dir -maxdepth 1 -type f -name '*.file' | wc -l) -eq 0 ]; then and ls ~/home/dir/*.file >/dev/null 2>&1 if [ $? -ne 0 ]; then Ben |
| |||
| Re: Bash script glitch - (Finding files) Responding to myself... After fiddling around with the examples, I came up with this #!/bin/bash cd ~/some/dir for filesIwant in *.txt; do if [ -f $filesIwant ]; then echo -n " File found: $filesIwant " else echo echo " Files not found." echo exit fi done cd ~/ Clumsy maybe, but easy to wrap the domestic mind around. ;) The echo -n is of course not required, but it does make a pretty pattern onscreen, and lots of reported files fit in the same screen space, which can give you an instant visual idea of how many files you might have. Thanks for the pointers guys. -- Yellow Submarine? Nah. Its a TeaPot! www.tinyurl.com/382gmp |
![]() |
| Bookmarks |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Printing: OpenOffice yes, bash no. | toni | Linux | 6 | 06-27-2007 04:30 PM |
| help! - /bin/bash Permission Denied | Dokurobei | Apple Macintosh Hardware | 1 | 05-06-2007 06:18 AM |
| Bash script questions. | Longfellow | Linux | 5 | 05-05-2007 11:26 PM |
| grep in a bash script | hobbzilla | Linux | 4 | 05-05-2007 11:11 PM |
| cron.daily bash scripting question | Beowulf | Linux | 2 | 01-15-2007 12:02 PM |