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've 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With