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.
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.
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
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.)
On Sat, Jul 2, 2011 at 12:45 PM, Daniel B. Thurman dant@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
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))
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! :)
On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurman dant@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
On Saturday, July 02, 2011 02:11:52 PM inode0 wrote:
On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurman dant@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
On 07/02/2011 02:11 PM, inode0 wrote:
On Sat, Jul 2, 2011 at 2:07 PM, Daniel B. Thurmandant@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.
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. Thurmandant@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!
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. Thurmandant@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