Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 为什么没有';不行?bash中的If指令_Linux_Bash - Fatal编程技术网

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
另见,(东南欧),(东南欧),(东南欧),(东南欧)等。