LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Brace expansion: does this kind exist? (http://www.linuxquestions.org/questions/linux-software-2/brace-expansion-does-this-kind-exist-721839/)

exscape 04-26-2009 01:50 PM

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. ;)

Tinkster 04-26-2009 04:16 PM

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

jschiwal 04-26-2009 04:36 PM

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.

Kenhelm 04-26-2009 04:43 PM

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


exscape 04-28-2009 03:45 AM

Quote:

Originally Posted by Tinkster (Post 3521753)
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 (Post 3521765)
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 (Post 3521771)
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!


All times are GMT -5. The time now is 09:00 AM.