From: Myx on
Hello:

I am writing a Unix shell script which will be executing by means of a
cron job every day once a day. Inside the shell script, I have a
counter which must be incremented every time the shell script runs.
For example, if today the counter is 4408, after the shell script is
executed tomorrow, it should be 4409, the day after tomorrow 4410,
etc. This value is to be used in the shell script, which transfers a
certain file (via ftp). The number will tell what file to extract.

Any help on how to set up this counter will be greatly appreciated.
Another solution that I thought may be feasible to my problem is
instead of keeping a counter, if I can figure out a way to determine
which file is the most recently modified file (which should be the one
with the greater number) - but I don't know how to do this or even if
this is possible.

Thanks
From: John Gordon on
In <ac2b3056-16ca-4e02-b0ae-a47c842eeaa4(a)z66g2000hsc.googlegroups.com> Myx <mariya.nagorna(a)gmail.com> writes:

> cron job every day once a day. Inside the shell script, I have a
> counter which must be incremented every time the shell script runs.

You could keep the number inside of a separate file, and when the shell
script executes it reads the file, increments the number and rewrites the
file with the new number.

> Another solution that I thought may be feasible to my problem is
> instead of keeping a counter, if I can figure out a way to determine
> which file is the most recently modified file (which should be the one

ls -t will list files in order of their modification time.

--
John Gordon A is for Amy, who fell down the stairs
gordon(a)panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

From: Jens Thoms Toerring on
Myx <mariya.nagorna(a)gmail.com> wrote:
> I am writing a Unix shell script which will be executing by means of a
> cron job every day once a day. Inside the shell script, I have a
> counter which must be incremented every time the shell script runs.
> For example, if today the counter is 4408, after the shell script is
> executed tomorrow, it should be 4409, the day after tomorrow 4410,
> etc. This value is to be used in the shell script, which transfers a
> certain file (via ftp). The number will tell what file to extract.

You can't have a counter in the script that doesn't get forgotten
- once the script finishes the counter will vanish with it. You
would have to store its value in some persistent medium, e.g. a
file, upon exit of the script and read it in when the script is
started again.

> Any help on how to set up this counter will be greatly appreciated.
> Another solution that I thought may be feasible to my problem is
> instead of keeping a counter, if I can figure out a way to determine
> which file is the most recently modified file (which should be the one
> with the greater number) - but I don't know how to do this or even if
> this is possible.

You can find out the modification time of a file using e.g. the
stat utility:

stat -c "%Y" filename

will print out the number of seconds since the epoch of the last
modification date of the file named 'filename'. So something like
this

#!/bin/bash

file="";
md=0;

for i in *; do
cd=`stat -c "%Y" $i`;
if [ $cd -gt $md ]; then
file=$i;
md=$cd;
fi
done

echo $file

should print out the name of the file with the newest modification
time in the current directory. Perhaps this gives you an idea of
how to approach the problem.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Ben Bacarisse on
Myx <mariya.nagorna(a)gmail.com> writes:

> I am writing a Unix shell script which will be executing by means of a
> cron job every day once a day. Inside the shell script, I have a
> counter which must be incremented every time the shell script runs.
> For example, if today the counter is 4408, after the shell script is
> executed tomorrow, it should be 4409, the day after tomorrow 4410,
> etc. This value is to be used in the shell script, which transfers a
> certain file (via ftp). The number will tell what file to extract.

You need a file to store the counter, like this:

#!/bin/bash

read COUNT <counter
echo $COUNT
echo $((COUNT+1)) >counter

but that may be a bad idea. It sounds like you want to keep instep
with some other counter:

> Any help on how to set up this counter will be greatly appreciated.
> Another solution that I thought may be feasible to my problem is
> instead of keeping a counter, if I can figure out a way to determine
> which file is the most recently modified file (which should be the one
> with the greater number) - but I don't know how to do this or even if
> this is possible.

If that is the case, then along with ls -t that you have been pointed
at there is the option sorting the file list to extract the file with
largest number in its name. The detail will depend on the file name
format but sort (and maybe cut) would be involved.

--
Ben.
From: Gordon Burditt on
>I am writing a Unix shell script which will be executing by means of a
>cron job every day once a day. Inside the shell script, I have a
>counter which must be incremented every time the shell script runs.

Assuming you do not have locking problems (e.g. you have to worry
about whether the script run every 24 hours takes more than 24 hours
to finish), you can keep a little counter file that contains one
line with a number on it. In shell you can use such operations as:

read $count < counter
expr $count + 1 > counter
scp filename`cat counter`.txt othersys:/var/spool/data

to manipulate it. You may be better off setting a shell variable
to the absolute path name of the counter file and using that to
reference the counter.

Another possibility is to use the date as part of the file
name in a way that sorts correctly, e.g.
filename=/dir/file`date +%Y%m%d`.ext
which might come out to:
filename=/dir/file20080826.ext
You can also throw in hours, minutes, and seconds if needed.

Incidentally, it's a lot easier than you might expect to have
problems with more than one copy of a shell script that will
"obviously" finish in time. Things like NFS servers hanging up,
CPU fans dying, nameservers not serving queries, NIC cards dying,
root nameservers being unreachable, etc. can cause problems at
unexpected times.

>For example, if today the counter is 4408, after the shell script is
>executed tomorrow, it should be 4409, the day after tomorrow 4410,
>etc. This value is to be used in the shell script, which transfers a
>certain file (via ftp). The number will tell what file to extract.

Things to worry about:

System downtime: what happens if the file is not generated (because the
system was down), and the script tries to transfer it, or the file
IS generated, but the attempt to transfer doesn't happen? What happens
if the system is down several days? Is the counter number supposed
to be tied to a particular day?

Synchronization: unless the file is generated by the same script
that transfers it, how do you know the file is *finished* before
copying it?


>Any help on how to set up this counter will be greatly appreciated.
>Another solution that I thought may be feasible to my problem is
>instead of keeping a counter, if I can figure out a way to determine
>which file is the most recently modified file (which should be the one
>with the greater number) - but I don't know how to do this or even if
>this is possible.

ls -t | head -1
gets you the most recently modified file (which does NOT say it's
complete). You might want to create the file elsewhere, then "mv"
it to this directory on the same filesystem, so the mtime doesn't
change.