LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cannot get bash if command correct (https://www.linuxquestions.org/questions/linux-newbie-8/cannot-get-bash-if-command-correct-4175633214/)

bkelly 07-03-2018 05:48 PM

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?

Keith Hedger 07-03-2018 06:00 PM

use space after 'if' and before '[', -eq NOT '=' for integers, space before ']', dont forget 'then' and 'fi'

keefaz 07-03-2018 06:08 PM

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
  ...


scasey 07-03-2018 06:09 PM

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 ]

bkelly 07-03-2018 06:12 PM

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.

keefaz 07-03-2018 06:16 PM

Quote:

Originally Posted by bkelly (Post 5875201)
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

bkelly 07-03-2018 06:27 PM

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.

michaelk 07-03-2018 06:34 PM

This might be easier to understand

https://linuxacademy.com/blog/linux/...if-statements/

Keith Hedger 07-03-2018 06:46 PM

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.

bkelly 07-03-2018 06:47 PM

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.

scasey 07-03-2018 08:02 PM

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


All times are GMT -5. The time now is 07:02 AM.