Reputation: 1215
These two lines
function some {
## myFunc(true) is just a pattern
local isEnabled="grep myFunc(true) $indexFile | grep true"
if [ -z $($isEnabled) ]; then ... fi
}
give me : binary operator expected
but when I remove pipe symbol |
it works, how to make command with pipe being executed ? I am using sh
Upvotes: 2
Views: 483
Reputation: 42999
You are getting that error because $($isEnabled)
is expanding to nothing and [ -z ]
requires an argument.
myFunc(true)
in single or double quotes since ()
has special meaning$indexFile
in double quotes to prevent the same issueYou can rewrite your code for sh
:
function some {
local isEnabled=$(grep 'myFunc(true)' "$indexFile" | grep true)
if [ -z "$isEnabled" ]; then
: your logic here
fi
}
Or, more directly as:
function some {
# just interested in the presence or absence of a pattern
# irrespective of the matching line(s)
if grep 'myFunc(true)' "$indexFile" | grep -q true; then
: your logic here
fi
}
Or, use [[ ]]
in Bash:
function some {
local isEnabled=$(grep 'myFunc(true)' "$indexFile" | grep true)
if [[ $isEnabled ]]; then
: your logic here
fi
}
[[ $var ]]
is as good as [[ -z $var ]]
or [[ -n $var ]]
. It will evaluate to true as long as $var
has a length > 0.
no need to enclose variables inside [[ ]]
in quotes - Bash handles the expansion without any word splitting or wildcard expansion issues.
Upvotes: 1