【发布时间】:2018-10-02 09:56:13
【问题描述】:
我想检查 Android 上是否存在目录。
从an answer to Check if a file exists with a wildcard in a shell script,我得到了一个想法。所以我使用ADB,如下所示。
if [ adb shell ls ${test_dir} 2> /dev/null ] ;
then
echo "files exist"
else
echo "files do not exist"
fi
我是 Bash 脚本的新手。我知道adb shell ls 将返回所有文件名。但是2> /dev/null是什么意思呢?
我只关心files do not exist 条件。那么如何反转条件呢?
第二版
if [ ! adb shell ls ${test_dir} 2> /dev/null ] ;
then
echo "files exist"
else
echo "files do not exist"
fi
添加! 对我不起作用。
【问题讨论】:
-
将所有错误消息发送到
/dev/null(比特桶)用if [ ! .... ]反转 -
@DavidC.Rankin 非常感谢。你能回答这个问题吗?
-
好的,我会写的。
-
不要在命令周围使用方括号;它们用于测试表达式。见my answer here。