Linux 为什么没有';不行?bash中的If指令
我的剧本有问题。我不知道怎么修理它Linux 为什么没有';不行?bash中的If指令,linux,bash,Linux,Bash,我的剧本有问题。我不知道怎么修理它 location=$(locate wpa_supplicant | sed -n '2p') x=$(ls -l /etc | grep "su" | sort | head -n1 | cut -d " " -f13) y=$(du -h $location) a=$(test -f $location) b=$(test -b $location) if [ $a = 0 ] || [ $b = 0 ]; th
location=$(locate wpa_supplicant | sed -n '2p')
x=$(ls -l /etc | grep "su" | sort | head -n1 | cut -d " " -f13)
y=$(du -h $location)
a=$(test -f $location)
b=$(test -b $location)
if [ $a = 0 ] || [ $b = 0 ]; then
echo "This is file"
else
echo "This is not file"
fi
我启动了此脚本,但出现错误:
./ko.ssh:第18行:[:=:应为一元运算符
./ko.ssh:第18行:[:=:应为一元运算符
怎么了?两个
$a
和$b
都是空的。因此,shell得到的是
if [ = 0 ] || [ = 0 ]
这会产生您遇到的错误。请将变量双引号引起来
[ "$a" = 0 ]
使贝壳看见
if [ "" = 0 ]
两个变量都为空的原因是赋值
a=$(test -f $location)
$(…)
是命令替换,它返回封闭命令的输出。但是test
没有输出任何内容,您对它的返回值感兴趣
test -f $location
a=$?
test -b $location
b=$?
或者直接使用条件
if [ -f "$location" ] || [ -b "$location" ] ; then
注意双引号
如果您使用的是bash,并且不关心到其他shell的可移植性,那么可以切换到双方括号,它不需要引号,并且可以自己处理逻辑运算符:
if [[ -f $location || -b $location ]] ; then
另见,(东南欧),(东南欧),(东南欧),(东南欧)等。