我有一个bash脚本,如下所示
if [ -f "/sdcard/testfile"]
then
echo "exists" > /sdcard/outfile
else
echo "does not exist" > /sdcard/outfile
fi
Run Code Online (Sandbox Code Playgroud)
我有足够的权限来运行它/system/bin/sh.我从我的应用程序调用此脚本并运行它/system/bin/sh.但是在运行后我得到了假,即使文件'/ sdcard/testfile'存在.当我在adb shell中明确运行时,我收到此错误
[: not found
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以完成这项任务?由于应用程序的权限问题,我不能只使用java.io.File; 因此,我坚持使用shell脚本(命令).
我需要应用程序本身的输出.我的意思是,
if(filesAreAvailable)
executeSomething();
else
executeSomethingElse();
Run Code Online (Sandbox Code Playgroud)
基本上我是以编程方式在/data/data/myPackageName/files目录中编写此脚本并调用命令:
if [ -f "/sdcard/testfile"]
Run Code Online (Sandbox Code Playgroud)
如
fileWriterScript.write("if [ -f \"/sdcard/testfile\" ]\n")
Run Code Online (Sandbox Code Playgroud)
使用时test,需要在开口支架后和关闭支架前留出空间.
来自man test:
SYNOPSIS
test expression
[ expression ]
Run Code Online (Sandbox Code Playgroud)
所以改变:
[ -f "/sdcard/testfile"]
Run Code Online (Sandbox Code Playgroud)
至:
[ -f "/sdcard/testfile" ]
Run Code Online (Sandbox Code Playgroud)
如果您需要在 bash 脚本中使用它,那么您可以这样做:
if [[ `adb shell ls /sdcard/path/to/your.file 2> /dev/null` ]]; then
echo "File exists";
else
echo "File doesn't exist";
fi
Run Code Online (Sandbox Code Playgroud)