我试图在if
语句中将参数传递给函数,然后评估函数返回的内容(在 bash 中)。我得到的结果是不正确的。你如何正确地做到这一点?
#!/bin/bash
foo() {
if [ $1 = "zero" ]; then
echo "0"
else
echo "1"
fi
}
arg="zero"
if foo $arg -eq 0 ; then
echo "entered 0"
else
echo "entered something else"
fi
arg="blah"
if foo $arg -eq 0 ; then
echo "entered 0"
else
echo "entered something else"
fi
不是想要的结果:
cknight@macbook:~/work$ ./test.sh
0
entered 0
1
entered 0