LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2017, 05:10 AM   #1
geminishaa
LQ Newbie
 
Registered: Mar 2017
Posts: 1

Rep: Reputation: Disabled
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
 
Old 03-22-2017, 06:07 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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.
 
Old 03-22-2017, 06:44 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,992
Blog Entries: 13

Rep: Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443
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 View Post
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 View Post
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.

Last edited by rtmistler; 03-22-2017 at 06:47 AM.
 
  


Reply


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
about immediate and deferred variable assignment in GNU make George2 Programming 1 07-22-2011 05:55 PM
Question on variable naming and assignment... ${manintheyellowhat} Linux - Newbie 6 07-26-2010 07:41 PM
Variable assignment in Lisp. zaxonus Programming 2 03-18-2010 06:10 AM
[SOLVED] [BASH] variable assignment help RaptorX Programming 2 08-30-2009 10:28 AM
Java String variable re-assignment Cyhaxor Programming 9 11-24-2007 03:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:19 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration