LinuxQuestions.org
Review your favorite Linux distribution.
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, 05:53 PM   #76
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 MadeInGermany View Post
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.
for one what I have been saying the true = 0 or no error false is any thing other then zero


take your first code example


The following illustrates the difference
Code:
if [[ ! -d "$move_to" ]]; then mkdir -v "$move_to"; else echo "$move_to is present already"; fi
it is checking to see if that directory is not there if it is in fact not there then what does that equate to? a true
therefore it will create a directory.

if it is there then it echos it is already here.

now you use the [ ] && ||

the same thing I bet you a beer that && will execute if that directory is in fact not there just like the first example. because it equates to a true


Code:
[[ ! -d "$move_to" ]] && mkdir -v "$move_to" || echo "$move_to is present already or it's not present and mkdir has failed"

further more you are wrong in that 1 C++ main returns a 0 (zero) for non error or a true it did not error is zero in C++ ... sooo that is true = 0 like bash

Code:
// 'Hello World!' program 
 
#include <iostream>
 
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Last edited by BW-userx; 01-18-2017 at 05:59 PM.
 
Old 01-18-2017, 06:27 PM   #77
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 MadeInGermany View Post
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.
that is what you are talking about with the 0 or 1 but that is not the argument here.

Quote:
i.e. anything that is not zero and can be converted to a boolean is not false , thus it must be true . true evaluates to 1 , but any int that is not false (i.e. 0 ) evaluates to true but is not equal to true since it isn't equal to 1 .Mar 4, 2011
boolean - c++ bool question - Stack Overflow
 
Old 01-18-2017, 06:49 PM   #78
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
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.
if of all when one changes the equation then of course you are going to get different results because now the original has been tainted. that is why what I have been getting shown to prove me wrong is your argument.



the Conditional (ternary) Operator

it only takes three arguments or three commands or three of whatever you want to call it. anything other then that it will error out and not do what it is expected of it.



JAVA in their example in how to use the the Conditional (ternary) ternary Operator , the ternary now in brackets because the rules have not changed only in the Java example of how it works true or false boolean ternary Operator.

if BASH set itself up that
true = 0
and
false is anything other then zero

then one that codes in BASH has to take that into account.

that is Boolean is bash.

it is because the [ ] returns both that the && and || now can be used in conjunction with the [ test ]

&& operates only off true which is zero
anything that is on the left side of && has to be a zero value or whatever is on the right side of && will not be executed.


the same but opposite is true of ||

anything that is on the left side of && has to be a non zero value or whatever is on the right side of || will not be executed.


in a straight line read.

[ true or false ] && true gets executed || false gets executed

if a false is returned then it skips over the right side of && until it sees the || then evauates what is on the right side of that to see if it is a match, a non zero or false.

if that is true then it will execute whatever is on the right side of ||

everything always equates to a true in the end. Just because it is a false = non zero it is still equates to a true if it matches.

It is one argument per place holder not a block code { argument; argument;} that equates to two even though it is represented as one because of the { } brackets.


the boolean rule or is it true false rule?
Quote:
i.e. anything that is not zero and can be converted to a boolean is not false ,
thus it must be true . true evaluates to 1 , but any int that is not false
(i.e. 0 ) evaluates to true
but is not equal to true since it isn't equal to 1 .Mar 4, 2011
boolean - c++ bool question - Stack Overflow


so BASH did what? made 0 zero a true and anything not zero false because they had to deal with it one way or another.


Code:
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

I am using the basic proper rules of engagement. whereas no one else is.

Quote:
In languages like C++, Java, Python, and the like, there's the concept of a ternary operator.
Basically it allows you to assign one value if a condition is true, else another.
Well, there's no ternary operator in Bash, but there is a way to fake it.
Where whatever conditional you want is within the brackets.
[ ] && ||

now is a "fake" ternary operator that produces the same results when the rules are applied just like ?: are.
condition ? expr1 : expr2
[condition] && expr1 || expr2

to take that and make it just like it is done in Java can be done,

java
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"

BASH
Code:
    firstCheck = false,
    secondCheck = false,
    access = firstCheck && "Access denied" || secondCheck && "Access denied" || "Access granted";
  
console.log( access ); // logs "Access granted"
that works I tested it before posting this.

that is not written to work but the logic works. that is what i tested.

Last edited by BW-userx; 01-18-2017 at 06:59 PM.
 
Old 01-18-2017, 07:42 PM   #79
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
NoobMEsees in `man bash`, ([fwiw:0])
?: down in the Numeric operator section only;
&& || early in Command/Control Grammar (yes, also NumOp).
Later section is ?math?; earlier is ['program'] ?flow?.
(bash.c executes; humans reason) Idk. Over&OUT.
 
Old 01-18-2017, 09:30 PM   #80
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
So I am curious, what exactly do all the people except you have to gain by, as you put it, "lying", about how this works? Do you honestly believe that all the people who have attempted to help you are
now somehow working together to convince you of a lie??

I have also noticed that you are selective about which examples you rally against while completely ignoring others. I wonder is this because you cannot use your logic to disprove what has been stated?

So I have devised a series of yes / no questions which I would like you to answer:

1. In all excluding bash arithmetic, your ternary examples utilise the ?: operator?

2. In all other languages, from your examples, where the ternary operator is used, do they ever alter their behaviour? (ie. is there a scenario where the ternary operator behaves not as expected)

3. Do you agree that if a set of operations can change the expected solution, then it cannot be considered a ternary operator?
 
2 members found this post helpful.
Old 01-19-2017, 12:30 AM   #81
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,232

Rep: Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451
[ ] && || is not a fake ternary operator but two different operators, evaluated first && and finally ||.
Code:
    firstCheck = false,
    secondCheck = false,
    access = firstCheck && "Access denied" || secondCheck && "Access denied" || "Access granted";

is practically invalid in bash, because "Access denied" will be executed (a command is expected, not a variable), and the result is false, because no such command.
access will be equal to "firstCheck" (will be taken as a string)
I'm really interested how could you test it, and how do you know it is working.
 
2 members found this post helpful.
Old 01-19-2017, 05:16 PM   #82
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 grail View Post
So I am curious, what exactly do all the people except you have to gain by, as you put it, "lying", about how this works? Do you honestly believe that all the people who have attempted to help you are
now somehow working together to convince you of a lie??
you all keep violating the definition of ternary operator by presenting me with
Semantic errors: errors due to an improper use of program statements. to tell me that this is not a ternary operator [ ] && ||
by using examples like this
Code:
[ test ] && { command1; command2; } || command3
that is four or a quad operator not a ternary operator

Quote:

I have also noticed that you are selective about which examples you rally against while completely ignoring others. I wonder is this because you cannot use your logic to disprove what has been stated?

So I have devised a series of yes / no questions which I would like you to answer:

1. In all excluding bash arithmetic, your ternary examples utilize the ?: operator?
the ternary operator by definition does not have to use arithmetic in order to utilize it.
Quote:

2. In all other languages, from your examples, where the ternary operator is used, do they ever alter their behavior? (ie. is there a scenario where the ternary operator behaves not as expected)

3. Do you agree that if a set of operations can change the expected solution, then it cannot be considered a ternary operator?
I have no idea I have not tested them, but

all operators can only operate off a truth be that truth can be a yes or a no
the binary operator only acts on a true or yes
whereas
the ternary operator acts on both the yes and no truths that are returned by another binary operator or ternary operator
because
Quote:
Conditional logic defined by google results.
A conditional statement, symbolized by p q, is an if-then statement in which p is a hypothesis and q is a conclusion. The logical connector in a conditional statement is denoted by the symbol . The conditional is defined to be true unless a true hypothesis leads to a false conclusion.

Hypothesis: a supposition or proposed explanation made on the basis of limited evidence as a starting point for further investigation.

what I say to that is:
A hypothesis cannot be true or false within itself. One cannot have a true hypothesis or a false hypothesis because by its definition it is a proposition made as a basis for reasoning, without any assumption of its truth. It is made so that it can lead to the ascertaining of a truth. If it is carried out to the end then a truth will be ascertained. It will be either a true or a false statement and no longer an assumption. It is then that the truth is ascertained.

Therefor Conditional Statements - Math Goodies are in error in their definition of what conditional logic is. So I cannot not use it fully because it is only a half truth. Now I have to fix that so it will no have an error within it. It needs to be a true statement else errors will arise and a complete truth will never be ascertained.

It should read:
A conditional statement, symbolized by p q, is an if-then statement in which p is a hypothesis and q is a conclusion. The logical connector in a conditional statement is denoted by the symbol . The conditional is defined to be true or false when the conclusion (truth) of the hypothesis is ascertained.
The action depends on the condition of the hypothesis. The condition is determined by the conclusion of the hypothesis. Which is either a true or a false. It is only at the conclusion of the hypothesis that an action can take place. If an action takes place prior to the conclusion of the hypothesis then an error can take place if the action contradicts the conclusion of the hypothesis.

Because the hypothesis will always ascertain a truth in its conclusion. An action can then be performed on that conclusion that will not contradict the conclusion of the hypothesis. Because it will no longer be a hypothesis. But a truth that the action is now being operated on.

This is the reason that the binary if then statement can only act on a return value of true (yes).

If (true) then do something.
An operation will only be preformed when a true exist. Because the conclusion always reviles two truths. Only one can be acted on in a binary operation.


To further define a truth. When a truth is false that is actually a no then it is still considered a true statement. Therefor it can be acted upon.
In order to determine if it is a true statement it has to do a comparison with the results from the hypothesis and the action waiting to be preformed. If they match then another truth is had and then the action can take place.
It will not preform an action without a truth. If it does then an error is taking place within the system due to an action that has been preformed without a truth.
The binary operator can only act on one conclusion of the condition (hypothesis) only when it returns true, yes. A trinity (ternary) operator can act on two conclusions of the condition (hypothesis). Regardless if it returns either a true, yes, or a false, no. Because the options being given by the trinity (ternary) operator are then put to the test by another binary operation. One for each conclusion (condition).


Even though Universal Binary is 1=ON or YES. 0=OFF or NO
This is how these two numbers are used in programming.
if (program_executed_fine)
return 0;

else if (program_had_error)
return 1;
If the hypothesis of the if then statement returns a value of 1 is it in error? NO because it is binary. Therefor it is only a no that is being returned not an error. Therefor a trinity operator too works on a truth and not an error no matter if the value return is a 0 (zero) or a 1 (one) because it is a yes or a no.

that is only part of it....

Last edited by BW-userx; 01-19-2017 at 05:28 PM.
 
Old 01-19-2017, 05:41 PM   #83
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
[ ] && || is not a fake ternary operator but two different operators, evaluated first && and finally ||.
Code:
    firstCheck = false,
    secondCheck = false,
    access = firstCheck && "Access denied" || secondCheck && "Access denied" || "Access granted";

is practically invalid in bash, because "Access denied" will be executed (a command is expected, not a variable), and the result is false, because no such command.
access will be equal to "firstCheck" (will be taken as a string)
I'm really interested how could you test it, and how do you know it is working.
if you are working with strings then do you check for integer? NO unless you want a false reading or error to occur.

where is your logic and reasoning?

if we are working with strings then we check with strings.


access is made to equals true so we will get a false returned. because of this
Code:
firstCheck = false,
secondCheck = false,
Code:
the && acts on the truth or a yes 0
the || acts on the truth or a  no or a non-zero
the [ ] returns a truth of yes or no or a zero or non-zero
Code:
access= false
check=false
then this returns a true because they match. this is string logic we want a false for this
example so what needs to be done?

where the Java example set the conclusion of a condition as false we too have
to do the same to get the same results.

access=true
check=false

therefore we now get a return value of non-zero to issue a false 
when the comparisons takes place.

for this example to be exactly the same as the JAVA example because 
that is the only way to see the truth that Java shows about this type of operation.
the logic works like I stated.

my quote
Quote:
that works I tested it before posting this.

that is not written to work but the logic works. that is what i tested.
run this and see ... it is now written to work
Code:
#!/bin/bash

 

firstCheck=false
secondCheck=false
access=true

  [ "$firstCheck" =  "$access" ] && echo "Access denied1" || [ "$secondCheck" = "$access" ]  && echo "Access denied2" || echo "Access granted3";

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

Rep: Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599
This thread has strayed very far from the original question, "BASH fancy test vs if statements how to?".

The discussion of ternary operators is relevant, but only insofar as it is a model to be emulated by bash conditionals. That material has been adequately covered for most to understand the nuances. The many ways ternary operators can be used in java or other languages is irrelevant.

To the OP, please re-read this thread with effort to understand the excellent help that has been offered, as opposed to endlessly and selectively arguing against it without directly addressing those replies. Posting lengthy copy paste snippets from other sources only obliquely related is not helpful to you or anyone else. Please don't do that.

Remember that long into the future others will land on this thread from search links, seeking helpful information related to their own problems - make it useful to them!

Take this as a gentle warning and please, everyone let's get this thread back on topic.

Last edited by astrogeek; 01-19-2017 at 06:05 PM. Reason: typo
 
Old 01-19-2017, 06:32 PM   #85
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
This thread has strayed very far from the original question, "BASH fancy test vs if statements how to?".

The discussion of ternary operators is relevant, but only insofar as it is a model to be emulated by bash conditionals. That material has been adequately covered for most to understand the nuances. The many ways ternary operators can be used in java or other languages is irrelevant.

To the OP, please re-read this thread with effort to understand the excellent help that has been offered, as opposed to endlessly and selectively arguing against it without directly addressing those replies. Posting lengthy copy paste snippets from other sources only obliquely related is not helpful to you or anyone else. Please don't do that.

Remember that long into the future others will land on this thread from search links, seeking helpful information related to their own problems - make it useful to them!

Take this as a gentle warning and please, everyone let's get this thread back on topic.
I gave out my points for it then I was challenged and called a liar. I an defending a truth in this and a Java example is valid to prove my point.

as I have by using it.

they are now in error of the truth in this and continue to use errors to try and prove me a lie.

I am only responding to it in my defense in hope that they actually learn what I am trying to show them is a truth. So they can take this knowledge with then an hopefully not only be better programmers but a better person as a result of it if then apply this logic to there everyday life as well..

because the if then statement only acts on a truth

therefore they should to. else errors will arise.

look at post #10 that is when I marked it solved points given then was attacked afterwords.

post #26 is when a lie enters this discussion. I counter it with a truth and have continued to do so and I am the one getting the warning? for speaking the truth.

if true then

else
disregard

too if you actually look at this it is still on topic because the topic is

Quote:
"BASH fancy test vs if statements how to?".
Code:
 [ ] && ||
qualifies as a fancy test but the others HAVE no real understanding of how it really works to they are using lies (errors with there examples = lie) to try and discount its value?

they do not even seem to know how this thing called logic actually works to apply it across the board, so now I am trying to help them by the use of the topic showing them how a fancy conditional (ternary) operator can actually work in BASH too.

Last edited by BW-userx; 01-19-2017 at 07:09 PM.
 
Old 01-19-2017, 11:45 PM   #86
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,583
Blog Entries: 11

Rep: Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599
No one has called you a liar. No one has attacked you in any way or even been discourteous to you in this thread.

Personal attacks are not tolerated on LQ. If you think that someone has attacked you or insulted you the proper response is to report the offending post for moderation.

Thread closed.
 
1 members found this post helpful.
  


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 09:16 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