有人可以解释如何测试bash shell脚本吗?
例如,我有一个包含此代码的.sh文件...
#!/bin/sh
for file in *.txt; do
mv "$file" "`basename $file .txt`.doc"
done
Run Code Online (Sandbox Code Playgroud)
我该如何为它编写测试?就像在Java中一样,你有单元测试,你可以在其中编写像assertEquals这样的代码来测试代码,从而得到所需的输出.
你可以在Bash中做断言.从Advanced Bash-Scripting Guide中查看:
http://tldp.org/LDP/abs/html/debugging.html#ASSERT
试试看:assert.sh
source "./assert.sh"
local expected actual
expected="Hello"
actual="World!"
assert_eq "$expected" "$actual" "not equivalent!"
# => x Hello == World :: not equivalent!
Run Code Online (Sandbox Code Playgroud)
对于初学者来说,我会echo在 前面添加mv来验证是否正在创建正确的命令。(使用可能难以撤消更改的命令总是一个好主意。)
一些可能有用的资源:
\n\n