From: Hongyi Zhao on
Hi all,

When I download a file by curl, I use -o switch to write the file as a
local file, say, myfile. The issue is: if a file with the same
filename already exists in the target folder, the original file will
be overwrited. So, I want to rename this downloaded file to something
like this: myfile.1 in order to avoid filename conflict in the above
case; in gerneral, if myfile.1 also has already exists in the
destination folder, use myfile.2 as the filename for the curl's
output; and so on ...

Any hints for the above purpose?

Thanks in advance.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Hongyi Zhao on
On 4 Feb 2010 06:38:44 GMT, "Chris F.A. Johnson"
<cfajohnson(a)gmail.com> wrote:

>dlfile=myfile
>dfnum=1
>while [ -e "$myfile.$dfnum" ]
>do
> dfnum=$(( $dfnum + 1 ))
>done
>dlfile=$dlfile.$dfnum
>
>curl -o "$dlfile" ...

Dood, thanks a lot, I've done some revision based on your version:

dlfile=myfile
dfnum=1

if [ ! -e "$dlfile" ]
then
curl -o "$dlfile" ...
else

while [ -e "$dlfile.$dfnum".pdf ]
do
dfnum=$(( $dfnum + 1 ))
done
dlfile=$dlfile.$dfnum
curl -o "$dlfile" ...

fi

Thanks again.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Hongyi Zhao on
On Thu, 04 Feb 2010 16:12:30 +0800, Hongyi Zhao
<hongyi.zhao(a)gmail.com> wrote:

>while [ -e "$dlfile.$dfnum".pdf ]

Should be

while [ -e "$dlfile.$dfnum" ]

Sorry.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Mart Frauenlob on
On 04.02.2010 07:28, Hongyi Zhao wrote:
> Hi all,
>
> When I download a file by curl, I use -o switch to write the file as a
> local file, say, myfile. The issue is: if a file with the same
> filename already exists in the target folder, the original file will
> be overwrited. So, I want to rename this downloaded file to something
> like this: myfile.1 in order to avoid filename conflict in the above
> case; in gerneral, if myfile.1 also has already exists in the
> destination folder, use myfile.2 as the filename for the curl's
> output; and so on ...
>
> Any hints for the above purpose?
>
> Thanks in advance.

Example in bash, x is undeclared or '0' at first call:

#!/bin/bash

filename="/tmp/file"
newfile="${filename}"
x=""

until [[ ! -e $newfile ]]; do newfile="${filename}.$((++x))"; done
curl -o "$newfile"


best regards

Mart
From: Hongyi Zhao on
On Thu, 04 Feb 2010 14:28:36 +0800, Hongyi Zhao
<hongyi.zhao(a)gmail.com> wrote:

>When I download a file by curl, I use -o switch to write the file as a
>local file, say, myfile. The issue is: if a file with the same
>filename already exists in the target folder, the original file will
>be overwrited. So, I want to rename this downloaded file to something
>like this: myfile.1 in order to avoid filename conflict in the above
>case; in gerneral, if myfile.1 also has already exists in the
>destination folder, use myfile.2 as the filename for the curl's
>output; and so on ...

Let me be more precise in descripting my requirements as follows:

1- If myfile doesn't exist in the target folder, use myfile as the
downloaded filename.
2- If myfile already exists in the target folder and at the same time
myfile.1 doesn't exist in the target folder, use myfile.1 as the
downloaded filename.
3- If myfile and myfile.1 already exist in the target folder and at
the same time myfile.2 doesn't exist in the target folder, use
myfile.2 as the downloaded filename.
4- Extending the above rules to deal with the remaining cases.

Regards.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.