Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] simple bash logic question
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Vieri
Apprentice
Apprentice


Joined: 18 Dec 2005
Posts: 232

PostPosted: Thu Oct 16, 2008 9:10 pm    Post subject: [SOLVED] simple bash logic question Reply with quote

Not strictly Gentoo-related but I'm wondering if someone could enlighten me on this.

I'd like to understand how "grouped conditions" work in bash. When I run the following code and the mount point doesn't exist, it runs the commands wthin || (). So, "error" is echoed out as expected. However, "continue" is also printed and if I check $? after the run I get 0.

Why doesn't the script "exit" with status 1 on the first line, within || () ?

The only way I found this to work as I want it to is by using the last lines I commented out.

Bash code:
Code:

#!/bin/bash
mount /furnstugurn || (echo "error"; exit 1)
echo "continue"
exit 0
#if [ $? -ne 0 ]; then
#    echo "error"; exit 1
#fi


Thanks in advance,

Vieri


Last edited by Vieri on Thu Oct 16, 2008 10:48 pm; edited 1 time in total
Back to top
View user's profile Send private message
yabbadabbadont
Advocate
Advocate


Joined: 14 Mar 2003
Posts: 4790
Location: 2 exits past crazy

PostPosted: Thu Oct 16, 2008 9:18 pm    Post subject: Reply with quote

By placing the echo and exit within parends (), you are running those commands within a sub-process that will "exit 1". Because it is a sub-process, it doesn't affect the main process.
Back to top
View user's profile Send private message
Vieri
Apprentice
Apprentice


Joined: 18 Dec 2005
Posts: 232

PostPosted: Thu Oct 16, 2008 10:48 pm    Post subject: Reply with quote

Thanks!
Back to top
View user's profile Send private message
elzbal
Guru
Guru


Joined: 31 Aug 2002
Posts: 364
Location: Seattle, WA, USA

PostPosted: Mon Oct 20, 2008 3:07 am    Post subject: Reply with quote

Just a reference note for future readers of this thread...

See also "man bash". Look at the "Shell Grammar" section, specifically the sections on "Compound Commands" and "Lists". There's a bit more information there about this.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum