Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Daniel B. Thurman

I seem to forget my shell programming
but is the following statement valid?

($foo==0)?foo=1:foo=0

I thought it was called the tristate conditional
operator but in any case I could not find it in
google.


-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Joe Zeff
On 07/02/2011 10:45 AM, Daniel B. Thurman wrote:

 I seem to forget my shell programming
 but is the following statement valid?

 ($foo==0)?foo=1:foo=0

 I thought it was called the tristate conditional
 operator but in any case I could not find it in
 google.



I don't know if it's allowed in bash, or any other shell.  I do know, 
however, that it's a valid construct in C.
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Marko Vojinovic
On Saturday 02 July 2011 18:50:46 Joe Zeff wrote:
 On 07/02/2011 10:45 AM, Daniel B. Thurman wrote:
  I seem to forget my shell programming
  but is the following statement valid?
  
  ($foo==0)?foo=1:foo=0
  
  I thought it was called the tristate conditional
  operator but in any case I could not find it in
  google.
 
 I don't know if it's allowed in bash, or any other shell.  I do know,
 however, that it's a valid construct in C.

With a small detail that the $ in $foo is not actually a valid C construct 
itself, right?

Or were you looking at the subject line, where the code doesn't have the $? 
;-)

Best, :-)
Marko

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Joe Zeff
On 07/02/2011 11:01 AM, Marko Vojinovic wrote:
 With a small detail that the $ in $foo is not actually a valid C construct
 itself, right?

 Or were you looking at the subject line, where the code doesn't have the $?

The trinary operator itself is a valid C construct, even though the 
example given wouldn't work.  (Thanx for pointing out the error; it's 
been way, way too long since I actually did any programming and didn't 
notice it.)
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread inode0
On Sat, Jul 2, 2011 at 12:45 PM, Daniel B. Thurman d...@cdkkt.com wrote:

 I seem to forget my shell programming
 but is the following statement valid?

 ($foo==0)?foo=1:foo=0

 I thought it was called the tristate conditional
 operator but in any case I could not find it in
 google.

I think you might be looking for something similar to

foo=$(($foo==0 ? 1 : 0))

John
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Robert Nichols
On 07/02/2011 12:45 PM, Daniel B. Thurman wrote:

 I seem to forget my shell programming
 but is the following statement valid?

 ($foo==0)?foo=1:foo=0

 I thought it was called the tristate conditional
 operator but in any case I could not find it in
 google.

You need to enclose the entire expression in double parentheses to make
bash parse it as an expression.  Plus, your syntax is slightly wrong:

 ((($foo==0)?foo=1:0))

or, since within an expression the '$' to reference a variable is optional:

 (((foo==0)?foo=1:0))

and, if you want to insert the result directly into a command line:

 echo $(((foo==0)?1:0))
or
 echo $(((foo==0)?foo=1:0))

-- 
Bob Nichols NOSPAM is really part of my email address.
 Do NOT delete it.

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Daniel B. Thurman
On 07/02/2011 11:30 AM, Robert Nichols wrote:
 On 07/02/2011 12:45 PM, Daniel B. Thurman wrote:
 I seem to forget my shell programming
 but is the following statement valid?

 ($foo==0)?foo=1:foo=0

 I thought it was called the tristate conditional
 operator but in any case I could not find it in
 google.
 You need to enclose the entire expression in double parentheses to make
 bash parse it as an expression.  Plus, your syntax is slightly wrong:

  ((($foo==0)?foo=1:0))

 or, since within an expression the '$' to reference a variable is optional:

  (((foo==0)?foo=1:0))

 and, if you want to insert the result directly into a command line:

  echo $(((foo==0)?1:0))
 or
  echo $(((foo==0)?foo=1:0))

Thanks - this is good information!
I used:  (((foo==0)?foo=1:0)) and it works in a bash script!

Will add this to my bookmarks! :)

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread inode0
On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurman d...@cdkkt.com wrote:
 I used:  (((foo==0)?foo=1:0)) and it works in a bash script!

I don't think that is quite the same as what I'm guessing your
original attempt intended. In this case if foo does not equal 0 to
begin with it won't be set to 0. Perhaps that doesn't matter in your
particular case.

John
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Rick Sewill
On Saturday, July 02, 2011 02:11:52 PM inode0 wrote:
 On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurman d...@cdkkt.com wrote:
  I used:  (((foo==0)?foo=1:0)) and it works in a bash script!
 
 I don't think that is quite the same as what I'm guessing your
 original attempt intended. In this case if foo does not equal 0 to
 begin with it won't be set to 0. Perhaps that doesn't matter in your
 particular case.
 
 John

If you know that foo is always initialized to either a value of zero or one, 
would the following seem reasonable?
let foo=1-$foo
-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Robert Nichols
On 07/02/2011 02:11 PM, inode0 wrote:
 On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurmand...@cdkkt.com  wrote:
 I used:  (((foo==0)?foo=1:0)) and it works in a bash script!

 I don't think that is quite the same as what I'm guessing your
 original attempt intended. In this case if foo does not equal 0 to
 begin with it won't be set to 0. Perhaps that doesn't matter in your
 particular case.

Indeed, I got it wrong too.  Looks like it needs to be:

 (((foo==0)?(foo=1):(foo=0)))

Frankly, I don't think I'll be using that syntax much.  Too many ways
to get it wrong.

-- 
Bob Nichols NOSPAM is really part of my email address.
 Do NOT delete it.

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Daniel B. Thurman
On 07/02/2011 02:15 PM, Robert Nichols wrote:
 On 07/02/2011 02:11 PM, inode0 wrote:
 On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurmand...@cdkkt.com  wrote:
 I used:  (((foo==0)?foo=1:0)) and it works in a bash script!
 I don't think that is quite the same as what I'm guessing your
 original attempt intended. In this case if foo does not equal 0 to
 begin with it won't be set to 0. Perhaps that doesn't matter in your
 particular case.
 Indeed, I got it wrong too.  Looks like it needs to be:

  (((foo==0)?(foo=1):(foo=0)))

 Frankly, I don't think I'll be using that syntax much.  Too many ways
 to get it wrong.

Yeah, you guys are correct, it does not toggle
which was my intent.  So I basically went back
to the trusty if-else-fi statement of which anyone
can read without much obscurity. :P

Thanks to all, for contributing!

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


Re: Bash: (foo==0)?foo=1:foo=0 valid?

2011-07-02 Thread Patrick O'Callaghan
On Sat, 2011-07-02 at 14:31 -0700, Daniel B. Thurman wrote:
 On 07/02/2011 02:15 PM, Robert Nichols wrote:
  On 07/02/2011 02:11 PM, inode0 wrote:
  On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurmand...@cdkkt.com  wrote:
  I used:  (((foo==0)?foo=1:0)) and it works in a bash script!
  I don't think that is quite the same as what I'm guessing your
  original attempt intended. In this case if foo does not equal 0 to
  begin with it won't be set to 0. Perhaps that doesn't matter in your
  particular case.
  Indeed, I got it wrong too.  Looks like it needs to be:
 
   (((foo==0)?(foo=1):(foo=0)))
 
  Frankly, I don't think I'll be using that syntax much.  Too many ways
  to get it wrong.
 
 Yeah, you guys are correct, it does not toggle
 which was my intent.  So I basically went back
 to the trusty if-else-fi statement of which anyone
 can read without much obscurity. :P
 
 Thanks to all, for contributing!
 

((foo=!foo))

or if using an interactive Shell:

((foo=\!foo))

poc

-- 
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines