Debug shell script

Source: Internet
Author: User
[email protected]:~$ cat b.sh
#!/bin/bashdir=`pwd`dir=$dir‘/‘for f in `ls *.png`do echo $dir$fdone

Check the execution of each line of code:

[email protected]:~$ bash -x b.sh++ pwd+ dir=/home/wade+ dir=/home/wade/++ ls chrome_1407299385726.png chrome_1427299385726.png+ for f in ‘`ls *.png`‘+ echo /home/wade/chrome_1407299385726.png/home/wade/chrome_1407299385726.png+ for f in ‘`ls *.png`‘+ echo /home/wade/chrome_1427299385726.png

Further, we can see that the Code executed corresponds to the row number:

[email protected]:~$ export PS4=‘+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: ‘[email protected]:~$ bash -x b.sh++b.sh:2:: pwd+b.sh:2:: dir=/home/wade+b.sh:3:: dir=/home/wade/++b.sh:8:: ls chrome_1407299385726.png chrome_1427299385726.png+b.sh:4:: for f in ‘`ls *.png`‘+b.sh:7:: echo /home/wade/chrome_1407299385726.png/home/wade/chrome_1407299385726.png+b.sh:4:: for f in ‘`ls *.png`‘+b.sh:7:: echo /home/wade/chrome_1427299385726.png

Specify the part of the script to run the detailed line number, and ignore the rest:

[email protected]:~$ cat b.sh #!/bin/bashdir=`pwd`dir=$dir‘/‘#this line above won‘t log out set -xfor f in `ls *.png`do  echo $dir$fdoneset +x#the follow line won‘t log out echo ‘end‘abc=‘new var‘

Running output:

[email protected]:~$ ./b.sh++./b.sh:10:: ls chrome_1407299385726.png chrome_1427299385726.png+./b.sh:6:: for f in ‘`ls *.png`‘+./b.sh:9:: echo /home/wade/chrome_1407299385726.png/home/wade/chrome_1407299385726.png+./b.sh:6:: for f in ‘`ls *.png`‘+./b.sh:9:: echo /home/wade/chrome_1427299385726.png/home/wade/chrome_1427299385726.png+./b.sh:11:: set +xend

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.