LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2011, 02:17 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 421

Rep: Reputation: 18
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
 
Old 07-27-2011, 02:29 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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"'

Last edited by crts; 07-27-2011 at 02:30 PM.
 
1 members found this post helpful.
Old 07-27-2011, 02:38 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
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?
 
Old 07-27-2011, 03:23 PM   #4
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 421

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by crts View Post
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.
 
Old 07-27-2011, 03:37 PM   #5
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 421

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
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.
 
Old 07-27-2011, 07:45 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
ahhh ... with you now Sorry had my blinkers on when reading this one.
 
Old 07-28-2011, 01:10 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[bash] Read file's content into variable problem Coolmax Programming 6 08-31-2009 04:12 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Bash script to strip some content from XML file. musther Programming 10 10-14-2007 05:47 PM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 01:41 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration