FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 02-17-2012, 09:08 PM
Johnny Rosenberg
 
Default How can I make several commands to be executed after a && or a ||?

Something like the following, described in some kind of self-invented
pseudo code:

If ThisCommand exits successfully {
printf("Something");
SomeVariable=3;
}
DoSomethingElse


How could I do something similar with bash?

You know, something like this:

ThisCommand && (
echo $Something
SomeVariable=3)
DoSomethingElse


What's the syntax for this?



Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-17-2012, 09:29 PM
PleegWat
 
Default How can I make several commands to be executed after a && or a ||?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/17/2012 11:08 PM, Johnny Rosenberg wrote:
> Something like the following, described in some kind of
> self-invented pseudo code:
>
> If ThisCommand exits successfully { printf("Something");
> SomeVariable=3; } DoSomethingElse
>
>
> How could I do something similar with bash?
>
> You know, something like this:
>
> ThisCommand && ( echo $Something SomeVariable=3) DoSomethingElse
>
>
> What's the syntax for this?

What you've written should work; if it doesn't play with the whitespace.

However, shell script also has an if command. This is even usable on
the interactive shell.

if ThisCommand
then
echo $Something
SomeVariable=3
fi
DoSomethingElse

PleegWat
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPPtTiAAoJEAM6sLJjDJaMEIcH/19e9a3q1gH87gqFG1vsQK9l
OD2Ec50AD60oCjS9QAYnLxp8VG820PSKfABpaxQ8hZw0Hf+BwT 3N+30KLFk64chx
zxMR+V5QBhf8+wZrFGnWVLcbd2n+DiNzh4F88/TK7jjVCC1Q8A5lR8N+WMQaTCLx
BJQZb4mMAHuHdvCm5RgHFjbUvF4PkUIlBBFNiPw/soOq/FWtSIx1CtxHvafEWrsD
jxHfEcRmfi5dsq7BPHTqKkTt3L/rM6MSALfEV9p+IY/tESEEH9JF6Bie+4qThysB
u7g56jkqJvuGvF7WBYF2iGxlne+qLYyAam4WRkMjIHkIpnqjFO a2c+7xl4XVhQM=
=FjsE
-----END PGP SIGNATURE-----

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-18-2012, 07:17 AM
Alexander Skwar
 
Default How can I make several commands to be executed after a && or a ||?

Hi.

2012/2/17 Johnny Rosenberg <gurus.knugum@gmail.com>




You know, something like this:



ThisCommand && (

* * * *echo $Something

* * * *SomeVariable=3)

DoSomethingElse



Exactly this works. With "( … )", you spawn a subshell.


Alexander
--
↯*** Lifestream (Twitter, Blog, …) ↣*http://alexs77.soup.io/* ** ↯


↯ Chat (Jabber/Google Talk) ↣ a.skwar@gmail.com , AIM: alexws77* ↯


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-18-2012, 11:17 AM
Johnny Rosenberg
 
Default How can I make several commands to be executed after a && or a ||?

2012/2/17 PleegWat <pleegwat@telfort.nl>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 02/17/2012 11:08 PM, Johnny Rosenberg wrote:
>> Something like the following, described in some kind of
>> self-invented pseudo code:
>>
>> If ThisCommand exits successfully { printf("Something");
>> SomeVariable=3; } DoSomethingElse
>>
>>
>> How could I do something similar with bash?
>>
>> You know, something like this:
>>
>> ThisCommand && ( echo $Something SomeVariable=3) DoSomethingElse
>>
>>
>> What's the syntax for this?
>
> What you've written should work; if it doesn't play with the whitespace.
>
> However, shell script also has an if command. This is even usable on
> the interactive shell.
>
> if ThisCommand
> then
> * * * *echo $Something
> * * * *SomeVariable=3
> fi
> DoSomethingElse

Thanks for replying. The reason I asked was not that I tried make
something work and failed, I was rather that I was trying to do it in
a specific way, so my question was only about && and || respectively,
and nothing else. I guess I could have mentioned that. My bad, sorry.


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-18-2012, 11:24 AM
Johnny Rosenberg
 
Default How can I make several commands to be executed after a && or a ||?

2012/2/18 Alexander Skwar <alexanders.mailinglists+nospam@gmail.com>:
> Hi.
>
> 2012/2/17 Johnny Rosenberg <gurus.knugum@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?

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.



Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-18-2012, 01:46 PM
Alexander Skwar
 
Default How can I make several commands to be executed after a && or a ||?

Hi

On Sat, Feb 18, 2012 at 13:24, Johnny Rosenberg <gurus.knugum@gmail.com> wrote:


2012/2/18 Alexander Skwar <alexanders.mailinglists+nospam@gmail.com>:



> Hi.

>

> 2012/2/17 Johnny Rosenberg <gurus.knugum@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=3fiDoSomethingElse


Difference: With "(echo $something; SomeVariable=3)", thesecommands are run in a sub shell. Because of this, if you printthe 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 shellsolution.*
--
Alexander


--
↯*** Lifestream (Twitter, Blog, …) ↣*http://alexs77.soup.io/* ** ↯
↯ Chat (Jabber/Google Talk) ↣ a.skwar@gmail.com , AIM: alexws77* ↯




--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 04:17 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org