|
|
| Next: Tablet PC cursor |
| Author |
Message |
Mike External

Since: Apr 29, 2007 Posts: 64
|
Posted: Thu Aug 09, 2007 11:42 am Post subject: Bash script glitch Archived from groups: alt>os>linux (more info?) |
|
|
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 |
|
| Back to top |
|
 |
Ben Collver External

Since: Apr 23, 2007 Posts: 9
|
Posted: Thu Aug 09, 2007 11:42 am Post subject: Re: Bash script glitch [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
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 |
|
| Back to top |
|
 |
Chris F.A. Johnson External

Since: Oct 06, 2004 Posts: 589
|
Posted: Thu Aug 09, 2007 2:51 pm Post subject: Re: Bash script glitch [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
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 |
|
| Back to top |
|
 |
Mike External

Since: Apr 29, 2007 Posts: 64
|
Posted: Thu Aug 09, 2007 8:31 pm Post subject: Re: Bash script glitch [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
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 |
|
| Back to top |
|
 |
Mike External

Since: Apr 29, 2007 Posts: 64
|
Posted: Thu Aug 09, 2007 8:33 pm Post subject: Re: Bash script glitch [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
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 |
|
| Back to top |
|
 |
Ben Collver External

Since: Apr 23, 2007 Posts: 9
|
Posted: Thu Aug 09, 2007 9:26 pm Post subject: Re: Bash script glitch [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
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 |
|
| Back to top |
|
 |
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|