【基本介绍】
在编写shell脚本的时候会经常遇到“line 2: [: too many arguments”类似的错误,特别是在if判断语句中。
【简单案例】
[root@bogon script]# cat bb.sh
tmp="grep Accept /root/test.log | grep -v something"
if [ $tmp ]
then
echo "aa"
else
echo "bb"
fi
[root@bogon script]# bash bb.sh
bb.sh: line 2: [: too many arguments
bb
解决方案:在if判断的变量尽可能的添加双引号
[root@bogon script]# cat bb.sh
tmp="grep Accept /root/test.log | grep -v something"
if [ "$tmp" ]
then
echo "aa"
else
echo "bb"
fi
[root@bogon script]# bash bb.sh
aa
【参考】
http://fengyonghui.iteye.com/blog/568541
分享到:
相关推荐
在Linux的Bash Shell环境中,当你尝试执行一个命令或脚本时,如果你遇到了"too many arguments"的错误,这通常意味着你传递给命令的参数数量超过了它的限制,或者是bash解析命令行的方式导致了误解析。这个问题通常...
Bash是最常用的一种,它是Bourne Again SHell的缩写,是Unix和Linux系统默认的shell。 【命令的使用】 在Linux中,命令通常由命令、选项和参数组成。命令提示符#表示当前用户为管理员,$表示普通用户。获取命令...
10)..Fixed: Ignore timeouts from Shell_NotifyIcon 11)..Fixed: Possible failure to handle/process stack overflow exceptions 12)..Changed: VCL/CLX/FMX now will assign Application.OnException handler ...
configuring many of the aspects of PHP's behavior. ; PHP attempts to find and load this configuration from a number of locations. ; The following is a summary of its search order: ; 1. SAPI module ...
echo "Too many arguments" fi } ``` 8. **错误处理** 可以使用`set -e`命令使脚本在遇到错误时立即退出,这有助于编写更健壮的函数。例如: ```bash safeCommand() { set -e # 执行可能会出错的命令 ...
4.3 Your Program’s Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 Your Program’s Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Your ...