- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: finding file with for loop
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 05:18 AM
11-28-2008 05:18 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 05:23 AM
11-28-2008 05:23 AM
Re: finding file with for loop
for file in *.bak *.txt *.cat *.pll ; do
if test -f $file; then
cp $file /logs
fi
done
But ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 05:29 AM
11-28-2008 05:29 AM
Re: finding file with for loop
>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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 05:52 AM
11-28-2008 05:52 AM
Re: finding file with for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 06:23 AM
11-28-2008 06:23 AM
Re: finding file with for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 06:25 AM
11-28-2008 06:25 AM
Solution2) 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 06:35 AM
11-28-2008 06:35 AM
Re: finding file with for loop
Asking about AIX in an HP-UX forum?
Not using "find"?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 06:56 AM
11-28-2008 06:56 AM
Re: finding file with for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 06:58 AM
11-28-2008 06:58 AM
Re: finding file with for loop
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 07:46 AM
11-28-2008 07:46 AM
Re: finding file with for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2008 11:19 AM
11-28-2008 11:19 AM
Re: finding file with for loop
> 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.)