How do i debug bash shell scripts?
-
There are a few options that can help debug shell scripts.
-v Print shell input lines as they are read. -x After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list. -n Read commands but do not execute them. This may be used to check a shell script for syntax errors. This is ignored by interactive shells.
To syntax check a script use the -n option. Introduce a syntax error by removing the double quotes in any shell script you have, then run this, replacing the command with your script name.
$ bash -n ./myfirst.sh ./myfirst.sh: line 4: unexpected EOF while looking for matching `"' ./myfirst.sh: line 6: syntax error: unexpected end of file
The -x and -v options. You can see the -v option does not expand the variable in the echo command, because it just prints lines whereas the -x option expands the variables.
$ bash -v ./myfirst.sh #!/bin/bash Myvar=pineapple eport Myvar ./myfirst.sh: line 3: eport: command not found echo "My favourite fruit is $Myvar" My favourite fruit is pineapple ./mysecond.sh But Barney does not like
$ bash -x ./myfirst.sh + Myvar=pineapple + eport Myvar ./myfirst.sh: line 3: eport: command not found + echo 'My favourite fruit is pineapple' My favourite fruit is pineapple + ./mysecond.sh But Barney does not like
© Lightnetics 2024