自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Shell腳本中執(zhí)行語法檢查調(diào)試模式

系統(tǒng) Linux 系統(tǒng)運維
我們開啟了 Shell 腳本調(diào)試系列文章,先是解釋了不同的調(diào)試選項,下面介紹如何啟用 Shell 調(diào)試模式。寫完腳本后,建議在運行腳本之前先檢查腳本中的語法,而不是查看它們的輸出以確認它們是否正常工作。在本系列的這一部分,我們將了解如何使用語法檢查調(diào)試模式。

我們開啟了 Shell 腳本調(diào)試系列文章,先是解釋了不同的調(diào)試選項,下面介紹如何啟用 Shell 調(diào)試模式。

[[179288]]

寫完腳本后,建議在運行腳本之前先檢查腳本中的語法,而不是查看它們的輸出以確認它們是否正常工作。

在本系列的這一部分,我們將了解如何使用語法檢查調(diào)試模式。記住我們之前在本系列的***部分中解釋了不同的調(diào)試選項,在這里,我們將使用它們來執(zhí)行腳本調(diào)試。

啟用 verbose 調(diào)試模式

在進入本指導(dǎo)的重點之前,讓我們簡要地探索下 verbose 模式。它可以用 -v 調(diào)試選項來啟用,它會告訴 shell 在讀取時顯示每行。

要展示這個如何工作,下面是一個示例腳本來批量將 PNG 圖片轉(zhuǎn)換成 JPG 格式。

將下面內(nèi)容輸入(或者復(fù)制粘貼)到一個文件中。

  1. #!/bin/bash 
  2. #convert 
  3. for image in *.png; do 
  4. convert  "$image"  "${image%.png}.jpg" 
  5. echo "image $image converted to ${image%.png}.jpg" 
  6. done 
  7. exit 0 

接著保存文件,并用下面的命令使腳本可執(zhí)行:

  1. $ chmod +x script.sh 

我們可以執(zhí)行腳本并顯示它被 Shell 讀取到的每一行:

  1. $ bash -v script.sh  

 

 

 

顯示shell腳本中的所有行

在 Shell 腳本中啟用語法檢查調(diào)試模式

回到我們主題的重點,-n 激活語法檢查模式。它會讓 shell 讀取所有的命令,但是不會執(zhí)行它們,它(shell)只會檢查語法。

一旦 shell 腳本中發(fā)現(xiàn)有錯誤,shell 會在終端中輸出錯誤,不然就不會顯示任何東西。

激活語法檢查的命令如下:

  1. $ bash -n script.sh 

因為腳本中的語法是正確的,上面的命令不會顯示任何東西。所以,讓我們嘗試刪除結(jié)束 for 循環(huán)的 done 來看下是否會顯示錯誤:

下面是修改過的含有 bug 的批量將 png 圖片轉(zhuǎn)換成 jpg 格式的腳本。

  1. #!/bin/bash 
  2. #script with a bug 
  3. #convert 
  4. for image in *.png; do 
  5. convert  "$image"  "${image%.png}.jpg" 
  6. echo "image $image converted to ${image%.png}.jpg" 
  7. exit 0 

保存文件,接著運行該腳本并執(zhí)行語法檢查:

  1. $ bash -n script.sh  

 

 

 

檢查 shell 腳本語法

從上面的輸出中,我們看到我們的腳本中有一個錯誤,for 循環(huán)缺少了一個結(jié)束的 done 關(guān)鍵字。shell 腳本從頭到尾檢查文件,一旦沒有找到它(done),shell 會打印出一個語法錯誤:

  1. script.sh: line 11: syntax error: unexpected end of file 

我們可以同時結(jié)合 verbose 模式和語法檢查模式:

  1. $ bash -vn script.sh  

 

 

 

在腳本中同時啟用 verbose 檢查和語法檢查

另外,我們可以通過修改腳本的首行來啟用腳本檢查,如下面的例子:

  1. #!/bin/bash -n 
  2. #altering the first line of a script to enable syntax checking 
  3. #convert 
  4. for image in *.png; do 
  5. convert  "$image"  "${image%.png}.jpg" 
  6. echo "image $image converted to ${image%.png}.jpg" 
  7. exit 0 

如上所示,保存文件并在運行中檢查語法:

  1. $ ./script.sh 
  2. script.sh: line 12: syntax error: unexpected end of file 

此外,我們可以用內(nèi)置的 set 命令來在腳本中啟用調(diào)試模式。

下面的例子中,我們只檢查腳本中的 for 循環(huán)語法。

  1. #!/bin/bash 
  2. #using set shell built-in command to enable debugging 
  3. #convert 
  4. #enable debugging 
  5. set -n 
  6. for image in *.png; do 
  7. convert  "$image"  "${image%.png}.jpg" 
  8. echo "image $image converted to ${image%.png}.jpg" 
  9. #disable debugging 
  10. set +n 
  11. exit 0 

再一次保存并執(zhí)行腳本:

  1. $ ./script.sh  

總的來說,我們應(yīng)該保證在執(zhí)行 Shell 腳本之前先檢查腳本語法以捕捉錯誤。

請在下面的反饋欄中,給我們發(fā)送關(guān)于這篇指導(dǎo)的任何問題或反饋。在這個系列的第三部分中,我們會解釋并使用 shell 追蹤調(diào)試模式。

作者簡介: 

 

 

[[179289]]

Aaron Kili 是一個 Linux 及 F.O.S.S 熱衷者,即將是 Linux 系統(tǒng)管理員、web 開發(fā)者,目前是 TecMint 的內(nèi)容創(chuàng)作者,他喜歡用電腦工作,并熱心分享知識。

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2016-12-16 09:23:29

LinuxShell腳本

2017-01-18 20:38:36

LinuxShell腳本命令

2018-02-01 17:32:30

LinuxUNIXBash Shell

2023-04-04 07:52:26

RedisLua腳本

2021-04-21 08:03:34

腳本Shell讀取

2021-08-20 10:46:25

Shell腳本文件Linux

2021-01-18 17:23:30

代碼調(diào)試VS Code

2009-12-01 09:13:51

shell腳本linux

2021-08-30 07:50:42

腳本語言命令行

2021-02-15 17:29:46

LinuxShell腳本

2022-11-03 08:13:52

echo 命令Linux

2020-12-14 06:57:37

shell

2023-07-10 12:11:50

TypeScripChrome識別

2022-11-01 15:38:22

LinuxShell

2022-10-28 16:42:04

Linuxcrontab日志

2021-01-12 10:10:41

shell腳本Linux命令

2021-01-08 08:06:19

腳本Shell文件

2022-10-09 10:18:44

LinuxShell腳本

2022-12-22 20:47:01

腳本循環(huán)結(jié)構(gòu)

2021-03-14 09:28:24

Linux Shell腳本
點贊
收藏

51CTO技術(shù)棧公眾號