Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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
如何使用bash检查一个文件是否为空,另一个文件是否处于一种状态_Bash_Conditional Statements - Fatal编程技术网

如何使用bash检查一个文件是否为空,另一个文件是否处于一种状态

如何使用bash检查一个文件是否为空,另一个文件是否处于一种状态,bash,conditional-statements,Bash,Conditional Statements,我有两个文件,我想检查其中一个是否为空,另一个是否为空。这就是我尝试写的: if [ !-s $tmp2 && -s $tmp ];then 我只想检查tmp是否为空,tmp2是否为空,但无论我做什么,它都不起作用,有时第一个条件总是正确的,有时第二个条件总是正确的。我已经搜索了这个网站,没有找到任何可以帮助我的东西。以下应该可以 if [[ ! -s $tmp && -s $tmp2 ]] ; then echo "OK" else echo "

我有两个文件,我想检查其中一个是否为空,另一个是否为空。这就是我尝试写的:

if  [ !-s $tmp2 && -s $tmp ];then

我只想检查tmp是否为空,tmp2是否为空,但无论我做什么,它都不起作用,有时第一个条件总是正确的,有时第二个条件总是正确的。我已经搜索了这个网站,没有找到任何可以帮助我的东西。

以下应该可以

if [[ ! -s $tmp && -s $tmp2 ]] ; then
   echo "OK"
else
   echo "Not OK"
fi ;
-s表示文件存在且大小大于0

!-s表示文件为空(或不存在)


因此,上面检查tmp是否为tmp2是否为非空

您正在逆转意图。这样做-

if [ ! -s "$tmp" ] && [ -s "$tmp2" ]; then
更新

确保用
fi
结束
if
语句

[jaypal~]$ ls tmp* # Checking to see if tmp file exists
tmp2

[jaypal~]$ cat sc.sh 
#!/bin/bash

a=tmp
b=tmp2

if [ ! -s $a ] && [ -s $b ]; then
echo "ok"
fi    # End your if statement with fi

[jaypal~]$ ./sc.sh 
ok # Output confirms that tmp file does not exist and tmp2 exists and has size greater than zero

它仍然不起作用。。。现在我得到了这个错误:[:缺少']',这意味着什么?我假设在那一行之后还有更多的东西要添加。单凭它自己是行不通的!类似这样的东西可能
if[[!-s$tmp&&s$tmp2];然后回显“OK”,否则回显“Not OK”fi
此外,还必须定义tmp和tmp2,或者将其替换为文件的路径。您希望将bash显式用于
[…]
。普通的
sh
可能无法理解它。