请考虑以下代码。如果我删除了eval
而只写了if "$1"
,则它不起作用。是否可以摆脱eval
?
assert () {
if eval "$1" #Is it possible to get rid of the eval?
then
echo "Assertion OK: \"$1\""
else
echo "Assertion FAILED: \"$1\""
fi
}
assert " [[ /tmp/a = /tmp/* ]]"
将括号扔掉,然后输入并完成
#!/bin/bash
assert () {
if [[ $1 ]]
then
echo "Assertion OK: \"$1\""
else
echo "Assertion FAILED: \"$1\""
fi
}
assert " /tmp/a = /tmp/* "