String comparison does not work in Shell [duplicate] - linux

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
I'm pretty new to shell programming languages. Why does the following code echo false after printing "File or directory not found."?
#!/bin/sh -xu
ARG_PATH="/srv/path/to/Something"
if ["$ARG_PATH" = "/srv/path/to/Something"]
then
echo "true!"
else
echo "false!"
fi
I've tried running the code in sh and bash, doesn't really change anything.

check whether path is correct or not ? your syntax is seems okay except Bash is space sensitive give the space after [ and before ]
ARG_PATH="/srv/path/to/Something"
if [ $ARG_PATH = "/srv/path/to/Something" ]
then
echo $? #display 0 if both r same
fi

Put spaces around the bracket, if you don't the shell will think ["$ARG_PATH" is the command when it should be [.
The correct test thus is if [ "$ARG_PATH" = "/srv/path/to/Something" ]

Related

regex work on Regex101 but doesn't work on bash [duplicate]

This question already has answers here:
What regex syntax is supported by the =~ operator in bash?
(2 answers)
How to match version of a command using bash "=~"?
(1 answer)
Closed 4 years ago.
I'm setting a hook and i need to evaluate a regex, but i don't know why is not working the regex in bash, i check a format in commit-msg
MSG = "$1"
FEAT='(feat)(\:\sRQ)([0-9])+(_)(([A-Z][a-z]+)+)'
if [[ MSG =~ TEST ]] ; then
echo "yeah!!"
else
echo "oops"
exit 1
fi
this is a valid commit message feat: RQ00_Hello
I'm sure that \: is unnecessary, and believe that bash doesn't understand \s.
P.S.: Btw, you also need to lose the SPACES around the = in the first line... the variables in the test are missing $, and $TEST != $FEAT

Am I using the correct syntax for my shell script? [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I wrote a shell script that gets a value from a file and based on that value I want to echo a particular message. My console keeps on saying that there is an error on line 7 and 9. Any suggestions on how to fix it will be greatly appreciated.
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home'
echo $JAVA_HOME
export CLASSPATH='/Users/edgarjohnson/Desktop/JarFiles/mlDownload.jar:/ddc/config'
echo $CLASSPATH
var=$(cat /ddc/config/LastRefreshDate.dat)
echo $var
if [$var > 0 ];then
echo "Run Get Latest Update Class"
elif [$var = 0]; then
echo "No need to run any updates"
fi
After [ and before ] there must be a space. Otherwise the variable is substituted and the shell will try to execute a program called [Whatever.
[ itself is actually just a binary which is executed with var's content, =, 0 and ] as arguments and its return code is used to determine whether the if or else branch should be taken.
However the operators used are not really the ones you intend to use, e.g. > is interpreted as shell redirect creating a file called 0 (or overwritting it) and is not actually comparing anything, use -gt instead. = checks string equality, -eq checks value equality.
As mentioned in the comments it may be better to use [[ ]] instead of [ ].

"if[ 0 -lt 1] command not found" error in shell script [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Why is whitespace sometimes needed around metacharacters?
(5 answers)
Closed 5 years ago.
I got error message as if[ 0 -lt 1] command not found. I just started shell scripting. I don't know what's wrong in my code:
if[ $# -lt 1 ]
then
echo "Give a number as a parameter. Try again."
else
n=$1
sum= 0
sd=0
while[ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digits for $1 is $sum"
You need a space between if and [. You'll have the same issue after while.
The [ ] notation is kind of an oddity in shell. It looks like part of the language, but it actually isn't. The if and while words always need to be followed by whitespace and then a command, which is executed, and considered to be true or false according to whether its exit code is zero or nonzero.
So there is actually a command named [ which evaluates the conditions given on its command line, and terminates with an exit code according to whether the condition evaluated true or false. You can see an executable in the /usr/bin directory on most systems (though the shell usually has a built-in version for efficiency). It works the same as test.
Also, keep in mind that if needs a matching fi after the else clause.
Check out https://www.shellcheck.net/ (Open Source - free)
It allows you to check your shell scripts before running them. You just copy your shell script into the browser and do a syntax check, you can also run locally.
Why?
Well, this I think will really help you learn what you are doing wrong.
i.e. Fix spacing errors etc.
Example Output:
$ shellcheck myscript
Line 1:
if[ $# -lt 1 ]
^-- SC1046: Couldn't find 'fi' for this 'if'.
^-- SC1073: Couldn't parse this if expression.
^-- SC1069: You need a space before the [.
Line 8:
while[ $n -gt 0 ]
^-- SC1069: You need a space before the [.
Line 15:
^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
^-- SC1072: Expected 'fi'. Fix any mentioned problems and try again.
$

Linux Shell Scritping - checking arguments [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
Trying to make a program where it takes two arguments from the command line and adds them together. However if there are not two arguments passed it needs to ask for two arguments.
if [$# = 2]; then
ans=$(($1 + $2))
echo "$1 + $2 = $ans"
else
echo "Pass me two Arguments!"
fi
What is the correct way to check whether the number of arguments is equivalent to a number?
You got it but just forgot some escaping quotes and the spaces before/after the test ([$# = 2]). Fixed:
if [ "$#" = 2 ]; then
ans=$(($1 + $2))
echo "$1 + $2 = $ans"
else
echo "Pass me two Arguments!"
fi
Odd as it may seem, [ is actually the name of an executable just like echo, ls and what have you:
$ which [
/usr/bin/[
This means that if you don't have a space after [, you're trying to start a program called [your_statement. The space is needed to separate the executable from its arguments.
Most *nix systems allow you to use the equivalent test executable rather than [ for a more intuitive syntax: if test "$#" = 2; then ...
By omitting the quotes in the if statement, bash/shell interprets everything following the # as a comment and so does SO's code formatting tool (everything after if [$ is greyed out in your code but not in mine because I added the double quotes in "$#").
Btw. ShellCheck can be very helpful.

bash, issue on if condition [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 7 years ago.
I'm trying to follow these explanations on if condition: http://linuxconfig.org/bash-printf-syntax-basics-with-examples
My code is
#!/bin/bash
result="test"
if [$result = "test"];
then printf "ok"
fi
But I have the following error:
line 4: [test: command not found
[ is a command. There must be spaces between not only it and its first argument, but between every argument.
In bash (and also POSIX shells), [ is equivalent to test command, except that it requires ] as the last argument. You need to separate command and its arguments for the shell doing token recognition correctly.
In your case, bash think [test is a command instead of command [ with argument test. You need:
#!/bin/bash
result="test"
if [ "$result" = "test" ]; then
printf "ok"
fi
(Note that you need to quote variables to prevent split+glob operators on them)

Resources