Solved Debugging a shell script?

Greetings all,

I have recently wrote a shell script using vi(1) and had quite a difficulty to debug it. Eventually, I was able to make the script work using mainly
Code:
sh -en myscript.sh
, which causes the script to exit on errors. However, it was rather tedious, so I am wondering if there is a better way to debug a shell script.

The second issue I had was that vi(1) has no, or I was unable to find any way to set, concept of syntax and/or syntax checking. So I am wondering if there is a shell commands aware editor.

Any help in this matter would be appreciated.

Kindest regards,

M
 
I often use the -x switch:
Code:
dice@armitage:~/test % sh -x test.sh
+ FOO=hello
+ BAR=world
+ [ == hello ]
[: ==: unexpected operator
dice@armitage:~/test % cat test.sh
#!/bin/sh

FOO='hello'
BAR='world'

if [ $FOOD == 'hello' ]; then

        echo $FOO $BAR
fi

The $FOO/$FOOD typo is intentional ;)

The second issue I had was that vi(1) has no, or I was unable to find any way to set, concept of syntax and/or syntax checking.
Install editors/vim-console and add an alias so vi myfile actually runs vim(1) instead.

Something I often add to my ~/.cshrc:
Code:
if ( -x /usr/local/bin/vim ) then
 setenv EDITOR vim
 alias vi vim
endif
The test prevents errors in case vim(1) isn't installed.
 
And this is why I always use # comment sections in my shell scripts.

As to debugging: depends on the structure of the script. I usually take it apart, cut it up in small pieces, then study the smaller sections and check how it all comes together.
 
Gentleman,

thank you very much for your answers. If I understand correctly, there is not a tool that would allow setting breakpoints, step through the script, etc, like, e.g., gdb. However, since my attempt is just an implementation on installing FreeBSD without installer inspired by ShelLuser's How-To, the use of a shell script was mandatory.

However, the lack of proper tools begs a question, whether I should not invest time in learning a different language. My goal is not to become a code writer, just to be able to write some utilities. A typical example would be to walk through a directory, calculate for each found file a check-sum, compare the check-sum with a previously calculated check-sum, and take an action based on the resuly of the comparison. As this is a different topic, I opened a new thread for it.

Kindest regards,

M
 
I actually had a need to step through a shell script, working on it right now actually. There are some various tools for bash like bashdb (http://bashdb.sourceforge.net/). I could only find tools for bash and I need to debug a sh script. I ended up putting a read command between each line to step through it. Starting the script with sh -v prints each command line, but you only see the line after it executes. Not prefect but works well enough to step through.
 
Hi CraigHB,

Code:
sh -v
was, how I started. However, the script was moving forward, and I could not quite remember the errors. Than I discovered the
Code:
sh -n
, which stops at the error.

Kindest regards,

M
 
Back
Top