bash腳本錯誤

bash腳本錯誤

bash-5.0-6 嘗試執行以下腳本會導致語法錯誤:

wget -nv -O index.html "https://de.wikibooks.org/w/index.php?title=Spezial%3APrefixindex&namespace=0&from=$buch"

產生的錯誤訊息:

2020-08-24 20:38:51 URL:https://de.wikibooks.org/w/index.php?title=Spezial:Pr%C3%A4fixindex&namespace=0&from=Mathematik%3A_Lineare_Algebra [96400] -> "index.html" [1]
get_wikibooks.sh: 18: Syntax error: Unterminated quoted string

看來,符號「%」導致了錯誤。但是我該如何編寫腳本才能使其可用?

預先感謝再見漢斯

PS我的完整程式碼是:

set -x
#!/bin/bash
buch=$(zenity --entry --title "Download eines Wikibooks" --text "Bitte geben Sie den Buchtitel an:")
buch=$(echo $buch | sed "s/ /_/g")
mkdir -p ~/wikibooks/$buch
cd ~/wikibooks/$buch
wget -nv -O index.html 'https://de.wikibooks.org/w/index.php?title=Spezial%3APrefixindex&namespace=0&from=$buch'
if [ $? -ne 0 ] ; then
        zenity --error --title "Download eines Wikibooks - Fehler" \
          --text "Ein Fehler ist aufgetreten\! \nÜberprüfen sie die Internet-Verbindung und den Buchtitel."
fi
wget -nv -c $(cat index.html | tr '"' '\n’ | egrep "^/wiki$buch" | sort -u | sed "s#^#https://de.wikibooks.org#")
if [ $? -ne 0 ] ; then
        zenity --error --title "Download eines Wikibooks - Fehler" \
          --text "Ein Fehler ist aufgetreten\! \nÜberprüfen sie die Internet-Verbindung und den Buchtitel."
fi
for i in $(ls); do sed "s#href=\"/wiki/$buch#href=\"./$buch#g" $i > $i.temp; mv $i.temp $i; done
zenity --info --title "Download eines Wikibooks" --text "Herunterladen des Buches erfolgreich\!"

答案1

該程式碼存在引用錯誤。考慮這一行:

wget -nv -c $(cat index.html | tr '"' '\n’ | egrep "^/wiki$buch" | sort -u | sed "s#^#https://de.wikibooks.org#")

tr 指令是tr '"' '\n’.該指令中的最後一個引號是 unicode(不是 ASCII)反引號。這作為 shell 單引號無效。

用。tr '"' '\n’tr '"' '\n'

完成後,將程式碼剪下並貼上到 shellcheck.net 並更正其識別的任何剩餘錯誤(重要)或警告(可能重要)。

答案2

單獨發出相同的命令效果很好。但是,由於您$在 URL 中包含它,因此它可能會被視為一個變量,因為您正在使用" try:

wget -nv -O index.html 'https://de.wikibooks.org/w/index.php?title=Spezial%3APrefixindex&namespace=0&from=$buch'

請注意'單引號.. except$buch是 bash 腳本中的變數。

相關內容