Home > Article > Operation and Maintenance > How to debug shell script?
How to debug Shell script? We can debug the shell script using the "set-xv" command in the shell script or using -xv on the command line while executing the script.
Debug the shell script by adding the command:
$ cat checkdebug.sh
#!/bin/bash set -xv #<< This will enable debugcd /var/log/ for i in "*.log"; do du -sh $i done
Execute the above script and observe the output:
#sh checkdebug.sh
Output:
cd /var/log/ + cd /var/log/ for i in "*.log"; do du -sh $i done + for i in '"*.log"' + du -sh boot.log mysqld.log post111.log post1121.log yum.log 0 boot.log 32K mysqld.log 0 post111.log 0 post1121.log 4.0K yum.log
Debug shell script using option:
With this option, we don’t need to add “set-xv” in the shell script. Just create a shell script as shown below.
$ cat checkdebug2.sh #!/bin/bash cd /var/log/ for i in "*.log"; do du -sh $i done
The execution is as follows
# sh -xv checkdebug2.sh
Output:
#!/bin/bash cd /var/log/ + cd /var/log/ for i in "*.log"; do du -sh $i done + for i in '"*.log"' + du -sh boot.log mysqld.log post111.log post1121.log yum.log 0 boot.log 32K mysqld.log 0 post111.log 0 post1121.log 4.0K yum.log
This article is all over here. For more other exciting content, you can pay attention to the PHP Chinese website Linux tutorial video column!
The above is the detailed content of How to debug shell script?. For more information, please follow other related articles on the PHP Chinese website!