我有一个 bash 语句来测试命令行参数。如果传递给脚本的参数是“clean”,则脚本将删除所有 .o 文件。否则,它会构建一个程序。但是,无论传递什么(如果有的话),脚本仍然认为传递了参数“clean”。
#!/bin/bash
if test "`whoami`" != "root" ; then
echo "You must be logged in as root to build (for loopback mounting)"
echo "Enter 'su' or 'sudo bash' to switch to root"
exit
fi
ARG=$1
if [ $ARG=="clean" ] ; then
echo ">>> cleaning up object files..."
rm -r src/*.o
echo ">>> done. "
echo ">>> Press enter to continue..."
read
else
#Builds program
fi
答案1
[
您需要用空格分隔参数;另外,相等运算符是单个=
,您应该引用两边:
if [ "$ARG" = "clean" ]; then