I just started doing shell scripts and getting unknown operand error while using regex in if statement. i searched google but did not get anything
IP="172.21.1.1"
if [[ "$IP" =~ /d ]] ; then
echo "qqq"
fi
Getting error as
sh: =~: unknown operand
Bash version is : BusyBox v1.19.3 (2012-01-31 08:57:52 PST) built-in shell (ash)
This is happening because the operator =~ doesn't existe for bash.
As see you are trying to use a Regex to compare your variables. I recommend to use the expr command. Here is an example:
IP="172.21.1.1"
if [[ $(expr match "$IP" 'my_regex') != 0 ]]; then echo "qqq"; fi;
Related
I try to built a Programm where I can create Text-Files and folders. The user can type in i.a. the directory. To check if the given path exits I created a while-loop but it doesn’t work.
My question is where are my errors and how can I fix them.
while [[ test -e $path == false]]
do echo “type in a valid path”
read -r path
done
Error:
./Create: line 28: conditional binary operator expected
./Create: line 28: syntax error near `e'
./Create: line 28: `while [[ test -e $path == false ]]'
I found a couple of issues:
You should use [[ or test, but not both
false is not a constant in bash
The fix:
while [[ ! -e $path ]]
do
....
done
I have a script which utilises SaltStack's command-line as well as BASH commands. The script is used to gather data from multiple Linux servers (hence SaltStack), one of the checks which I would like to gather is disk space.
I have done this by using the following command:
salt $i cmd.run 'df -Ph / | tail -n1 | awk '"'"'{ print $4}'"'"'' | grep -v $i
$i = hostname and the use of the ugly '"'"' is so that my command can run via SaltStack as Salt's remote execution functionality requires single quotes around the command, if I left them in my command wouldn't run inside my BASH script.
Example syntax:
salt $hostname cmd.run 'command here'
After many questions on here and with colleagues I have this section of the script sorted. However I now the problem of stripping the output of my above command to remove the 'G' so that my script can compare the output with a threshold I have defined and turn the HTML which this script is piping to red.
Threshold:
diskspace_threshold=5
Command:
while read i ; do
diskspace=`salt $i cmd.run 'df -Ph / | tail -n1 | awk '"'"'{ print $4}'"'"'' | grep -v $i`
Validation check:
if [[ "${diskspace//G}" -lt $diskspace_threshold ]]; then
ckbgc="red"
fi
The method I have used for stripping the G works on the command line but not within my script so it must be something to do with the syntax or just the fact that it is now within a script. Any ideas/thoughts would be helpful.
Cheers!
EDIT: Here is the error message I receive when running my script:
serverdetails.sh: line 36: p
: 2.8: syntax error: invalid arithmetic operator (error token is ".8")
I assume the error is coming from here (is this line 36?)
if [[ "${diskspace//G}" -lt $diskspace_threshold ]]; then
Note the error message:
serverdetails.sh: line 36: p : 2.8: syntax error: invalid arithmetic operator (error token is ".8")
bash does not do floating point arithmetic
$ [[ 2.8 -lt 3 ]] && echo OK
bash: [[: 2.8: syntax error: invalid arithmetic operator (error token is ".8")
You'll need to do something like this:
result=$( bc <<< "${diskspace%G} < $diskspace_threshold" )
if [[ $result == 1 ]]; then
echo OK
else
echo Boo
fi
I have a script to match a pattern, and if it matches, I used the match to append to a variable.
My script works on Bash v3.2.57 and fails on v4.3.30.
Could someone tell me what is wrong with the second ifcondition that matches a pattern here?
#!/bin/sh
if [ -f .file-to-read ]; then
while read p; do
echo "yes"
if [[ $p =~ "#user/"(.+)"#"[0-9]+"."[0-9]+"."[0-9]+ ]]
then
var="$var<#user/${BASH_REMATCH[1]}|$p>\\n"
fi
done < .file-to-read
fi
The error message is
Syntax error: "(" unexpected (expecting "then")
I figured out the issue. The problem was the file was being executed in #!/bin/sh when it should have been #!/bin/bash
I'm trying to add a function to my bash_profile for msysgit:
function git-unpushed {
brinfo=$(git branch -v | grep git-branch-name)
if [[ $brinfo =~ ("[ahead "([[:digit:]]*)]) ]]
then
echo "(${BASH_REMATCH[2]})"
fi
}
But I get the following error:
bash: conditional binary operator expected`
bash: syntax error near =~'
From what I can find, the "equals tilde" operator (=~) evaluates as regex in bash.
Why is =~ is throwing an error?
UPDATE: Here's a screenshot of inputting it manually (this is running sh.exe):
I had the same error on Bash 3.1.0 from Git installation on Windows. Ultimately I changed it to:
if echo $var | grep -E 'regexp' > /dev/null
then
...
fi
According to https://groups.google.com/forum/#!topic/msysgit/yPh85MPDyfE this is because msys doesn't ship libregex along with bash. Supposedly if you compile/find an msys built libregex, and put it in the library path, =~ starts working fine.
Update 2015: msysgit is now obsolete.
You should use the bash which comes with git-for-windows.
As mentioned in this answer, it uses a much more recent bash (4.3+), for which the =~ syntax will work.
Original answer (march 2013)
The bash packaged with msysgit might simply be too old to fully support this operator.
It is certainly too old to compare with unquoted regex, as mentioned in "Bash, version 3" and "How do I use regular expressions in bash scripts?":
As of version 3.2 of Bash, expression to match no longer quoted.
Actually, mklement0 mentions in the comments:
=~ was introduced in bash 3.0 and always supported an unquoted token on the RHS.
Up to 3.1.x, quoted tokens were treated the same as unquoted tokens: both were interpreted as regexes.
What changed in 3.2 was that quoted tokens (or quoted substrings of a token) are now treated as literals.
But I tried with quotes (in the latest msysgit 1.8.1.2), and it still fails:
vonc#voncvb /
$ /bin/bash --version
GNU bash, version 3.1.0(1)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.
vonc#voncvb /
$ variable="This is a fine mess."
vonc#voncvb /
$ echo "$variable"
This is a fine mess.
vonc#voncvb /
$ if [[ "$variable" =~ T.........fin*es* ]] ; then echo "ok" ; fi
bash: conditional binary operator expected
bash: syntax error near `=~'
vonc#voncvb /
$ if [[ "$variable" =~ "T.........fin*es*" ]] ; then echo "ok" ; fi
bash: conditional binary operator expected
bash: syntax error near `=~'
vonc#voncvb /
Here is a solution that supports extracting matched strings. If the operator =~ is not supported by bash, then the sed command is used (installed with msysgit)
if eval "[[ a =~ a ]]" 2>/dev/null; then
regexMatch() { # (string, regex)
eval "[[ \$1 =~ \$2 ]]"
return $?
}
elif command -v /bin/sed >/dev/null 2>&1; then
regexMatch() { # (string, regex)
local string=$1
if [[ ${2: -1} = $ ]]; then
local regex="(${2%$})()()()()()()()()$"
else
local regex="($2)()()()()()()()().*"
fi
regex=${regex//\//\\/}
local replacement="\1\n\2\n\3\n\4\n\5\n\6\n\7\n\8\n\9\n"
local OLD_IFS=$IFS
IFS=$'\n'
BASH_REMATCH=($(echo "$string" | /bin/sed -rn "s/$regex/$replacement/p" | while read -r; do echo "${REPLY}"; done))
IFS=$OLD_IFS
[[ $BASH_REMATCH ]] && return 0 || return 1
}
else
error "your Bash shell does not support regular expressions"
fi
Usage example:
if regexMatch "username#host.domain" "(.+)#(.+)"; then
echo ${BASH_REMATCH[0]}
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
fi
I have the following bash code, which is copied and pasted from "bash cookbook" (1st edition):
#!/bin/bash
VERBOSE=0;
if [[ $1 =-v ]]
then
VERBOSE=1;
shift;
fi
When I run this (bash 4.0.33), I get the following syntax error:
./test.sh: line 4: conditional binary operator expected
./test.sh: line 4: syntax error near `=-v'
./test.sh: line 4: `if [[ $1 =-v ]]'
Is this as simple as a misprint in the bash cookbook, or is there a version incompatibility or something else here? What would the most obvious fix be? I've tried various combinations of changing the operator, but I'm not really familiar with bash scripting.
Bash uses spaces to tokenise scripts. The line:
if [[ $1 =-v ]]
should be:
if [[ $1 = -v ]]