How can I make several commands to be executed after a && or a ||?
Alexander Skwar
alexanders.mailinglists+nospam at gmail.com
Sat Feb 18 14:46:11 UTC 2012
Hi
On Sat, Feb 18, 2012 at 13:24, Johnny Rosenberg <gurus.knugum at gmail.com>wrote:
> 2012/2/18 Alexander Skwar <alexanders.mailinglists+nospam at gmail.com>:
> > Hi.
> >
> > 2012/2/17 Johnny Rosenberg <gurus.knugum at gmail.com>
> >>
> >>
> >> You know, something like this:
> >>
> >> ThisCommand && (
> >> echo $Something
> >> SomeVariable=3)
> >> DoSomethingElse
> >>
> >
> > Exactly this works. With "( … )", you spawn a subshell.
>
> Yes, I saw something about that when I googled for the answer, but I
> just didn't understand that subshell thing (maybe because my native
> language is not English). Seems like it creates a separate process.
>
> Does this mean that what's in the parentheses is executed at the same
> time as ”DoSomethingElse” in this case? Could that be a bad thing in
> some cases?
>
No, it's not run at the same time. It's run sequentially. Take your example:
>> ThisCommand && (
>> echo $Something
>> SomeVariable=3)
>> DoSomethingElse
Here, the two commands "echo $Something" and "SomeVariable=3"
are run prior to "DoSomethingElse".
It's more or less identical to the more verbose form:
if ThisCommand; then
echo $Something
SomeVariable=3
fi
DoSomethingElse
Difference: With "(echo $something; SomeVariable=3)", these
commands are run in a sub shell. Because of this, if you print
the content of SomeVariable after the subshell, you might be
surprised. Just give it a try!
>
> Another obvious solution would of course be to create a function that
> does everything inside the parentheses, and just call it on a single
> line, but that was not what I was asking about.
>
The function way would be pretty much identical to the sub shell
solution.
--
Alexander
--
↯ Lifestream (Twitter, Blog, …) ↣ http://alexs77.soup.io/ ↯
↯ Chat (Jabber/Google Talk) ↣ a.skwar at gmail.com , AIM: alexws77 ↯
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20120218/5292bc5a/attachment.html>
More information about the ubuntu-users
mailing list