LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-03-2018, 05:48 PM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
cannot get bash if command correct


Definite newbie problem

Code:
#!/bin/bash
...
CREATE_TARGET_LIST=0
...
if[ $CREATE_TARGET_LIST = 1 ]
then
and the response is
Quote:
./find.sh: line 65: if[ 0 = 1]: command not found
./find.sh line 66: syntax error near unexpected token 'then'
./find.sh line 66: 'then'
I have tried ==, -eq, removed all the white space and get the same response. It shows line 66 for the error and I have commented out all the code above this fragment except for the she-bang and the variable creation.

What is it that I just cannot see?

Last edited by bkelly; 07-03-2018 at 05:50 PM.
 
Old 07-03-2018, 06:00 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,151

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
use space after 'if' and before '[', -eq NOT '=' for integers, space before ']', dont forget 'then' and 'fi'
 
Old 07-03-2018, 06:08 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes [ is actually a command so you need a space after if

On another side, if it is for testing integer, you can use bash arithmetic built-in

Code:
if (( CREATE_TARGET_LIST == 1 )); then
  ...
This works too:
Code:
if(( CREATE_TARGET_LIST == 1 )); then
  ...
 
Old 07-03-2018, 06:09 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
what Keith said, for sure, but the biggest clue was
Code:
if[ 0 = 1]: command not found
which shows that the interpreter was seeing the if[ 0 = 1]as a single command.
The bash interpreter depends heavily on the proper location of white space.
In fact, I wouldn't expect that if statement to work if there really is no space between the 1 and the trailing ]
 
Old 07-03-2018, 06:12 PM   #5
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Keith got me working.
keefaz: So the brackets are commands rather than grouping operators. Ok, That will take some getting accustomed to.
On second thought: please explain the command that is embodied by: [ and ]. I presume the two characters taken together are the command with the entity between them being the argument.

Thank you for your replies.

Edit: interesting, and not in the good sense. I thought this was a good tutorial:

https://ryanstutorials.net/bash-scri...statements.php
But it does not mention the word spaces and does not describe what the posts here did. I need to find a better tutorial.

Last edited by bkelly; 07-03-2018 at 06:20 PM.
 
Old 07-03-2018, 06:16 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by bkelly View Post
On second thought: please explain the command that is embodied by: [ and ]. I presume the two characters taken together are the command with the entity between them being the argument.
I suggest to read the manual page for the test command, it should explain thing better then me

Code:
man test
 
Old 07-03-2018, 06:27 PM   #7
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
So I visited: man test with the result that I was and am unable to really understand what it is saying in the context of a bash if statement. I presume that man page makes sense to you guys, but from my novice perspective it does not help. So I will look for some more bash tutorials and bookmark one or two of them that discusses the bash if statement much better.

End result, I am much better off now than an hour ago. Thank you.
 
Old 07-03-2018, 06:34 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
This might be easier to understand

https://linuxacademy.com/blog/linux/...if-statements/
 
Old 07-03-2018, 06:46 PM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,151

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
grab a copy of the advanced bash scripting guide (abs) it may be in your distros repos, if not itz easy to find online, it covers all sorts from simple concepts upwards you will find it the best source of documentation for bash there is.
 
Old 07-03-2018, 06:47 PM   #10
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
The one I referenced was easy to understand. But it left out a bunch of stuff. The site you, michaelk, suggested looks pretty good. Bash is more complicated than at first appears. (isn't everything)

Edit: The example
Quote:
if [ -r somefile ]; then
The concept of putting the semicolon after the close bracket is a major contradiction with writing C code.

Last edited by bkelly; 07-03-2018 at 06:53 PM.
 
Old 07-03-2018, 08:02 PM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
In bash we use a ; in place of a new line. The ; causes the preceding command to be executed before proceeding to the next command
 
  


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 choose correct /dev/sdX for a bash script iFunction Linux - Newbie 17 02-26-2016 08:02 AM
[SOLVED] simple Bash script not returning correct value unassassinable Programming 6 09-15-2010 04:04 AM
usermod command,is it correct? cola Linux - Newbie 7 06-02-2010 05:17 AM
Bash Prompt - display correct $(pwd) tschima Linux - General 2 07-05-2007 07:03 PM
Correct Command, anyone? tomplate Linux - General 3 02-26-2002 08:39 PM

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

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