Re: Bash script glitch
- From: Mike <Not@xxxxxxxxxxx>
- Date: Thu, 09 Aug 2007 20:31:33 GMT
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
.
- Follow-Ups:
- Re: Bash script glitch - (Finding files)
- From: Mike
- Re: Bash script glitch
- From: Ben Collver
- Re: Bash script glitch - (Finding files)
- References:
- Bash script glitch
- From: Mike
- Re: Bash script glitch
- From: Ben Collver
- Bash script glitch
- Prev by Date: Re: Windows kill Linux?
- Next by Date: Re: Bash script glitch
- Previous by thread: Re: Bash script glitch
- Next by thread: Re: Bash script glitch
- Index(es):
Relevant Pages
|
Loading