Bash scripting question
Hi All, I have the following script fragment: tags=( $( vorbiscomment "$1") ) for tag in $(seq 0 $((${#tags[@]} - 1))) do echo ${tags[$tag]} done which should extract the comments from the filename in $1 into the array $tags. So far so good. Unfortunately I find that the input is split up at spaces and line breaks, meaning that multi-word tags get distributed across more than one array element. Is there a way to get the assignment statement to break the vorbiscomment at line breaks only, or do I need to write a routine to put the broken lines back together? Thanks, Dylan -- "The man who strikes first admits that his ideas have given out." (Chinese Proverb)
On Thursday 10 August 2006 20:12, Dylan wrote:
distributed across more than one array element. Is there a way to get the assignment statement to break the vorbiscomment at line breaks only, or do I need to write a routine to put the broken lines back together?
What you want is the IFS (Internal Field Separator) variable. Try: man bash /IFS Then tap the 'n' key twice. Alternately google for "bash IFS" and you'll get: http://www.faqs.org/docs/bashman/bashref_33.html then you can try setting IFS to a newline with: IFS="\n" or perhaps it needs: IFS=" " -- ----- stephan@s11n.net http://s11n.net "...pleasure is a grace and is not obedient to the commands of the will." -- Alan W. Watts
On Thursday 10 August 2006 20:12, Dylan wrote:
Hi All,
I have the following script fragment:
tags=( $( vorbiscomment "$1") ) for tag in $(seq 0 $((${#tags[@]} - 1))) do echo ${tags[$tag]} done
which should extract the comments from the filename in $1 into the array $tags. So far so good. Unfortunately I find that the input is split up at spaces and line breaks, meaning that multi-word tags get distributed across more than one array element. Is there a way to get the assignment statement to break the vorbiscomment at line breaks only, or do I need to write a routine to put the broken lines back together?
A bit late, but who knows, this might be usefull: leen@ws-03:~> a=( `echo -e 'a a\nb b'` ); declare -p a declare -a a='([0]="a" [1]="a" [2]="b" [3]="b")' That was (sort of) what you had. My solution: leen@ws-03:~> eval a=( `echo -e 'a a\nb b' | sed 's#^#"#; s#$#"#'` ); declare -p a declare -a a='([0]="a a" [1]="b b")' You would probably do: eval tags=( $( vorbiscomment "$1" | sed 's#^#"#; s#$#"#' ) ) Cheers, Leen
I have the following script fragment:
tags=( $( vorbiscomment "$1") ) for tag in $(seq 0 $((${#tags[@]} - 1))) do echo ${tags[$tag]} done
sifs="$IFS"; IFS=' '; tags=(`vorbiscomment "$1")); IFS="$sifs"; for tag in "${tags[@]}"; do echo "$tag"; done;
which should extract the comments from the filename in $1 into the array $tags. So far so good. Unfortunately I find that the input is split up at spaces and line breaks, meaning that multi-word tags get distributed across more than one array element. Is there a way to get the assignment statement to break the vorbiscomment at line breaks only, or do I need to write a routine to put the broken lines back together?
Jan Engelhardt --
On Tuesday 22 August 2006 13:20, Jan Engelhardt wrote:
I have the following script fragment:
tags=( $( vorbiscomment "$1") ) for tag in $(seq 0 $((${#tags[@]} - 1))) do echo ${tags[$tag]} done
sifs="$IFS"; IFS=' '; tags=(`vorbiscomment "$1")); IFS="$sifs";
Yes, this saves the use of external tools. And with this variation one does not need to save the IFS-variable: leen@ws-03:~> IFS=' ' a=(`echo -en "a a\nb b"`) leen@ws-03:~> declare -p a declare -a a='([0]="a a" [1]="b b")' ;)
for tag in "${tags[@]}"; do echo "$tag"; done;
Cheers, Leen
participants (4)
-
Dylan
-
Jan Engelhardt
-
Leendert Meyer
-
stephan beal