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
.



Relevant Pages

  • Re: GPS altitude to GIS elevation at same position
    ... > The IP address is correct, and the server is responding to PING from ... I haven't had any other reports of "can't reach your Web ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Winter meetings
    ... Romero's "reports" outside the SoCal bubble. ... Ric Romero has had little, as far as I know, to say about the stupendous ... Responding to coachrose13 - oh this can only end well. ... I dont know and dont give a rats-ass who Ric is or what he thinks, ...
    (rec.sport.baseball)
  • Re: How stable is 1.5? CRASH REPRODUCED
    ... > I submitted the report to Sun and got the usual message of: ... > have a three week response time for responding to Bug Reports." ...
    (comp.lang.java.programmer)
  • Re: How stable is 1.5? CRASH REPRODUCED
    ... word from Sun after more than three weeks. ... crash bugs would get treated with a greater sense of urgency. ... > have a three week response time for responding to Bug Reports." ...
    (comp.lang.java.programmer)

Loading