LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Test and Variable assignment (https://www.linuxquestions.org/questions/linux-newbie-8/test-and-variable-assignment-4175602315/)

geminishaa 03-22-2017 05:10 AM

Test and Variable assignment
 
I am having below requirement

if $test is internal ; SERVTYPE is assigned as a
if $test is external ; SERVTYPE is assigned as b

[[ $test == "internal" ]] && SERVTYPE="a" || [[ $test == "external" ]] && SERVTYPE="b" || { echo ${USAGE}; };echo $SERVTYPE

but when i run this with test=internal; it gives SERVTYPE as b. I understand i have to put in brackets but when I put it like below:

( [[ $test == "internal" ]] && SERVTYPE="a" ) || ( [[ $test == "external" ]] && SERVTYPE="b" ) || { echo ${USAGE}; };echo $SERVTYPE
the variable SERVTYPE is not assigned.

I can write this thing in if else statement but I want to do it this way. Please suggest

grail 03-22-2017 06:07 AM

Do not confuse &&/|| with a if/then/else construct, they are most definitely not the same. You can make them behave in similar ways but it is better to just use the correct tool.

Your second example has the problem that by placing it inside () has put all the assignments into a subshell, which once returned from, your variables will only have the values that were
assigned prior to the subshell being entered.

Long story short, use the 'if' construct as that was designed for this task.

rtmistler 03-22-2017 06:44 AM

Hi geminishaa and welcome to LQ! :)

So very many times I see people try to write complex stuff all in one line.

As grail points out, the syntax can be complicated.

It does not need to be. You can write tests and nest your if tests within each other and have more easy to maintain, as well as readable code.

Further, those complex test lines do not match the requirement you cited.

You cited this:
Quote:

Originally Posted by geminishaa (Post 5686694)
I am having below requirement

if $test is internal ; SERVTYPE is assigned as a
if $test is external ; SERVTYPE is assigned as b

And I do not see how those lines correlate to satisfy these very simple requirements.
Quote:

Originally Posted by geminishaa (Post 5686694)
I can write this thing in if else statement but I want to do it this way.

I suggest that you first post how you've written it as an if-else statement to clarify exactly what you're trying to accomplish by optimizing the way the code is written.

I will end by reminding that one-line and concise is not always better.

Please use [code][/code] tags to enclose code and output.

Please post any errors or output you're receiving with your attempts.

If you're running this within a script, add the line "set -xv" just prior to the point where you perform this test to get additional debug output.

If you're running this from the command line, you can also type "set -xv" into the command line, and then later type "set +xv" to disable the verbose output. You can also add a "set +xv" line within a script to do a similar reversing of the extra debug mode.


All times are GMT -5. The time now is 06:14 PM.