LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash: how ls content of a variable in bash script (http://www.linuxquestions.org/questions/programming-9/bash-how-ls-content-of-a-variable-in-bash-script-894147/)

porphyry5 07-27-2011 03:17 PM

bash: how ls content of a variable in bash script
 
I have a variable containing various file and directory paths, thus:
Code:

echo $brn
"/home/g/ToBurn/Fugitives (1986) Les fugitifs" "/home/g/ToBurn/Kosmos-[2010]-DVDRIP-[TUR]-Hitro" "/home/g/ToBurn/Night.on.Bald.Mountain.avi" "/home/g/ToBurn/Riget II/R2E2" "/home/g/ToBurn/Riget II/R2E4" "/home/g/ToBurn/Riget II/R2E1" "/home/g/ToBurn/Riget1/R1E3" "/home/g/ToBurn/Riget1/R1E2"

I have tried the following: "ls $brn"; ls $brn; echo $brn|ls; echo "$brn"|ls; cat <<< $brn|ls; ls $(printf "%q" "$brn"). None of these produce the desired result, that I get from
Code:

ls "/home/g/ToBurn/Fugitives (1986) Les fugitifs" "/home/g/ToBurn/Kosmos-[2010]-DVDRIP-[TUR]-Hitro" "/home/g/ToBurn/Night.on.Bald.Mountain.avi" "/home/g/ToBurn/Riget II/R2E2" "/home/g/ToBurn/Riget II/R2E4" "/home/g/ToBurn/Riget II/R2E1" "/home/g/ToBurn/Riget1/R1E3" "/home/g/ToBurn/Riget1/R1E2"
/home/g/ToBurn/Night.on.Bald.Mountain.avi

/home/g/ToBurn/Fugitives (1986) Les fugitifs:
Les fugitifs (Francis Veber, 1986).avi  Les fugitifs (Francis Veber, 1986).srt

/home/g/ToBurn/Kosmos-[2010]-DVDRIP-[TUR]-Hitro:
Kosmos-[2010]-DVDRIP-[TUR]-Hitro Eng.srt  Kosmos-[2010]-DVDRIP-[TUR]-Hitro.avi

/home/g/ToBurn/Riget II/R2E1:
The Kingdom s2e1.avi  The Kingdom s2e1.srt

/home/g/ToBurn/Riget II/R2E2:
The Kingdom s2e2.avi  The Kingdom s2e2.srt

/home/g/ToBurn/Riget II/R2E4:
The Kingdom s2e4.avi  The Kingdom s2e4.srt

/home/g/ToBurn/Riget1/R1E2:
The Kingdom s1e2.avi  The Kingdom s1e2.srt

/home/g/ToBurn/Riget1/R1E3:
The Kingdom s1e3.avi  The Kingdom s1e3.srt


crts 07-27-2011 03:29 PM

Hi,

try it this way:
Code:

var="'dir' 'Liszt - Piano Concertos 1 & 2'"
eval ls ${var}

[EDIT]
You can also use the quotes "the other way around"
Code:

var='"dir" "Liszt - Piano Concertos 1 & 2"'

grail 07-27-2011 03:38 PM

I am not sure I understand the issue? How is the output you have shown wrong or could you maybe show what you are expecting?

porphyry5 07-27-2011 04:23 PM

Quote:

Originally Posted by crts (Post 4426973)
Hi,

try it this way:
Code:

var="'dir' 'Liszt - Piano Concertos 1 & 2'"
eval ls ${var}

[EDIT]
You can also use the quotes "the other way around"
Code:

var='"dir" "Liszt - Piano Concertos 1 & 2"'

Thank you, perfect. It seems to be a truism of bash that; if it looks like it should work but doesn't, then eval the damn thing.

porphyry5 07-27-2011 04:37 PM

Quote:

Originally Posted by grail (Post 4426982)
I am not sure I understand the issue? How is the output you have shown wrong or could you maybe show what you are expecting?

My point was that, though the variable $brn contained a string that supplied everything that ls needed to list the files correctly, doing
Code:

ls $brn
produced output that revealed ls to be ignoring the "s that enclosed each path and was breaking them up on every included space. Whereas, if I simply pasted the content of $brn after typing ls in a terminal, it performed as expected.

I've so far not been able to figure out a reliable rule to predict how bash will deliver the content of a variable. Mostly, it does as expected, sometimes, like this example, it doesn't. But it does seem to be generally true that if such content is not delivered as expected, then eval will fix it for you.

grail 07-27-2011 08:45 PM

ahhh ... with you now :) Sorry had my blinkers on when reading this one.

tuxdev 07-28-2011 02:10 PM

I'd like to know what you're actually trying to do with the script. Pretty much all uses of "eval" are better done using some other method. Also attempting to do anything with ls besides display to the user is rife with problems.


All times are GMT -5. The time now is 11:47 AM.