Operating System - HP-UX
1833827 Members
2337 Online
110063 Solutions
New Discussion

Re: finding file with for loop

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

finding file with for loop

Hi All

A small question here.

I've no files under /tmp
My script is something like that

cd /tmp
for file in *.bak *.txt *.cat *.pll ; do
cp $file /logs
done

Execution Result is
cp: *.bak: A file or directory in the path name does not exist.
cp: *.txt: A file or directory in the path name does not exist.
cp: *.cat: A file or directory in the path name does not exist.
cp: *.pll: A file or directory in the path name does not exist.

I'd like to avoid this because I don't undertsand why we enter inside the loop. I've no files ! Where is the mistake ?

Bests Regards
Den
10 REPLIES 10
Leo The Cat
Regular Advisor

Re: finding file with for loop

Of course I could use

for file in *.bak *.txt *.cat *.pll ; do
if test -f $file; then
cp $file /logs
fi
done

But ...
Yogeeraj_1
Honored Contributor

Re: finding file with for loop

Hi den,

>I'd like to avoid this because I don't undertsand why we enter inside the loop. I've no files ! Where is the mistake ?

It will never enter the loop only if the check condition is NULL. Here you are specifying static values.

e.g.

for file in $(ls -1 *.txt) ....

would perform better

hope this helps!

kind regards
yogeeraj

No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Leo The Cat
Regular Advisor

Re: finding file with for loop

Hi

I've always an error



___9@cerbere001(/su01/upload) $ for file in $(ls -1 *.bak *.txt *.cat *.pll ) ; do
> echo $file
> done
ls: 0653-341 The file *.bak does not exist.
ls: 0653-341 The file *.txt does not exist.
ls: 0653-341 The file *.cat does not exist.
ls: 0653-341 The file *.pll does not exist.
___9@cerbere001(/su01/upload) $



Bests Regards
Den
Peter Nikitka
Honored Contributor

Re: finding file with for loop

Hi,

1) This method to suppress the message at cp (my prefered method) will ignore directories, dangling symlinks - just everything that is NOT a plain file:

cd /tmp
for file in *.bak *.txt *.cat *.pll
do
[ -f $file ] || continue
cp $file /logs
done

2) To suppress messages at 'ls' just redirect stderr. The option '-1' (=one=) is useless when not set having a tty, I recommend '-d' because there may be directories matching the pattern:
for file in $(ls -d *.bak *.txt *.cat *.pll 2>/dev/null)
do echo $file
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor
Solution

Re: finding file with for loop

Sorry - misspelt 2):

2) To suppress messages at 'ls' just redirect stderr. The option '-1' (=one=) is useless when
not having a tty,
I recommend '-d' because there may be directories matching the pattern:

for file in $(ls -d *.bak *.txt *.cat *.pll 2>/dev/null)
do echo $file
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Steven Schweda
Honored Contributor

Re: finding file with for loop

> Where is the mistake ?

Asking about AIX in an HP-UX forum?

Not using "find"?
Leo The Cat
Regular Advisor

Re: finding file with for loop

Hi Steven

good. For this effectively Y was for a while with my old friend AIX.

I don't want to use find because I don't want to go on sub-directories... but we have perhaps any find option(s) to avoid search on sub directories...

Thank you very much to All, I've now the solution.

Bests Regards
Den
James R. Ferguson
Acclaimed Contributor

Re: finding file with for loop

Hi Den:

If you replace your 'cp' with 'echo' you will see that each value in the list is used as an un-evaulated argument to 'cp'. That is, since glob() doesn't find anything for "*.txt", it is left as "*.txt", generating a "no file" message from 'cp'.

I would do what Peter suggested in scripts like this:

...
[ -f ${FILE} ] && cp ${FILE} /logs
done

Steven has a sharp eye! Your script is *really* running on AIX and not HP-UX. While that doesn't matter in terms of the problem or of the solution, the error messages are different.

HP-UX reports:

cp: cannot access *.txt: No such file or directory

...whereas AIX gives:

cp: *.txt: A file or directory in the path name does not exist.

It's not nice to fool Mother Nature... :-)

Regards!

...JRF...
Leo The Cat
Regular Advisor

Re: finding file with for loop

It' s ok. Thanks to All
Steven Schweda
Honored Contributor

Re: finding file with for loop

> I don't want to use find because I don't
> want to go on sub-directories... but we
> have perhaps any find option(s) to avoid
> search on sub directories...

Easy with GNU "find". Previous Forum
discussions showing exactly how should be
easy to find.

So, you prefer the command-line-too-long
problems which arise from careless use of
wildcard file specifications (like "*")?


> Steven has a sharp eye!

Not really.

> ls: 0653-341 The file *.bak does not exist.

I've only seen one UNIX (-like) OS which
has useful error codes like this, and that's
AIX. (Very startling for a VMS user to find
a UNIX OS which showed some evidence of
having been designed, instead of just cobbled
together, hence memorable.)