Super Simple bash If (variable > integer) treating (variable as a command [duplicate] - bash

This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
I feel like this should be really simple but I can't get past this step.
$num = 5
if [$num > 2]; then echo greater; fi
the problem is, I keep getting [5: command not found.
Why is it not evaluating the if [ test ] block correctly? It's like the shell forgot about the if and just moved on to "hey, [5 > 2 does not look like a command, I can't find [5"... but [5 isn't a command, it's part of the if test block?
I have tried using different brackets and using -gt instead of >. The problem is bash doesn't actually ever do the test. For some reason it ignores if.

Just managed to find the answer:
if compare strings get a "command not found"-Error
really unexpected, there needs to be a space between the [ brackets and the variable.
I would never have guessed.

Related

Please explain what is happening with this 2 lines of bash code [duplicate]

This question already has answers here:
Assigning default values to shell variables with a single command in bash
(11 answers)
Closed 3 years ago.
im trying to learn bash right now, came across a script and im not 100% sure if i read it right.
source_dir_123=${SOURCE_DIR:-/tmp}
echo source_dir_123=$source_dir_123
What is happening here? I guess this is some kinda variable assigment, but it looks weird to me.
What type of assigment/operation happens here? Any specific name of these types of assignments?
Sorry for a newbish question, but i dont get it why would you use these kinda of assignments instead of something more straight forward like
source_dir_12="/tmp"
/tmp is the default value for source_dir_123 in case SOURCE_DIR is not set, then you're displaying the result to the console.
See the following example:
> echo $SOURCE_DIR
> source_dir_123=${SOURCE_DIR:-/tmp}
> echo source_dir_123=$source_dir_123
source_dir_123=/tmp
# now let's set SOURCE_DIR
> SOURCE_DIR=/test
> source_dir_123=${SOURCE_DIR:-/tmp}
> echo source_dir_123=$source_dir_123
source_dir_123=/test

Problems with while in bash [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 years ago.
I am doing a bash script with the web api of checkpoint.
126: No such file or directory
this error refers to line 25 in which I have the next sentence:
while [$reglas < $n_reglas ];do
I have tried changing that to:
while [$reglas -lt $n_reglas ];do
but the error persist and I am not sure of what is the real problem.
The variables are defined like this:
reglas=1
n_reglas=$(echo $rulebase_list | jq '.total')
and I have printed their value in order to check that they are taking the correct value. Any idea of which is the problem?
Thanks!
Unfortunately it is not very clear what the actual problem is. Please try to reproduce the problem in a small code snippet that we can execute and troubleshoot. This way we don't need to guess and can provide more precise help.
Here is a small snippet with a ton of assumptions.
reglas=1
n_reglas=4 # https://jqplay.org/s/BCTXyJ4NLc
while [ $reglas -lt $n_reglas ]; do
echo $reglas
reglas=$(($reglas+1))
done
# $bash -f main.sh
# 1
# 2
# 3

Syntax error using expr to get string length [duplicate]

This question already has answers here:
How to store the length of a string in a variable in bash?
(2 answers)
Closed 4 years ago.
On mac running 4.4.23(1), an example I've seen used for finding the length of a string throws an error:
string_var=blah
echo `expr length $string_var`
expr: syntax error
Works fine on my Debian system.
shopt options are: himvBHs
expr is not part of bash -- it's an ancient UNIX tool from back when the shell couldn't do math (or much else useful) on its own.
You don't need it. In the modern day, ${#var} will give you the length of the value assigned to var, as follows:
string_var=blah
echo "${#string_var}"

BASH remove specific tokens from a word [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I am trying to find te longest word in a given file. Before I check the lengtgh of each word I need to remove all of the following tokens {,.:} that may be attached (once or more) to the word. so for example, for this text:
:,cat dog, encyclopedia; remove:.,
i need the result:
cat dog encyclopedia remove
I am trying this, but I get a "command not found":
longest=0
for word in $(<$1)
do
#new_word = $(echo "${word//[.,:]/}")
new_word = "${word//[.,:]/}"
len=${#new_word}
if (( len > longest ))
then
longest=$len
longword=$new_word
fi
done
echo The longest word is $longword and its length is $longest.
thank you.
Your use of parameter expansion replacement pattern is correct.
The problem is that there must not be any whitespace around = while declaring variables in bash (any shell in general).
So, the following should work:
new_word="${word//[.,:]/}"
As an aside, use a while read ... construct to loop over the lines in a file, using for is pretty fragile.

Why Pycharm gives warning on "simple variable usage" in .sh bash script? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
In Pycharm when we use variable e.g. $privateKey, we get the warning Simple variable usage as below snapshot and recommend us to turn to the syntax ${privateKey}
My question is why we get such warning? What is the risk to use simple variable like that?
When clicking more
Thanks to #Whymarrh. One answer is as below.
since "$foobar" would instead expand foobar
My answer is to separate/distinguish $myVar and notInVar in string "$myVarnotInVar"
In other words
myVar=122
echo "$myVarnotInVar" # will print empty string "" since undefined variable $myVarnotInVar
echo "${myVar}notInVar" # will print 122notInVar

Resources