- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: basic scripting
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
08-22-2007 05:38 AM
08-22-2007 05:38 AM
file contents:
flie1
file2
file3
i want to create the for loop as follows -
for i in `cat filename` do
cp file1 file1.old
i want to perform this task on all filenames in the file then exit when complete.
i've written things like this many times, but its been so long im having trouble setting up the structure to run an operation against a file list.
Please help.
Solved! Go to Solution.
- Tags:
- for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 05:44 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 05:44 AM
08-22-2007 05:44 AM
Re: basic scripting
do
cp $i $i.old
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 05:44 AM
08-22-2007 05:44 AM
Re: basic scripting
First, *don't* use 'for i in `cat file` do:
You don't need to spawn a process to read, let the shell do it:
while read FILE do
cp ${FILE} ${FILE}.old
done < filenames
Regards!
...JRF...
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 06:15 AM
08-22-2007 06:15 AM
Re: basic scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 06:23 AM
08-22-2007 06:23 AM
Re: basic scripting
> do i need an exit at the end?
It's not necessary. The exit value will be that of the last command. You could do:
while read FILE do
cp ${FILE} ${FILE}.old
STATUS=$?
if [ "${STATUS}" -ne 0 ]; then
echo "Error: Copy of ${FILE} failed"
exit 1
fi
done < filenames
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 06:35 AM
08-22-2007 06:35 AM
Re: basic scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 06:43 AM
08-22-2007 06:43 AM
Re: basic scripting
> what is filenames? Is the list? how do i print it into done?
"filenames" is a file that contains the *names* of the files to be copied, one filename per line.
# cat filenames
file1
file2
file3
The "< filenames" notation means to read each line of 'filenames'. The 'while read FILE' means read each line of the input file into the variable named 'FILE'.
This avoids spawning a 'cat' process to create a list of names (lines) that your original 'for' loop iterated over. In my code, the 'FILE' variable holds the filename. In yours, the 'i' variable did the same.
Regards!
...JRF...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 07:23 AM
08-22-2007 07:23 AM
Re: basic scripting
By the way, welcome to the ITRC Forums! When you are satisfied with the help you have been offered, please read and give consideration to:
https://forums1.itrc.hp.com/service/forums/helptips.do?#28
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 07:57 AM
08-22-2007 07:57 AM
Re: basic scripting
for i in $(< /filename )
do
cp -p ${i} ${i}.old
done
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2007 09:04 PM
08-22-2007 09:04 PM
Re: basic scripting
for i in $(< /filename )
You beat me. :-)
The difference between $(< file) and while read is that the former allows multiple files per line (delimited by whitespace but read doesn't. Also while read will allow an infinite number of files but $(<) won't.