LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (https://www.linuxquestions.org/questions/general-10/)
-   -   Evaluating variables in bash - vent (https://www.linuxquestions.org/questions/general-10/evaluating-variables-in-bash-vent-4175653557/)

AnneRanch 05-08-2019 12:43 PM

Evaluating variables in bash - vent
 
After couple of years scripting (in bash) I have to ask.
Finally got used to looking at bash "variables" as characters / strings.
About same time I realized that Linux is Unix based and recalled having hard time accepting NO response when Unix command completed successfully.

Yes, I am that old to remember that TTY was pretty slow and NO or minimal response was just memory / time saver.

So - taking BOTH into account - if CORRECT response is NUMERICAL zero, why do some "coders" use "set=yes" to complicate such simple task as evaluation ?

I am not sure where I encountered a discussion about "test" evaluating for "empty string" as opposed for "undefined string". Maybe that is the reason for replacing 0/1 test with "yes/no".
Just venting.
Cheers

pan64 05-08-2019 01:04 PM

would be nice to show us some examples.
bash has variables, but these variables have no type (so they can be integers and strings too). bash has no real arithmetic (just some minimal functionality implemented), so it is not really useful.

astrogeek 05-08-2019 01:32 PM

Quote:

Originally Posted by AnneRanch (Post 5992987)
After couple of years scripting (in bash) I have to ask.
Finally got used to looking at bash "variables" as characters / strings.
About same time I realized that Linux is Unix based and recalled having hard time accepting NO response when Unix command completed successfully.

Yes, I am that old to remember that TTY was pretty slow and NO or minimal response was just memory / time saver.

So - taking BOTH into account - if CORRECT response is NUMERICAL zero, why do some "coders" use "set=yes" to complicate such simple task as evaluation ?

I am not sure where I encountered a discussion about "test" evaluating for "empty string" as opposed for "undefined string". Maybe that is the reason for replacing 0/1 test with "yes/no".
Just venting.
Cheers

That is very vague.

Without an example it is not possible for anyone here to guess why other coders may or may not do some thing, or guess the nature of a discussion you cannot yourself remember.

Please review the Site FAQ for guidance in asking well formed questions.

If you wish to amend your post to meet that general standard then please do so. Otherwise the thread may be moved to the General forum which is more appropriate for vents and rants.

MensaWater 05-08-2019 03:36 PM

Quote:

After couple of years scripting (in bash) I have to ask.
There are multiple languages available if you don't like bash. Perl, Ruby, Python, PHP and of course you can write your own C code and compile it.

Quote:

About same time I realized that Linux is Unix based
Technically incorrect. Linux is a clone of UNIX written from scratch. It is not derived from UNIX source. Being an old UNIX admin myself I can tell you in general even though many commands have the same name the GNU/Linux/FOSS versions invariably have more functionality.

Quote:

recalled having hard time accepting NO response when Unix command completed successfully.
Every command gives return codes even if they don't output anything visible. If you want confirmation the command worked checked to see if its return code is 0. Non-zero return codes are defined by the applications so don't always indicate "error" but most do.


Quote:

Yes, I am that old to remember that TTY was pretty slow and NO or minimal response was just memory / time saver.
I'm fairly old as well. Linux (like UNIX) allows you to background things so TTYs aren't required. As others have noted you'd have to give examples for anyone to know what your talking about.

Quote:

why do some "coders" use "set=yes" to complicate such simple task as evaluation
For the same reason some people drive smart cars and others drive Hummers. Personal preference.
FOSS by its very nature encourages people to do things the way they want and if someone finds they don't like another's way they are free to modify the source code to do it the way they want.

Mailing lists and discussion boards are full of opinions and sometimes the discussions are quite heated. Feel free to vent away but don't be surprised if people counter-vent.

ntubski 05-08-2019 05:21 PM

Quote:

Originally Posted by AnneRanch (Post 5992987)
So - taking BOTH into account - if CORRECT response is NUMERICAL zero, why do some "coders" use "set=yes" to complicate such simple task as evaluation ?

(NOTE: The following is speculation, I don't know the actual historical reasons.) Using "0" to mean yes is confusing, because the natural interpretation is 0=no, 1=yes. Furthermore, you can't save a bash exit status inside a variable, it's not a first class value. You can save the string "0", then you have compare with test $foo = 0 to reinstate the 0 exit status, so you can see that this doesn't actually save any complication. May as well use test $foo = yes and avoid the possible confusion over 0/1.

astrogeek 05-08-2019 06:06 PM

Moved: This thread is more suitable in the General forum and has been moved accordingly.

rtmistler 05-08-2019 06:44 PM

Answer #1: All of this is very easily settled by writing a test script, echoing out some of these variables, and turning on shell debug.

Pretty much many members of LQ are experienced and similarly have used Unix.

The problem here is your vent is misplaced if this is all that you have to say.

Firm Answer: I'd be glad to inspect a small bash script and offer my thoughts on why tests obtain a certain result.

Blocking Point: For now, you have shown zero code in support of your rant and instead told us all about how you have experience. Great! But those of well rounded experience also know that they don't know everything, myself included, and thus when we ask a question, we do so, but also support it in some technical manner. If our way of thinking is absolutely skewed, one or more response givers will point out the flaws in our logic and, for myself, I'd be glad to understand where my thinking was incorrect.

That is the old, "You learn something everyday", mantra.

I can say for certain that there are inadvisable coding practices that I see in source code as well as in scripts. One of them is a true/false assumption for command results. An example is grep. It returns zero for success, 1 for nothing found, and 2 if there was an actual error in the command. The distinctions are important, and by reviewing the manual page for grep, one can see this for their self.

ondoho 05-09-2019 12:44 AM

Quote:

There are multiple languages available if you don't like bash.
there are also multiple shells available if you don't like bash.
but i doubt any one of them would use 1 for true and 0 for false...


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