bash, issue on if condition [duplicate] - string

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)

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

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

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

String comparison does not work in Shell [duplicate]

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" ]

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.

Resources