LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 01-18-2017, 01:57 PM   #61
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,207

Rep: Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419

hey, BW-userx your response is not ok. In general. I mean you posted an example when the two different constructs work similarly and when we try to explain there are other cases - when they will not work the same way - you refuses to accept this because we changed it. But actually your cases to prove your statements are working the same way and your examples only prove there are cases when the two constructs may give the same result - so your cases will not prove or rule out there are other cases when the two constructs are different. You refuses to accept that, because we modified your test cases. But actually it is not about the test cases you have, but the syntax and the language, which accepts both your examples and ours.
 
Old 01-18-2017, 02:08 PM   #62
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,207

Rep: Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419
Quote:
Originally Posted by BW-userx View Post
because it is more then 3 arguments.

you got 4 now
no, it is just 3, because && and || works on exit codes, { } will usually "deliver" the exit code of the last command, in this case: [
 
Old 01-18-2017, 02:09 PM   #63
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
Quote:
Originally Posted by BW-userx View Post
because that is all I was going to do with them? then figured out I didn't even need to use them in the first place. now it is turned into a big argument pissing contest. Which I didn't mind until I see the arguments that where being used against me where lies.

the things done to prove me wrong were never going to work in the first place.
...

showing me something that is testing if it is greater then itself is what?
and writing bad code that will never work is what?
and manipulating data so the code will never work is what?

all to try and prove me wrong.

prove me wrong with honestly and I will show you my humility.
Take a deep breath and re-read the examples given.

No one is making a personal attack on you or making false claims, all are trying to be helpful and presenting some excellent examples.

Code:
 true ? yes  : no

    !=

[ true ] && yes || no
If you do not understand why then you need to carefully consider the cases where it fails and not think of them as contrived or false attempts to discredit you.
 
Old 01-18-2017, 02:11 PM   #64
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by pan64 View Post
That is false, and actually twice.
1. it is not a replacement, it works differently.
2. no one ever wrote anything in bash to interpret it, therefore it does not exist, && || is not meant to do that.

No, no and again, no. It is not the same thing, not the same construct, same functionality. Although there are cases when they behave quite similarly (as both the frog and horse may have 4 legs), but otherwise they are definitely different.

What was not mentioned, but probably helps you to understand: using the ?: construct you have only one operator and one result, using && || you will have two operators and both of them will work independently to each other (but the result of the first one will influence the result of the second one).

So if you think one operator or two independent operators will do the same work....
I have yet to see actually prof only speculation in how it is being said.

in words only,
Quote:
No, no and again, no. It is not the same thing, not the same construct, same functionality. Although there are cases when they behave quite similarly (as both the frog and horse may have 4 legs), but otherwise they are definitely different.
not actual data

here you state in words only:
Quote:
What was not mentioned, but probably helps you to understand: using the ?: construct you have only one operator and one result, using && || you will have two operators and both of them will work independently to each other (but the result of the first one will influence the result of the second one).
they both work independently of each other yes! they where written to do so, BUT what defines a "ternary operator"?

Quote:
In computer science, a ternary operator is an operator that takes three arguments.
The arguments and result can be of different types. Many programming languages that use C-like syntax feature a ternary operator, ?: ,
which defines a conditional expression.
this ?: was written to be used together so that the definition of "ternary operator" could evaluate to true. It specifically takes three ( ternary or trinity which indicates three (3) parts) to get a result one way or the other it will preform an operation no matter what and not be skipped over because of the way it is constructed.

it first checks to see if a statement being the first argument is true. if yes then whatever is next it executes ?
if it is a false statement then it skips the ? and goes to the next argument after : and executes that instead.

the results are as stated. it will preform an operation no matter what.


taking the && and the ||

as stated the && only works if the return is a zero period else it skips over it and goes on to the next line of code.
how do we stop it from doing that?

well someone came up with the brilliant idea of doing this

taking the || and making it only work off of non zero return values.


sure maybe this ?: was an after thought. because it can only work when uses together. where as the && and || can NOT only work by themselves, but yes they too work together to allow 3 arguments to be taken in and evaluated and give only one result. doing the same thing. remember it is only 3 arguments that can be used in this construct when using the [ ] && || just like ?: can only take three arguments.


[ ] && || only allow 3 arguments else the desired results are not had. it is open to human error not itself error.

where as ?: is not

it will still preform an operation no matter what and not be skipped over because of the way it is constructed. but only 3 arguments are allowed. else it is no longer a trinity. it is something else all together.

Last edited by BW-userx; 01-18-2017 at 02:17 PM.
 
Old 01-18-2017, 02:20 PM   #65
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
I will not try to rehash the previously given examples, but perhaps a fresh example will help.

Code:
#!/bin/bash

# Ternary expressions with bash
#    condition1 ? result1 : result2

# Note that only the first term is a conditional - the next two are alternative results
# which depend ONLY on condition1.

# In other languages the ternary "operator" returns a value
# So to be exactly equivalent you need a construct that returns a value too.

# Looking only at the logical construct, this is often proposed as being ternary,
# but it is not! This example demonstrates the error...
# Create a file named somefile, remove the file nofile if exists, then...

[ "1" == "1" ] && ls -l nofile 2>/dev/null || echo "FALSE"
[ "1" == "1" ] && ls -l somefile 2>/dev/null || echo "FALSE"

# Reason: result is determined by condition1 AND result1, not ONLY condition1!
# It is logically different than the ternary operator in which the result depends
# ONLY on condition1.
Hope that helps.
 
Old 01-18-2017, 02:21 PM   #66
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by astrogeek View Post
Take a deep breath and re-read the examples given.

No one is making a personal attack on you or making false claims, all are trying to be helpful and presenting some excellent examples.

Code:
 true ? yes  : no

    !=

[ true ] && yes || no
If you do not understand why then you need to carefully consider the cases where it fails and not think of them as contrived or false attempts to discredit you.
stupid

Code:
!=
that still equates to a true

Code:
 
if [ 1 != 2 ] ; then
yes
else
no
fi
it is a true statement no matter if it is a negative or a positive. go take a basic logic course. I am glad I did have to to see that.



furthermore writing code that will never work or changing my code so it will not work is not proving their case nor is it excellent examples.

Last edited by BW-userx; 01-18-2017 at 02:23 PM.
 
Old 01-18-2017, 02:22 PM   #67
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,207

Rep: Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419
ok, my wife explained: it does not work for you, especially the writing of /tmp/myfile was that, so you need to think about it. Can you explain what's happened? Why do we need to tell you again and again
Quote:
"I didn't mind until I see the arguments that where being used against me where lies."
we did not lie, we have examples what you ignores just because you cannot handle them.
 
Old 01-18-2017, 02:41 PM   #68
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
Quote:
Originally Posted by BW-userx View Post
stupid

...
it is a true statement no matter if it is a negative or a positive. go take a basic logic course. I am glad I did have to to see that.

furthermore writing code that will never work or changing my code so it will not work is not proving their case nor is it excellent examples.
Please refrain from use of perjorative terms or expressions when replying to others. Personal attacks are not allowed on LQ.

Providing counter examples is in fact the most excellent foundation method of logical proofs. Please try to understand the point of those examples instead of rejecting them as attempts to somehow discredit yourself.
 
1 members found this post helpful.
Old 01-18-2017, 03:00 PM   #69
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by astrogeek View Post
I will not try to rehash the previously given examples, but perhaps a fresh example will help.

Code:
#!/bin/bash

# Ternary expressions with bash
#    condition1 ? result1 : result2
that is three arguments separated by ? and :

this is three argument separated by [ ] && ||

Code:
[ 1 != 2 ] && echo "yes" || echo "NO"
it is a true statement so yes is returned.

Code:
[ 1 == 2 ] && echo "yes" || echo "no"
it is not a true statement so what takes place?
it always test for true if true then do something else do something else
if/then/else

Ternary means what? 3Ternary Operator

discount this something I did not say

http://www.cprogramming.com/referenc...-operator.html

Quote:
<condition> ? <true-case-code> : <false-case-code>;
more of same
Code:
The ternary operator allows you to execute different code depending on the value of a condition,
 and the result of the expression is the result of the executed code. For example:

int five_divided_by_x = ( x != 0 ? 5 / x : 0 );

Here, x != 0 is checked first, and if it is true, then the division, 5/x, takes place.

Otherwise, the value is 0. The ternary operator is excellent for situations where you 
need to choose two different values depending on a single condition, but it is not a 
good general-purpose substitute for if/else statements.

The ternary operator is particularly useful when initializing const 
fields of a class as those fields must be initialized inside the initialization list, 
where if/else statements cannot be used (read more about initialization lists in C++). For example:
but it does not state to not use it nor does it prove why it is not a good general-purpose substitute for if/else statements.

that is all I see are words being repeated without any solid prof to back it up.


next part I am now looking at.

Quote:

# Note that only the first term is a conditional - the next two are alternative results
# which depend ONLY on condition1.

# In other languages the ternary "operator" returns a value
your point is?

that is what the "ternary operator" does. it is one term only. quit changing the truth to try and prove your case.

it depends one the first arguments condition (true) or (false) to give the results either ? here : or here.



Quote:
# So to be exactly equivalent you need a construct that returns a value too.

# Looking only at the logical construct, this is often proposed as being ternary,
# but it is not! This example demonstrates the error...
# Create a file named somefile, remove the file nofile if exists, then...
read this

key words to get it where
"operator programming defined"
Code:
In mathematics and sometimes in computer programming, an
 operator is a character that represents an action,
 as for example x is an arithmetic operator that represents multiplication. 
In computer programs, one of the most familiar sets of operators, 
the Boolean operators, is used to work with true/false values.
true = 0
false = non zero

we then act on that value to return the results.

here you now are no longer doing what you are telling me prior to this to prove you point, you ...

So to be exactly equivalent you need a construct that returns a value too.

here you are doing what?
Is that returning a value?

no it is preforming an operation.

Quote:


[ "1" == "1" ] && ls -l nofile 2>/dev/null || echo "FALSE"
[ "1" == "1" ] && ls -l somefile 2>/dev/null || echo "FALSE"

# Reason: result is determined by condition1 AND result1, not ONLY condition1!
# It is logically different than the ternary operator in which the result depends
# ONLY on condition1.
[/CODE]

Hope that helps.
condition one is being met so it is preforming a action off whatever it is. not what you are saying.

1 does equal 1 so what do you think it will do next?
so of course that first one right after the && is going to be preformed.

if you'd have wrote it like this

Code:
[ '1' == '2'] &&  ls -l nofile 2>/dev/null || echo "FALSE"
what do you think will happen?

condition1 is a lie .. not a true statement so || echo "FALSE" is now result2 that depended on what?
the condistion1 state that returns a value of a non zero.

so when you say

So to be exactly equivalent you need a construct that returns a value too.

they all return a value
( ?: )
[ ] && ||

if / then/ else

Last edited by BW-userx; 01-18-2017 at 03:07 PM.
 
Old 01-18-2017, 04:07 PM   #70
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
@astrogeek
@pan64
@szboardstretcher
@grail
@ and every body else who whats to read this and learn something.

You are thinking in separate terms when you need to think in combined terms because that is what is taking place here.

Code:
[ "1" == "1" ] && ls -l nofile 2>/dev/null || echo "FALSE"
[ "1" == "1" ] && ls -l somefile 2>/dev/null || echo "FALSE"
they are now being used in a combined construct.

so this thinking is now in error
Quote:
# Reason: result is determined by condition1 AND result1, not ONLY condition1!
# It is logically different than the ternary operator in which the result depends
# ONLY on condition1.
taking what @szboardstretcher posted the documentation on && and ||

https://www.gnu.org/software/bash/ma...ode/Lists.html


Quote:

AND and OR lists are sequences of one or more pipelines separated by
the control operators ‘&&’ and ‘||’, respectively. AND and OR lists
are executed with left associativity.


An AND list has the form

command1 && command2

command2 is executed if, and only if,
command1 returns an exit status of zero.
if the left side is zero then &&
if the left side is non zero then ||


that is this construct

no directory is present we just know that so just to be sure in case we are wrong, we check to be sure no directory is present first, if true then issue command2.

Code:
[ test ] && do something
 command1     &&     command2
[[ ! -d "$move_to" ]]  && mkdir -v "$move_to"
that first command returns a zero because their was no directory present. so command2 is issued. If their had been a directory by that name then it would not have been issued.

Quote:
command2 is executed if, and only if,
command1 returns an exit status of zero.


it is the same as an if statement.

Code:
if [ ! -d  "$move_to" ] ; then
    mkdir -v "$move_to"
the same results will be had if that named directory is not there. because it is a true statement. if directory not present then make directory present.

the first is just short hand coding.


now look at this separately. it does the opposite effect

Quote:

An OR list has the form

command1 || command2

command2 is executed if, and only if,
command1 returns a non-zero exit status.

The return status of AND and OR lists is the exit status
of the last command executed in the list.
therefor the code would look like this.


if the directory is not there we can check for it if not present then make directory present can now be written like this.

look and see what changes have taken place.


Code:
[[ -d "$move_to" ]] ||  mkdir -pv "$move_to"
if looks to see if a directory by that value inside of the variable is the same if it is not the it will return a non zero value then execute the code to the right of ||

an if statement would have to put an else in it to work and would look like this because it returns a non zero.

Code:
if [ -d "$move_to ] ; then
echo "backswords dude"
else
 mkdir -v "$move_to"

fi
the test is looking for a true to be inside of it or it will not work if not given something ELSE to do.

now if we put the two together then we can use the effects together to get it to do one thing or the other.


hence the "ternary operator" effect because we need to have three arguments for this to take place.

it is three operations clumped together making it one operator. ?: it is useless on its own. the one written in C and used in C and C++ and perhaps others as well just NOT bash because the people that wrote BASH did not add that to BASH.

but BASH does not have to write one because ones that use BASH can figure it out on there own if they are thinking properly.


because the brackets test for true and will return a value of zero if true and a non zero if not true BASH users can now capitaleze on that when putting these three things together.

we already know that this && only works with zero
and
we already know that this || only works with non zero
and
we know that this [ ] will return one or the other.


if we put it all together we can then create a ternary operator with what we have to work with.
Quote:
[ ] && ||
need I explain further?

Code:
[ returns a value ] && returned 0 || returned non zero
that is how it is NOW a ternary operator just like ( ?: )

no more no less it can only work properly if only 3 arguments are used. just like [ ] and && and || can only work to their specification that they where written for.

I am right and the rest of you are still wrong and your pride is stopping you from being humble?

hummm
time will tell.

read this

Conditional (ternary) Operator

Last edited by BW-userx; 01-18-2017 at 04:36 PM.
 
Old 01-18-2017, 04:47 PM   #71
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Now just like Java uses it from the last link i added
combining the ternary operator
Multiple ternary evaluations are also possible

Code:
#!/bin/bash


[ -d ~/testme/testme ] && mkdir -v ~/testme/testme2 || mkdir -v ~/testme/testme3 && echo "I did it"
returns a non zero then a zero after || because the directory ~/testme/testme3 was created.
results

Code:
[testme]>>$ ./nowwhat
mkdir: created directory '/home/userx/testme/testme3'
I did it

Last edited by BW-userx; 01-18-2017 at 04:57 PM.
 
Old 01-18-2017, 04:54 PM   #72
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
Quote:
Originally Posted by BW-userx
Code:
# In other languages the ternary "operator" returns a value
your point is?

Code:
# Looking only at the logical construct, this is often proposed as being ternary,
My point was only to be complete, I then said clearly that we were now "Looking only at the logical construct". That is, we are ignoring the return value aspect for now.

Quote:
Originally Posted by BW-userx View Post

if we put it all together we can then create a ternary operator with what we have to work with.

need I explain further?

Code:
[ returns a value ] && returned 0 || returned non zero
that is how it is NOW a ternary operator

no more no less it can only work properly if only 3 arguments are used. just like [ ] and && and || can only work to their specification that they where written for.
No it is not a ternary operator, it is still two binary operators, && and ||, with different evaluation precedence and result.

You are interested in logic, so you surely understand truth table notation. So lets look at the truth table of each of these expressions.

Code:
Ternary truth table

X = True
!X = Not true
x = Don't care
(X) = Whatever X is
Res = result

 A  ? B  : C

 A    B    C    Res
___________________
 A |  B |  C | (B)
 A | !B |  C | (B)
 A |  B | !C | (B)
 A | !B | !C | (B)
!A |  B |  C | (C)
!A | !B |  C | (C)
!A |  B | !C | (C)
!A | !B | !C | (C)

Which DeMorgan would reduce to this...

 A    B    C    Res
___________________

 A |  x |  x | (B)
!A |  x |  x | (C)
Now look at the truth table for the two binary operators in series...

Code:
 A && B || C

 A    B    C    Res
___________________
 A |  B |  C | (B)
 A | !B |  C | (C)
 A |  B | !C | (B)
 A | !B | !C | (C)
!A |  B |  C | (C)
!A | !B |  C | (C)
!A |  B | !C | (C)
!A | !B | !C | (C)

Which reduces to this...

 A    B    C    Res
___________________

 A |  B |  x | (B)
 A | !B |  x | (C)
!A |  x |  x | (C)
The two expressions have different truth tables and produce different results and are therefore not equivalent.

Code:
 A    B    C    Res
___________________

 A |  x |  x | (B)
!A |  x |  x | (C)

Clearly !=

 A    B    C    Res
___________________

 A |  B |  x | (B)
 A | !B |  x | (C)
!A |  x |  x | (C)
Which is the main point of the various examples posted.

This was clearly stated by rnichols back in post #5:

Quote:
Originally Posted by rknichols View Post
No, you cannot use "else" without "if ... then", but you can accomplish it this way:
Code:
[[ some condition ]] && { code block 1; } || { code block 2; }
The catch (familiar to Python programmers) is that you hve to ensure that code block 1 always ends with a "true" condition (zero return code). You can do that by making the last command in code block 1 one that always succeeds, like "true" or ":".

Doing that should be an intellectual exercise only. In the real world, "if ... then ... else ... fi" would be preferable.
In other words, if we guarantee that B is always true, thus eliminating the middle row of the second truth table, then they become a special case which produces the same result:

Code:
 A    B    C    Res
___________________

 A |  B |  x | (B)
 A | !B |  x | (C)
!A |  x |  x | (C)
But that is something extra that must be done by the code, it is not inherent in the logic.
 
1 members found this post helpful.
Old 01-18-2017, 05:08 PM   #73
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by astrogeek View Post
My point was only to be complete, I then said clearly that we were now "Looking only at the logical construct". That is, we are ignoring the return value aspect for now.



No it is not a ternary operator, it is still two binary operators, && and ||, with different evaluation precedence and result.

You are interested in logic, so you surely understand truth table notation. So lets look at the truth table of each of these expressions.
But that is something extra that must be done by the code, it is not inherent in the logic.

Code:
 Originally Posted by rknichols View Post
No, you cannot use "else" without "if ... then", but you can accomplish it this way:
Code:

[[ some condition ]] && { code block 1; } || { code block 2; }

The catch (familiar to Python programmers) is that you have to ensure that code block 1 always ends
 with a "true" condition (zero return code). You can do that by making the last command in code block
 1 one that always succeeds, like "true" or ":".


Doing that should be an intellectual exercise only. 
In the real world, "if ... then ... else ... fi" would be preferable.
he is using code blocks not one argument each violating the rules. code raper ..


my logic is as such because the [ ] is always looking for a true if it is not a true then it will return a false. in that example



java

Quote:
The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.
Syntax

condition ? expr1 : expr2

Parameters

condition
An expression that evaluates to true or false.

expr1, expr2
Expressions with values of any type.
Code:
Multiple ternary evaluations are also possible (note: the conditional operator is right associative):

var firstCheck = false,
    secondCheck = false,
    access = firstCheck ? "Access denied" : secondCheck ? "Access denied" : "Access granted";
  
console.log( access ); // logs "Access granted"
true = zero
not true = anything but the zero

[ ] returns one or the other

&& only works with true statement

|| only works with not true statements.

USING CODE BLOCKS that is a violation of the conditional (ternary) operator

you are still wrong.

Last edited by BW-userx; 01-18-2017 at 05:33 PM.
 
Old 01-18-2017, 05:32 PM   #74
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,671

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
In bash (and standard shells) there are commands between if and then, while other languages only allow expressions.
In bash the logic works with the command's exit status, therefore a true is (exit)0, for practical reasons. In other languages a true is 1.
Documentation for other languages do not fully fit for bash.
--
The following illustrates the difference
Code:
if [[ ! -d "$move_to" ]]; then mkdir -v "$move_to"; else echo "$move_to is present already"; fi
Code:
[[ ! -d "$move_to" ]] && mkdir -v "$move_to" || echo "$move_to is present already or it's not present and mkdir has failed"
 
Old 01-18-2017, 05:43 PM   #75
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
this whole thing is called an operator
Quote:
condition ? expr1 : expr2
the word ternary is used because it consists of three arguments or how Java dude defines it one condistion and 2 expressions put together the total is 3. he further goes to calling it The conditional (ternary) operator placing the word that helps define it in ( )

He then shows how one can combine them to get something out of it too. therefore it is no longer a ternary operator, it is now a conditional operator because certain conditions have to be met for one of the expressions to actually do something.

do you even understand the example he gave?

Quote:
Multiple ternary evaluations are also possible (note: the conditional operator is right associative):

var firstCheck = false,
secondCheck = false,
access = firstCheck ? "Access denied" : secondCheck ? "Access denied" : "Access granted";

console.log( access ); // logs "Access granted"
or did everyone crap out of the truth?

Last edited by BW-userx; 01-18-2017 at 05:45 PM.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with scripting, aliases, and test statements. ThatCyberGuy Linux - Newbie 4 11-14-2015 02:58 PM
LXer: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting LXer Syndicated Linux News 0 06-29-2014 07:35 PM
LXer: How to Make a Fancy and Useful Bash Prompt in Linux LXer Syndicated Linux News 0 05-09-2014 07:51 PM
LXer: Bash One-Liner Script To Produce Somewhat-Fancy Output Of Who's On Your Linux O LXer Syndicated Linux News 0 11-19-2008 01:40 AM
urpmi looks for fancy dependency (bash) jfi Mandriva 6 03-05-2004 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration