ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
looks like you do not understand what we are trying to explain (probably you missed my previous post?)
[ is equal to the test command, you can also check there is a /usr/bin/[ and also it has a man page. There you can read what can be tested using [ ]. But [ is a bash builtin too, with more or less similar features.
szboardstretcher did not lie.
as far as I see you have problems with operator precedence and that will really hard to understand what will really happen. For example: (( y = x == 1 ? 2 : 3 ))here you can insert () to see what's happening:
Code:
(( y = (x == 1 ? 2 : 3) ))
# so first x == 1 will be evaluated, that is true, next the ternary operator will return 2 and finally y will be set to 2.
The other one:
Code:
(( y = x = 1 [ is x > 1 ] ? yes 2 : no 3 ))
# is much more difficult, but you can also try:
first x=1 and y=1
next x>1 comparison,
next ternary operation
and there is no assignment here
[QUOTE=pan64;5656883]looks like you do not understand what we are trying to explain (probably you missed my previous post?)
[ is equal to the test command, you can also check there is a /usr/bin/[ and also it has a man page. There you can read what can be tested using [ ]. But [ is a bash builtin too, with more or less similar features.
szboardstretcher did not lie.
as far as I see you have problems with operator precedence and that will really hard to understand what will really happen. For example: (( y = x == 1 ? 2 : 3 ))here you can insert () to see what's happening:
Code:
(( y = (x == 1 ? 2 : 3) ))
# so first x == 1 will be evaluated, that is true, next the ternary operator will return 2 and finally y will be set to 2.
The other one:
first off that is pseudo code. to show what it is doing, it is not and was not written to execute because it is pseudo code. look up pseudo code ..
Quote:
Code:
(( y = x = 1 [ is x > 1 ] ? yes 2 : no 3 ))
# is much more difficult, but you can also try:
first x=1 and y=1
next x>1 comparison,
next ternary operation
and there is no assignment here
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.
#!/bin/bash
e=r=t=y=1
[ e > 1 ] && [ r > 1 ] || echo "what?"
but why????
That line "e=r=t=y=1" in bash does not behave anything like what the corresponding C statement does. It sets one variable, "e", to the string "r=t=y=1", and does nothing else. Furthermore, when you write "[ e > 1 ]", that is not using the variable "e" at all. It is testing whether the string "e" is greater than the string "1", which is true in the ASCII collating sequence. Likewise for "[ r > 1 ]". If you want to test the variables, you write it as
Code:
[ $e > 1 ] && [ $r > 1 ] || echo "what?"
That is going to report a syntax error since the variable "r" has never been set.
This thread is degenerating into a battle of monkeys vs. typewriters.
touch /tmp/myfile
chattr +i /tmp/myfile
[ -e /tmp/myfile ] && echo "text" > /tmp/myfile || echo "file does not exist"
-bash: /tmp/myfile: Permission denied
file does not exist
Supposed plain english:
If file exists, then echo text into it, else echo file does not exist.
Results:
You will notice that it echos 'file does not exists' even though the file clearly exists, because the second command failed - not the first. This is not IF/THEN/ELSE behavior.
Code:
if [ -e /tmp/myfile ]; then
echo "text" > /tmp/myfile
else
echo "file does not exist"
fi
-bash: /tmp/myfile: Permission denied
Plain english:
If file exists, then echo text into it, else echo file does not exist.
Results:
The file existed, so it attempted to echo text into it. Since the file existed, it did not echo 'file does not exist'. This is typical IF/THEN/ELSE behavior.
lets see what you did
1. touch /tmp/myfile
created a file on system side
2. chattr +i /tmp/myfile
changed it so that it cannot be modified (immutable). Means no renaming, no symbolic link creation, no execution, no writable, only superuser can unset the attribute.
3. QUESTION:
is this command line or bash and ran as what, user or root?
Code:
[ -e /tmp/myfile ] && echo "text" > /tmp/myfile || echo "file does not exist"
ran it both ways off command line and got the same results. BUT WHY? what was changed to get the || to act and not the && to act?
chattr +i /tmp/myfile does what so it gets what in return as a result of it??
again you are doing what to prove you're a truth and I am a lie?
without the change it so it will not work no matter what by not adding the chattr +i
it works
why is that? because you still do not understand how does it really work and what you assumed is not the implemented behavior. Noone lies here.
chattr will lock the file and noone (even root) is allowed to modify that. That's why the echo text > /tmp/myfile failed.
what was changed to get the || to act? I think it is explained several times, but see post #41, probably missed.
what was changed to get the || to act and not the && to act?
You have missed the point here ... the && did work because the prior test was successful and [ will return a zero, hence what is after the && was executed, but, because the next statement resulted in an error
the || was enacted as there was an error prior to it so what is on the right of it is now launched.
So as you can see, because this version allows for both the zero and non-zero parts to be executed, it is not the same as if/then/else ... YES?
That line "e=r=t=y=1" in bash does not behave anything like what the corresponding C statement does. It sets one variable, "e", to the string "r=t=y=1", and does nothing else. Furthermore, when you write "[ e > 1 ]", that is not using the variable "e" at all. It is testing whether the string "e" is greater than the string "1", which is true in the ASCII collating sequence. Likewise for "[ r > 1 ]". If you want to test the variables, you write it as
Code:
[ $e > 1 ] && [ $r > 1 ] || echo "what?"
That is going to report a syntax error since the variable "r" has never been set.
This thread is degenerating into a battle of monkeys vs. typewriters.
let me test that now to be sure of something other then what you said as well ,, it is looking at strings and not Integer.
as I see what you are talking about, I forgot to add the $ sign. to get the value inside of it.
in my hast with the ( y=x==1 ? 2 : 3)) I just seen as Integers. but this what I did earlier
still proves what I am saying. if true then do this if not true do that
if then else
( ? : )
[ ] && ||
zero = true
non zero = false
now if one sees it as Integers this still will not and never work.
Code:
x=1
y=x=1
(( y=x == 1 ? 2 : 3 ))
echo $y
if Integers then
y equals one
x equals one
now test to see if they both equal one, yes it is a true so 2 is returned. now lets mess it up with something completely different that will always not be true.
Code:
x=1
(( y = x > 1 ? 2 : 3 ))
echo $y
is 1 greater then 1 .. NO so it will never equate to a true. so it will never execute in any kind of if statement without an else
Code:
if [[ 1 > 1 ]] ; then
echo "I am greater then myself!"
fi
that will never execute. It will forever be passed over. Unless you write it like this
Code:
if [[ 1 > 1 ]] ; then
echo "I am greater then myself!"
else
echo "There is no way in Heaven or hell or on earth
itself that I can ever be greater then myself"
fi
it still is the same as writing it like this ( ?: )
Code:
(1 > 1?yes:no)
because it is a ternary operator it has to have 3 (three) arguments which are
1 > 1
yes
no
if true do this if not true do that effect.
the way they are trying to present their joint argument they are using deceitfulness to try to prove me wrong.
let them live it themselves and see where that gets them in the end.
I got a movie to watch or something more productive to do.
still proves what I am saying. if true then do this if not true do that
No, it does not prove. This is one case, when it looks like they are similar, and there is another case where you can clearly see they work differently.
You know, a frog and a horse are identical, because both have 4 legs.
No, it does not prove. This is one case, when it looks like they are similar, and there is another case where you can clearly see they work differently.
You know, a frog and a horse are identical, because both have 4 legs.
state you cases to prove your statement.
You know, a frog and a horse are identical, because both have 4 legs.
ok, so state your case to prove your statement. Why do you think if/then/else are identical to && || ? Just because there is a case when they look like they are identical? stupid argument.
Apart from the commented code, the data inside (()) says:
y = :- this simply says that whatever the right side evaluates to set the value for y
x == 1 :- this is the test of whether or not x is equal to the number 1
? 2 :- should the expression evaluate to true, return the number 2
: 3 :- should the expression evaluate to false, return the number 3
As indicated by the echo, x has a value of 1 which will evaluate the expression to true so y will be set to the value of 2
This is indeed a true ternary operator and as discussed it ONLY works for integer numbers. So this operator is behaves exactly like the if/then/else construct.
Now let us look at your other example, first, exactly as you have written it:
Now we seem to have not the same output. here this process follow neither that of the ternary operator or of the if/then/else construct.
Therefore, it is ONLY a ternary operator under exact circumstances, hence it is LIKE a ternary operation, but it is in fact not because you can only get that operation to work with set input.
ok, so state your case to prove your statement. Why do you think if/then/else are identical to && || ? Just because there is a case when they look like they are identical? stupid argument.
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.
IN C when I was playing with C++
replacing all of my if then else statements with ?: in college. no professor said what I am hearing in here.
now I am being told in here that ?: is not if then else
it is short hand and it is the same
[ ] && || is BASH replacement for ?: because no one ever wrote anything in BASH to interpret the
( ? : )
so it uses
[ ] && ||
instead.
but wait someone in here showed me this
Code:
x=1
(( x > 1 ? yes : no ))
return $x
and told me that I was lying that it is not the same as if then else
when in fact it is!
it always test for true regardless then the next argument is fired if not true the third argument is fired.
Code:
if [ true ] ;
then
yes
else
no
fi
Code:
true ? yes : no
Code:
[ true ] && yes || no
it is that simple
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.
[ ] && || is BASH replacement for ?: because no one ever wrote anything in BASH to interpret the ( ? : )
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....
Apart from the commented code, the data inside (()) says:
y = :- this simply says that whatever the right side evaluates to set the value for y
x == 1 :- this is the test of whether or not x is equal to the number 1
? 2 :- should the expression evaluate to true, return the number 2
: 3 :- should the expression evaluate to false, return the number 3
As indicated by the echo, x has a value of 1 which will evaluate the expression to true so y will be set to the value of 2
This is indeed a true ternary operator and as discussed it ONLY works for integer numbers. So this operator is behaves exactly like the if/then/else construct.
getting rid of all of that other crap above because that was their examples lies or bad examples well I say lies.
Quote:
Now let us look at your other example, first, exactly as you have written it:
Now we seem to have not the same output. here this process follow neither that of the ternary operator or of the if/then/else construct.
Therefore, it is ONLY a ternary operator under exact circumstances, hence it is LIKE a ternary operation, but it is in fact not because you can only get that operation to work with set input.
Again, I hope that is clearer.
HOPE I MADE myself Clear, when you change it so it does not work then how can you expect it to work?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.