Re: FWD: About Bash Script
On Feb 15, 3:20 pm, Mart Frauenlob wrote: > > Original Message > > Subject: About Bash Script > > Date: Mon, 15 Feb 2010 10:39:25 -0600 > > From: Curt > > To: mart.frauen...@chello.at > > Hi Mart- > > > I saw you were helping another guy with a bash scripthopefully 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 Thanks for posting this Mart. I could get curl or wget to create the file, it really doesn't matter. I suppose wget would be the best bet though since curl require an additional package.
Re: FWD: About Bash Script
Here's what I have but i'm getting some errors #!/bin/bash if ! (-e b.txt); 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 functionlooking at b.txt(i) ..the first b.txt(some number) it finds NOT existing...it creates set i = 1 does_exist() { if (-e b.txt.$1); then i = i +1 does_exist else mv a.txt b.txt.$i exit fi }
Re: FWD: About Bash Script
Thanks Greg for the resource material! I'm making changes now
Re: FWD: About Bash Script
Thanks pk! That's the same thin Greg told me. #!/bin/bash if [! -e b.txt]; 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 functionlooking at b.txt(i) ..the first b.txt(some number) it finds NOT existing...it creates set i = 1 does_exist[] if [-e b.txt.$i]; { then i = i +1 does_exist else mv a.txt b.txt.$i exit fi } I'm close but getting an eror near the last linenot really sure why ./test: line 25: syntax error near unexpected token `fi' ./test: line 25: `fi'
Re: FWD: About Bash Script
I actually re-wrote it and got it working. Thanks for the suggested reading Mart, i'll definitely look those over. Thanks for all the help!
exit status question
Not exactly sure if this is a bug. But I don't understand why only the first time running ((i++)) returns an error exit status. Here's my script: #! /bin/bash echo $BASH_VERSION ${BASH_VERSINFO[5]} set -x #set -e i=0 ((i++)) echo $? ((i++)) echo $? And here's what the output looks like: 4.1.7(1)-release x86_64-redhat-linux-gnu + i=0 + (( i++ )) + echo 1 1 + (( i++ )) + echo 0 0 Is there something tricky going on here? Why exit 1 the first time, but then normally every successive time? As you can see, I cannot set -e because it will just exit the script. Uncommented it above and it does this: 4.1.7(1)-release x86_64-redhat-linux-gnu + set -e + i=0 + (( i++ )) Doh! That was not an error. ../C
Re: exit status question
11:30am Eric Blake said: On 12/20/2010 11:25 AM, Curtis Doty wrote: Not exactly sure if this is a bug. But I don't understand why only the first time running ((i++)) returns an error exit status. Because it follows the same semantics as 'expr', where status 1 is reserved for a successful run with value 0, and status 2 and above are reserved for errors. This was just brought up on the list earlier this month: http://lists.gnu.org/archive/html/bug-bash/2010-12/msg00087.html and seems to be a recurring question: http://lists.gnu.org/archive/html/bug-bash/2010-07/msg00121.html The workaround of using ((++i)) seems to work. Thanks for the cluebat! Sounds like this is a FAQ. ../C
read into array Segfaults
I did a stupid thing. Tried to read into an array--which is indexed--but the variable is already an associative array. declare -A foo read -a foo <
Re: read into array Segfaults
On Tue, 21 Jun 2011, Chet Ramey wrote: declare -A foo read -a foo << Yes. It certainly shouldn't crash the shell. Try the attached patch and let me know that it works for you. Thanks for the report. Yep, thanks. Verified no segfault; only the error. Tested on 4.0, 4.1, and 4.2 in Fedora 12, 14, and rawhide respectively. ../C
process substitution in PROMPT_COMMAND
I've recently refactored my PROMPT_COMMAND function to avoid superfluous fork()s. In the body of the function, what was once this line: local jobcount=$(jobs |wc -l) is now this: local job jobcount=0 while read job do ((jobcount++)) done < <(jobs) This works fine on bash 4. However, when attempting to use this on an older bash 3.00.15(1) host, an error occurs; but only on 2nd or additional subshells--not the first login shell!?! I.e. everything works fine, until I run bash from bash. bash: foo: line 39: syntax error near unexpected token `(' bash: foo: line 39: ` done <<(jobs);' bash: error importing function definition for `foo' Am I missing a finer point of redirection from a substituted process? Or is something different in bash 3 that I need to work around here? ../C
Re: process substitution in PROMPT_COMMAND
On Sun, 7 Aug 2011, Chet Ramey wrote: On 8/7/11 6:00 PM, Curtis Doty wrote: local job jobcount=0 while read job do ((jobcount++)) done < <(jobs) As you suspect, the problem is with this part of the function. It doesn't really have anything to do with PROMPT_COMMAND, though. You must be exporting the function so your PROMPT_COMMAND will work in interactive subshells, and the problem is there. Aha, indeed I have this too: export -f foo Removing it and the error goes away on successive subshells. :-) I already export PROMPT_COMMAND=foo. Maybe I don't need to try and re-export the function every time also? bash: foo: line 39: syntax error near unexpected token `(' bash: foo: line 39: ` done <<(jobs);' bash: error importing function definition for `foo' Am I missing a finer point of redirection from a substituted process? Or is something different in bash 3 that I need to work around here? One of the bugs fixed between bash-3.1 and bash-3.2 concerned formatting problems with redirections and process substitution -- the construct you used. That code is used to decompose functions to pass them through the environment, and the incorrectly-formed function has a syntax error that prevents it being imported by the subshell. Chet Thanks Chet! I'll chalk this up as an old bug already fixed. ../C
test -nt not sane
Or maybe I'm not groking. When one compares against a b0rk symlink, the result of -nt (newer than) is true--when it isn't! mkdir directory ln -s noexist symlink touch -hr directory symlink test directory -nt symlink &&echo yes ||echo no They have identical mtimes (as set by touch)--i.e. the directory is *not* newer than the symlink--but it still outputs "yes". Why? ../C
no apostrophe allowed in comment
When the comment is inside of a substituted process. #! /bin/bash -ex # tee >( # this is nifty echo hi there ) The above works. Whereas the below fails. #! /bin/bash -ex # tee >( # ain't this nifty echo hi there ) It burps thusly: ./foo.sh: line 7: bad substitution: no closing `)' in >( # ain't this nifty echo hi there ) Is bash trying to be like tcl here? ;-) ../C