bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FWD: About Bash Script


From: Mart Frauenlob
Subject: Re: FWD: About Bash Script
Date: Tue, 16 Feb 2010 20:43:15 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20100111 Thunderbird/3.0.1

On 16.02.2010 19:52, Curtis wrote:
> Thanks pk!
> 
> That's the same thin Greg told me.
> 
> #!/bin/bash
> 
> 
> if [! -e b.txt];

if [[ ! -e b.txt ]; then ...; fi

or

if [[ ! - b.txt ]]
then
...
fi

you only need the semicolon if you omit the newline.

> 
> then
> 
> mv a.txt b.txt
> 
> exit
> 
> fi
> 
> 
> 
> #The previous commands checks to see if b.txt is NOT already there, if
> NOT, it renames a.txt to b.txt
> 
> 
> 
> #If the script gets here, b.txt EXISTS..........
> 
> # does_exist is a recursive function....looking at b.txt(i) ......the
> first b.txt(some number) it finds NOT existing...it creates....
> 
> 
> 
> set i = 1

set??? see: help set

i=1
or
declare i=1

see: help declare (in you bash prompt)

> 
> does_exist[]

you want to define a function?

does_exist() {

<code_here>
}

> 
> 
> 
> if [-e b.txt.$i];
> {

this `{' one is bad here.

> 
> then
> 
> i = i +1

i=$((i + 1))
or
i=$((i++))
or
let i++
or
let i+=1

> 
> does_exist
> 
> else
> 
> mv a.txt b.txt.$i
> exit
> fi
> }
> 
> 
> I'm close but getting an eror near the last line....not really sure
> why....
> ./test: line 25: syntax error near unexpected token `fi'
> ./test: line 25: `fi'
> 

May i suggest some readings:

http://tldp.org/LDP/abs/html/index.html
http://bash-hackers.org/wiki/doku.php/start
http://mywiki.wooledge.org/BashFAQ
http://tiswww.case.edu/php/chet/bash/FAQ

I sent you this link in my first reply to you:
http://groups.google.com/group/comp.unix.shell/browse_thread/thread/233d175274e246bd/b4deba6bf5fea99a?lnk=raot

This thread talks about race conditions, that is what i based my first
piece of code for you on. That's why i tried not to use
if condition then react - type of stuff.
Might not affect you at all.

However, we see what you are trying to do, but we don't know what for.
I guessed file downloading, greg guessed log rotating.
It might be a good idea to provide some more information, because that
might lead to different solutions.

Again however, as i didn't do that before myself, i tried to write some
what hopefully meets your requirements:

#!/bin/bash

f() {
[[ $2 ]] || return 1
set -f
set -C
local in_file="$1" out_file="$2"
local i=0 ext=
(
    until command exec 3> "$out_file$ext"; do
        ext=.$((i++))
    done
    exec cat "$in_file" >&3
) 2>/dev/null || exit 1
}

f /tmp/a /tmp/b || echo "error"

---
the function takes 2 parameters, the name of the input file and the name
of the file which may already exist.
read the above link, it explains like all commands used here.

Best regards

Mart


reply via email to

[Prev in Thread] Current Thread [Next in Thread]