Testing a bash shell script [duplicate]
Can someone explain how to test for a bash shell script?
For example i've got a .sh file with this code in it...
#!/bin/sh
for file in *.txt; do
mv "$file" "`basename $file .txt`.doc"
done
How do I write a test for it? Like in Java you'开发者_如何学Pythonve got unit testing where you write code like assertEquals to test the code gives the desired output.
Try this out: assert.sh
source "./assert.sh"
local expected actual
expected="Hello"
actual="World!"
assert_eq "$expected" "$actual" "not equivalent!"
# => x Hello == World :: not equivalent!
You can do asserts in Bash. Check out this from the Advanced Bash-Scripting Guide:
http://tldp.org/LDP/abs/html/debugging.html#ASSERT
I'd add an echo
in front of the mv
to verify that the right commands are being created, for starters. (Always a good idea with commands that make possibly difficult to undo changes.)
Some possibly useful resources:
- shUnit2 — xUnit framework for script unit testing
- Bash IDE for Vim
- Bash debugger
精彩评论