Testing the bash shell script

Can someone explain how to check the bash shell script?

For example, I have 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? As in Java, you have unit testing where you write code like assertEquals to test code that produces the desired result.

+6
bash shell
source share
4 answers

You can make statements in Bash. Check out this from the Bash-Scripting Guide:

http://tldp.org/LDP/abs/html/debugging.html#ASSERT

+7
source share

I would add echo in front of mv to make sure that the correct commands are created for starters. (It is always a good idea with teams that can be difficult to roll back changes.)

Some useful resources:

+3
source share

Try the following: assert.sh

 source "./assert.sh" local expected actual expected="Hello" actual="World!" assert_eq "$expected" "$actual" "not equivalent!" # => x Hello == World :: not equivalent! 
+2
source share
 #!/bin/sh for file in *.txt; do echo mv "$file" "`basename $file .txt`.doc" done 
-one
source share

All Articles