做 Android 开发时,预制 test.sh 到 system/bin/test.sh ,到串口执行这个脚本报错如下,
console:/ # ./system/bin/test.sh
/system/bin/sh: ./system/bin/test.sh: No such file or directory
console:/system/bin # sh startsoftdetector.sh
inaccessible or not found
test.sh[23]: syntax error: unexpected 'fi'
有这个文件却提示 No such file or directory
,
sh 脚本语法没问题却提示 syntax error: unexpected 'fi'
,
cat -v ./system/bin/test.sh 查看文件,发现每一行最后都有 ^M ,说明是格式问题。
cat -v 的解释:
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
它和换行符相关,不同操作系统和编辑器下的换行符是有差异的
这个脚本是在 Windows 下编辑,然后拷贝到 Linux 服务器上的,所以格式出问题了。
解决办法:
- 1.手动编辑,删除每一行最后的 ^M 。
- 2.使用 dos2unix system/bin/test.sh 命令修改文件格式。
更多去掉 ^M 的方法参考 Linux中去掉 ^M 的方法-CSDN博客