1753720 Members
5278 Online
108799 Solutions
New Discussion юеВ

Re: Help with a script

 
SOLVED
Go to solution
George Doller
Frequent Advisor

Re: Help with a script

I tried using the date on the file instead of TIMESTAMP but I guess because they are same files they are happening at the same time and the 2nd file is over writing the 1st one. Thay for the suggestion. Can you explain how I might be able to try the loop?
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help with a script

Hi (again) George:

#!/usr/bin/sh
while read FILE
do
mv ${FILE} ${FILE}.$(date +%Y%m%d_%H%M%S)
sleep 1
done < inputlist

...will make sure that the timestamp extension is unique.

Regards!

...JRF...
George Doller
Frequent Advisor

Re: Help with a script

With the Sleep I was able to get my script to do everything I needed.
thank you very much.
Dennis Handly
Acclaimed Contributor

Re: Help with a script

>I tried adding the cat in front and I still only get /tmp/testfile.out.

(You shouldn't be using cat, that's evil. :-)

There are two basic ways to handle files with files. Either use while, as JRF said:
while file; do
...
done < file

Or if a "limited" number that will fit in a shell command line, you can use for (an improvement of Steven's case):
for file in $(< file); do
...
done

Note: No cats needed.

As a coding style, unless your other shell scripts need these variables, you shouldn't export them:
DATE=$(date '+%Y%m%d');export DATE
TIME=$(date '+%H%M%S');export TIME

And if you using a real shell, you can use the export command directly:
export FOO=bar
James R. Ferguson
Acclaimed Contributor

Re: Help with a script

Hi (again) George:

As Dennis noted, 'cat's can be evil.

Well-behaved (written) Unix filters will read files passed as commandline arguments, or take their input from STDIN as from a pipe's output.

Thus, when someone writes (variously):

# cat file | awk ...
# cat file | grep ...

...they are creating a needless extra process and wasting I/O. The 'cat' process reads a file and writes each line of output which is then *read* again by another process only to filter and *write* something. Instead, one should write:

# awk '{ ... }' file
# grep whatever file

Regards!

...JRF... [ who happens to like cats (as animals) ]
Steven Schweda
Honored Contributor

Re: Help with a script

> [...] (an improvement of Steven's case) [...]

Hey. Don't blame me for that "cat". I was
only offering an illustration of how the
previous suggestion actually _does_ work
(with the right punctuation). As I said, "It
is possible to read names from a file, but
this is not one of the ways." The one of
those ways which I'd prefer is a while-read
like that suggested by Mr. Ferguson, which
allows more flexible processing more easily.
trpjr1
Advisor

Re: Help with a script

George,

I like to use "set -x" at the begining of scripts and in functions while testing. It helps to debug... Just thought it might help.

Tommy
George Doller
Frequent Advisor

Re: Help with a script

I was able to use the while command and with the sleep it solved all my needs. Thanks for all the quick responses.