1828457 Members
3317 Online
109978 Solutions
New Discussion

basic scripting

 
SOLVED
Go to solution
CRollins
Advisor

basic scripting

i need help with a basic loop. for some reason the syntax is eluding me. im creating a list of filenames appended into a file. i want to run an operation against the names in the file. for example...

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.
10 REPLIES 10
Keith Johnson
Valued Contributor
Solution

Re: basic scripting

Try this...

for i in `cat filename`
do
cp ${i} ${i}.old
done

That should do it.
No matter where you go...there you are.
Sandman!
Honored Contributor

Re: basic scripting

for i in `cat filename`
do
cp $i $i.old
done
James R. Ferguson
Acclaimed Contributor

Re: basic scripting

Hi:

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...
CRollins
Advisor

Re: basic scripting

do i need an exit at the end?
James R. Ferguson
Acclaimed Contributor

Re: basic scripting

Hi (again):

> 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...

CRollins
Advisor

Re: basic scripting

what is filenames? Is the list? how do i print it into done?
James R. Ferguson
Acclaimed Contributor

Re: basic scripting

Hi (agani):

> 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...

James R. Ferguson
Acclaimed Contributor

Re: basic scripting

Hi (again):

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...
Geoff Wild
Honored Contributor

Re: basic scripting

You can also avoid the cat like so:

for i in $(< /filename )
do
cp -p ${i} ${i}.old
done


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Dennis Handly
Acclaimed Contributor

Re: basic scripting

>Geoff: You can also avoid the cat like so:
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.