LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 03-30-2018, 06:05 AM   #16
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053

Quote:
Originally Posted by Keith Hedger View Post
Been using this construct for years, just checked now with an unassignred var and you're right no error, guess the BASH dev finally fixed this! Wonder when?
UPDATE error thown when exp is not in quotes
Code:
keithhedger@LFSDev:~-> echo $var1
aaa
keithhedger@LFSDev:~-> echo $var2

keithhedger@LFSDev:~-> if [ $var1 = $var2 ];then echo "Success!"; fi
bash: [: aaa: unary operator expected
double brackets also help:
Code:
if [[ $var1 = $var2 ]];then echo "Success!"; fi
=> no error.
of course best coding practice demands to use both i guess.
 
Old 04-02-2018, 10:52 AM   #17
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Code:
BASE="8"
if [ ${BASE} -eq 8 ]
then echo "octal"
fi

LIGHT="green"
if [ ${LIGHT} -eq "green" ]
then echo "go"
fi
You may find your code easier to read if you indent and change structure a bit. The above is difficult at best for me to understand. Most scripting I've seen is done in the following way for bash.

Code:
BASE=8
if [[ "$BASE" == 8 ]] ; then
  echo "octal"
fi

LIGHT=green
if [[ "$LIGHT" == green ]] ; then
  echo "go"
fi
Indentation makes things a world easier to read imo. Also semicolons with your then and do parts easier to again... imo. This is how pretty much every script I've ever seen is done.
 
Old 04-02-2018, 11:43 AM   #18
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by jmgibson1981 View Post
Code:
BASE="8"
if [ ${BASE} -eq 8 ]
then echo "octal"
fi

LIGHT="green"
if [ ${LIGHT} -eq "green" ]
then echo "go"
fi
You may find your code easier to read if you indent and change structure a bit. The above is difficult at best for me to understand. Most scripting I've seen is done in the following way for bash.

Code:
BASE=8
if [[ "$BASE" == 8 ]] ; then
  echo "octal"
fi

LIGHT=green
if [[ "$LIGHT" == green ]] ; then
  echo "go"
fi
Indentation makes things a world easier to read imo. Also semicolons with your then and do parts easier to again... imo. This is how pretty much every script I've ever seen is done.
actually that first code was proper in comparisons which was changed. that might cause confusion as a result of it.


Code:
BASE=8
LIGHT=green

if [[ "$BASE" -eq '8' ]] ; 
then
  echo "octal"
fi


if [[ "$LIGHT" == "green" ]] ; 
then
  echo "go"
fi
 
  


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
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 06:33 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 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
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

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