調試 shell script 方法

來源:互聯網
上載者:User

標籤:style   blog   color   for   ar   div   代碼   log   

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

看每一行代碼的執行:

[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

進一步,看具體執行的代碼與行號對應:

[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

指定指令碼的部分去跑詳細的行號,其餘的忽略:

[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‘

運行輸出:

[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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.