I'm trying to write out a script that will automate one of my more tedious tasks. I've pretty much got everything down and can get the individual components of code to do what I want them to, but I'm running into trouble trying to get the if statement to work. Here's what I'm trying to do:
if [ ack --ignore-case 'foo' ]; then
Do this action
else
Do that action
fi
As of now it always does the else action, even if the ack parameter is true.
Thanks!
Remove the [
- you want to check the exit status of ack:
if ack --ignore-case 'foo'; then
ack returns a 0 (success) exit status if a match is encountered.
Although it may look like a syntactical construct, [
is in fact a function (synonymous with test
), which returns 0 if the test passes. if
is looking at the return code of this function. In this case, ack gives you the return code that you need instead, so you shouldn't also be using [
.