LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-26-2009, 01:50 PM   #1
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
Brace expansion: does this kind exist?


Shell brace expansion is wonderful, but I've ran in to a snag a bunch of times now. The last time was when I wanted to mount two disks - mounted at /storage/1 and /storage/2, respectively. So, to save time, I did "mount /storage/{1,2}" which of course doesn't work, since it expands to "mount /storage/1 /storage/2", and /storage/1 is a directory, not a block device as it'd have to be in this case. What I'd like is for it to break it up into multiple commands, something like
mount /storage/{{1,2}} == for P in 1 2; do mount /storage/$P; done
or
echo {{1..100}} == for i in {1..100}; do echo $i; done

I'm pretty sure this isn't possible without patching your shell (at least bash). Please prove me wrong.
 
Old 04-26-2009, 04:16 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
No, you're right. And it (the patch) would have to be pretty clever to know what
to do for which commands ... that, or you'd have to introduce a new syntax.


Cheers,
Tink
 
Old 04-26-2009, 04:36 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
Something like this is better done using a short script or function that you can do later.

You could write a function that that calls mount for each argument. The arguments could be the result of brace expansion.
 
Old 04-26-2009, 04:43 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 359

Rep: Reputation: 170Reputation: 170
You need to escape the space after the command and add an escaped ';' at the end.
Then 'eval' can be used to do a double expansion on the expression.
Code:
eval mount\ /storage/{1,2}\;
mount: can't find /storage/1 in /etc/fstab or /etc/mtab
mount: can't find /storage/2 in /etc/fstab or /etc/mtab

eval echo\ {1..5}\;
1
2
3
4
5
 
Old 04-28-2009, 03:45 AM   #5
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
No, you're right. And it (the patch) would have to be pretty clever to know what
to do for which commands ... that, or you'd have to introduce a new syntax.
Tink
Well, I did introduce a new syntax in my post - double braces (lack of imagination? Who knows, but it doesn't really matter).

Quote:
Originally Posted by jschiwal View Post
Something like this is better done using a short script or function that you can do later.

You could write a function that that calls mount for each argument. The arguments could be the result of brace expansion.
This was just an example. It could also be for completely different tasks that also require one argument per invocation, so one script per deal would be pretty useless - it'd be a bit like arguing that you can use seq and xargs instead of brace expansion. Both works, but the latter is a lot prettier.

Quote:
Originally Posted by Kenhelm View Post
You need to escape the space after the command and add an escaped ';' at the end.
Then 'eval' can be used to do a double expansion on the expression.
Code:
eval mount\ /storage/{1,2}\;
mount: can't find /storage/1 in /etc/fstab or /etc/mtab
mount: can't find /storage/2 in /etc/fstab or /etc/mtab

eval echo\ {1..5}\;
1
2
3
4
5
Thanks
Ugly, but it works!
 
  


Reply

Tags
bash, expansion, shell


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
Avoiding Shell Script Brace Expansion Woodsman Slackware 4 05-31-2008 09:36 AM
bash: extra stuff in brace expansion jhwilliams Programming 4 09-07-2007 02:44 AM
Does this kind of distro exist? damg Linux - Distributions 14 08-28-2006 08:37 AM
kind of a programming quesion...kind of not tho jhorvath Programming 2 06-30-2003 10:05 PM
Raid 5 Expansion Ruckuss Linux - General 3 01-05-2002 05:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration