> -------- Original Message --------
> Subject:      About Bash Script
> Date:         Mon, 15 Feb 2010 10:39:25 -0600
> From:         Curt <cain...@gmail.com>
> To:   mart.frauen...@chello.at
> Hi Mart-
> 
> I saw you were helping another guy with a bash script....hopefully you'll 
> remember but i'm trying to modify it for my use.

[...]

> What I want to do is simply if the destination file exists, instead it 
> creates an index number and appends that to the name.
> 
> If b.txt exists then a.txt will be renamed to b.txt.1, but if b.txt.1 exists 
> then it will be renamed to b.txt.2
> 
> I know I can do it with the script above but i'm a little lost on what I 
> should be focusing on. This is my first time trying to modify a bash script, 
> any help would be appreciative.


Hello Curt,

well I took a little time and tried to write something for you.
I also send this to the newsgroup, as it's generally a better place to
ask for help, than send me mail in private.

My old code didn't fit your requirements imho, so i wrote some new.
You didn't tell what you use to create the file curl, wget,...?

#!/bin/bash

set -f
set -C
shopt -s extglob

max_check=10

# list of files is in /tmp/list/ (as example)
for file in $(find /tmp/list -type f); do
#    echo "$file"
    file="/tmp/out/${file##*/}" # output dir is /tmp/out/ (as example)
    i=0
    ext=
    # start at max check - iterate backwards
    # -> file found start new index
    for ((x=max_check; x>=0; --x)); do
        if [[ -e ${file}.${x} ]]; then
            i=$x
            ext=.$((i++))
        break
        fi
    done
    (
    until command exec 3> "$file$ext"; do
        ext=.$((i++))
    done
    # replace 'echo' with command of your choice
    exec echo "$file$ext" >&3
    ) 2>/dev/null    # you might not want to eleminate stderr
done


Best regards

Mart

Reply via email to