LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-07-2008, 09:27 PM   #1
gchilders
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Rep: Reputation: 0
Scripting Question - multiple {parameters} and variables


Hi all,
I'm stymied by an anomaly I find when using multiple parameters and variables in a script. For example:

ls -l /dir1/dir2/dir3/{file1,file2,file3}

will produce the proper results:

-rw-r--r-- 1 jdoe users 0 Oct 7 14:34 /dir1/dir2/dir3/file1
-rw-r--r-- 1 jdoe users 0 Oct 7 14:34 /dir1/dir2/dir3/file2
-rw-r--r-- 1 jdoe users 0 Oct 7 14:34 /dir1/dir2/dir3/file3

However, if I use variable substitution to execute the same command:

DIR=/dir1/dir2/dir3
FILES="file1,file2,file3"
ls -l $DIR/{$FILES}

will produce:

/bin/ls: /dir1/dir2/dir3/{file1,file2,file3}: No such file or directory

What do I need to do to successfully use variable substitution?

Thanks,
 
Old 10-07-2008, 11:57 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Try
Code:
DIR=/dir1/dir2/dir3
FILES="file1,file2,file3"
ls -l $(eval echo $DIR/{$FILES})
Code:
eval echo $DIR/{$FILES}
/dir1/dir2/dir3/file1 /dir1/dir2/dir3/file2 /dir1/dir2/dir3/file3
 
Old 10-08-2008, 12:03 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
The problem is that brace expansion occurs before variable expansion:

Code:
$ man bash:

EXPANSION
   ...
       The order of expansions is: brace expansion, tilde  expansion,  parame-
       ter,  variable  and arithmetic expansion and command substitution (done
       in a left-to-right fashion), word splitting, and pathname expansion.
Inside of the braces, when brace expansion occurs, there is the string of characters "$FILES", and this matches no file names. What remains is:

Code:
ls -l $DIR/$FILE
which is dutifully variable expanded:
Code:
ls -l /dir1/dir2/dir3/file1,file2,file3
and of course you see the consequences. In these situations, eval is your friend:
Code:
eval ls -l $DIR/$FILE
 
Old 10-08-2008, 08:31 AM   #4
gchilders
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Works like a charm

Thanks, that's exactly what I needed.

My actual original command was:
scp $SOURCEDIR/{$FILES} $HOST:$TARGETDIR
which does not work, even though:
echo "scp $SOURCEDIR/{$FILES} $HOST:$TARGETDIR"
printed out the command exactly as I wanted to see it.

But the correct format:
scp $(eval echo $SOURCEDIR/{$FILES}) $HOST:$TARGETDIR
now works correctly in the script.

Many thanks! I don't think I would have figured this out on my own.

Regards,
 
  


Reply

Tags
bash, expansion, variable


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
shell scripting/ bash/ a command with 3 parameters mayaabboud Programming 4 01-05-2008 10:22 AM
another simple unix scripting question! storing values in variables christianunix Linux - Newbie 4 10-30-2007 01:13 PM
scripting - using long variables primorec Linux - Software 4 11-05-2006 09:10 AM
getopts multiple parameters arnulfo Programming 2 12-23-2004 05:12 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM


All times are GMT -5. The time now is 01:32 PM.

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