erro de script bash

erro de script bash

bash-5.0-6 tentar executar o seguinte script resulta em erro de sintaxe:

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

a mensagem de erro resultante:

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

parece que o sinal "%" causa o erro. Mas como posso escrever o script para que seja utilizável?

desde já obrigado, tchau, Hans

PS Meu código completo é:

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\!"

Responder1

O código contém erros de cotação. Considere esta linha:

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

O comando tr é tr '"' '\n’. A aspa final nesse comando é uma crase unicode (não ASCII). Isso não é válido como aspas simples do shell.

Substituir tr '"' '\n’com tr '"' '\n'.

Depois de fazer isso, recorte e cole seu código em shellcheck.net e corrija quaisquer erros restantes (importantes) ou avisos (podem ser importantes) que identificar.

Responder2

Emitir o mesmo comando sozinho funciona bem. No entanto, como você tem $o URL, ele pode ser contado como uma variável, já que você está usando " try:

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

observe as 'aspas simples. a menos que $buchseja uma variável em seu script bash.

informação relacionada