How to debug shell script?

不言
Release: 2019-03-20 13:43:18
Original
5127 people have browsed it

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.

How to debug shell script?

Debug the shell script by adding the command:

$ cat checkdebug.sh
Copy after login
#!/bin/bash

set -xv  #<< This will enable debugcd /var/log/
for i in "*.log"; do
 du -sh $i
done
Copy after login

Execute the above script and observe the output:

#sh checkdebug.sh
Copy after login

Output:

cd /var/log/
+ cd /var/log/
for i in "*.log"; do
 du -sh $i
done
+ for i in &#39;"*.log"&#39;
+ 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
Copy after login

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
Copy after login

The execution is as follows

# sh -xv checkdebug2.sh
Copy after login

Output:

#!/bin/bash
cd /var/log/
+ cd /var/log/
for i in "*.log"; do
 du -sh $i
done
+ for i in &#39;"*.log"&#39;
+ 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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template