LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 08-27-2012, 09:10 PM   #1
mackes
LQ Newbie
 
Registered: Aug 2012
Posts: 6

Rep: Reputation: Disabled
Is the true statement necessary in a simple script?


Is there a proper way to not have to say true?
If the first if is true, then no commands are needed and
continue on after the if-fi section with the rest of the script.

#!/bin/bash

var=1
if [ $var -eq 1 ]; then
true
elif [ $var -ne 1 ]; then
echo Exiting
exit
fi
echo Continuing


I tried this, and it does not work:
#!/bin/bash

var=1
if [ $var -eq 1 ]

elif [ $var -ne 1 ]; then
echo Exiting
exit
fi
echo Continuing
 
Old 08-27-2012, 09:17 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. please use code tags https://www.linuxquestions.org/quest...do=bbcode#code

2. just reverse the logic OR
in this case as its a binary check and you don't do anything anyway,
just drop the first if [ ]
Code:
#!/bin/bash

var=1
if [[ $var -ne 1 ]]
then
    echo Exiting
    exit
fi
echo Continuing
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

See the following for a comprehensive tutorial & references/examples
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
Old 08-27-2012, 10:11 PM   #3
mackes
LQ Newbie
 
Registered: Aug 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks. I probably should have used this for an example instead. Thanks for the links.
I will check them out.

Code:
#!/bin/bash

var1=2    # not known before script is run.
var2=1
if [ $var1 -gt $var2 ]; then
true
elif [ $var1 -le $var2 ]; then
echo Exiting
exit
fi
echo Continuing
 
Old 08-27-2012, 10:24 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Maybe I am missing something,. but it seems as if the true section doesnt need to be there.

Code:
#!/bin/bash

var1=2    # not known before script is run.
var2=1
if [ $var1 -le $var2 ]; then
echo Exiting
exit
fi

echo Continuing
 
1 members found this post helpful.
Old 08-27-2012, 10:43 PM   #5
mackes
LQ Newbie
 
Registered: Aug 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Maybe I am missing something,. but it seems as if the true section doesnt need to be there.
That is what the comment was for. I should have explained it better. I put in "2" for test purposes but it is really unknown and found before this part of the script.
Code:
var1=2    # not known before script is run.
Ok, I see what you are saying now. If it is not "that", then there is no other choice that it must be "this". I guess I am wanting a more absolute test even though it is overboard and unnecessary.

Thanks chrism01 and szboardstretcher, marking solved and will read some more tutorials.

Last edited by mackes; 08-27-2012 at 11:11 PM.
 
Old 08-27-2012, 11:37 PM   #6
mackes
LQ Newbie
 
Registered: Aug 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
One more question.

In my examples, and assuming there are multiple elifs, if the first "if" statement is true, the if-fi completes and does not need to go on to check the rest of the elif statements. Wouldn't this make the whole section more efficient?

And in this case it looks like the "true" statement would be necessary because the "if" must have a command.

Last edited by mackes; 08-27-2012 at 11:48 PM. Reason: if multiple elifs
 
Old 08-28-2012, 01:15 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
assuming there are multiple elifs, if the first "if" statement is true, the if-fi completes and does not need to go on to check the rest of the elif statements.
'true' ... that's basic logic

Like I said, you would just reverse or otherwise amend the logic to avoid doing pointless tests ie doing a test that caused it to 'do nothing'.
If you really want to do that test and then do 'nothing' it looks like
[code]
if [[ some_test_here ]]
then
:
fi
[code]
':' is the do nothing but effectively do a true (null) thing. You almost never see this technique used unless the succession of if .. elif .. elif .. else .. fi tests is sufficiently long/complex that it makes sense (easier to read) if it is there, in which case (sic) consider using a case statement instead ...

HTH

Have a good read of those docs
 
Old 08-28-2012, 10:21 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-28-2012, 12:31 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
chrism01's last post is on the money. elif is only needed when you have multiple separate independent conditions to test. If all you have is a boolean true/false condition, then you only need if..else; the else commands will always execute when the if part of the test returns false.

And when you have a single input, but multiple patterns to match, a case statement is usually cleaner and more efficient.

Code:
case $var in
	0) echo "variable is equal to zero" ;;
	1) echo "variable is equal to one" ;;
	2) echo "variable is equal to two" ;;
	*) echo "variable is not one, two, or three" ;;
esac
Finally, when using bash or ksh, it's recommended to use [[..]] for string/file tests, but ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

Code:
if (( var1 > var2 )); then
	echo "variable one is greater than variable two."
elif (( var1 == var2 )); then
	echo "variable one is equal to variable two."
else
	echo "variable one is less than variable two"
fi
http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr

Last edited by David the H.; 08-28-2012 at 12:33 PM.
 
  


Reply



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
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
LXer: Android Started Before iPhone: How True a Statement is That? LXer Syndicated Linux News 0 11-27-2011 07:51 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
if statement ignoring true/false and proceeding anyway. Goblin_C_Noob Programming 4 03-30-2008 09:56 AM
Why do I get true in this if statement in bash? nadavvin Programming 4 01-23-2007 12:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 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