[SWLUG] whitespace, backslash,piping and quoting query
bascule
asura at theexcession.co.uk
Sun May 4 00:35:18 UTC 2003
thanks for the reply dave,
i too had noticed that in actual practice the double // didn't seem to make a difference but i wasn't sure why:)
i've been doing some further reading and i think i now have a name for the problem - word splitting.
according to what i read an expansion such as $( ) will result in word splitting and "$( )"will not do any word splitting at all, the problem appears to be that i want a bit of both, no word splitting at escaped whitepsace in the filenames, but wordsplitting bwteen the filenames, now i inderstand the idea of changing the IFS to be newline than the word splitiing will no longer happen at whitespace, only, that doesn't seem to work!:
[bascule at mycroft bascule]$ (IFS="\
"; id3v2 -l `cat ~/Desktop/play2 | sed -e 's!\\\\ ! !g' -e 's!//!/!g'`)
fopen: /mp3s/Alison Moyet/Weak In The Prescence Of Beauty/01_Weak In The Presence of Beauty.mp3
/mp3s/Alison Moyet/Weak In The Prescence Of Beauty/02_Ordinary Girl.mp3: No such file or directory
/mp3s/Alison Moyet/Weak In The Prescence Of Beauty/01_Weak In The Presence of Beauty.mp3
/mp3s/Alison Moyet/Weak In The Prescence Of Beauty/02_Ordinary Girl.mp3: No ID3 tag
substituting 'echo' for 'id3v2 -l' i get the exact opposite of what i was expecting:
[bascule at mycroft bascule]$ (IFS="\
"; echo `cat ~/Desktop/play2 | sed -e 's!\\\\ ! !g' -e 's!//!/!g'`)
/mp3s/Alison Moyet/Weak In The Prescence Of Beauty/01_Weak In The Presence of Beauty.mp3
/mp3s/Alison Moyet/Weak In The Prescence Of Beauty/02_Ordinary Girl.mp3
[bascule at mycroft bascule]$
remove the 'IFS=" line:
[bascule at mycroft bascule]$ echo `cat ~/Desktop/play2 | sed -e 's!\\\\ ! !g' -e 's!//!/!g'`
/mp3s/Alison Moyet/Weak In The Prescence Of Beauty/01_Weak In The Presence of Beauty.mp3 /mp3s/Alison Moyet/Weak In The Prescence Of Beauty/02_Ordinary Girl.mp3
this confuses me because
[bascule at mycroft bascule]$
so when the IFS includes spaces 'echo' will just print a list of words on the same line but remove the spaces and it magically put each word on a its own line?
when i saw your reference to IFS i went "aha! yes" cos if the only place words are split are newlines then there's no need to quote to avoid splitting at spaces and no need to remove the newlines from the file just cat to st. output in an expansion thus:
[bascule at mycroft bascule]$ (IFS="\
"; id3v2 -l $(cat ~/Desktop/play2))
fopen: /mp3s/Alison\ Moyet/Weak\ In\ The\ Prescence\ Of\ Beauty//01_Weak\ In\ The\ Presence\ of\ Beauty.mp3
/mp3s/Alison\ Moyet/Weak\ In\ The\ Prescence\ Of\ Beauty//02_Ordinary\ Girl.mp3: No such file or directory
/mp3s/Alison\ Moyet/Weak\ In\ The\ Prescence\ Of\ Beauty//01_Weak\ In\ The\ Presence\ of\ Beauty.mp3
/mp3s/Alison\ Moyet/Weak\ In\ The\ Prescence\ Of\ Beauty//02_Ordinary\ Girl.mp3: No ID3 tag
[bascule at mycroft bascule]$
but as you see no word splitting at all whereas:
[bascule at mycroft bascule]$ id3v2 -l $(cat ~/Desktop/play2)
fopen: /mp3s/Alison\: No such file or directory
/mp3s/Alison\: No ID3 tag
......
wordsplitting aplenty!
now this is very depressing because if i can't even understand how 'echo' works and wordspitting and IFS happen then there's no point trying to progress further in writing scripts, cos i'll get bitten all the time! :)
oh, and my head hurts!
bascule
On Saturday 03 May 2003 9:41 pm, Dave Cridland [Home] wrote:
> There's probably hundreds of solutions, my personal fave would be:
>
> (IFS="\
> "; id3v2 -l `cat ~/Desktop/play2 | sed -e 's!\\\\ ! !g' -e 's!//!/!g'`)
>
> I've done the "\ "=>" " translation in sed, because I have to, reading
> from a file, unless I did a "read".
>
> IFS is changed to a newline (escaped for the shell), and therefore I've
> run this in a subshell to avoid pollution. (IFS is, I think, Input Field
> Seperator).
--
"I don't know, " said the voice on the PA, "apathetic
bloody planet, I've no sympathy at all. "
More information about the Swlug
mailing list