0

bash-5.0-6 trying to run the following script results in syntax-error:

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

the resulting error-message:

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

it seems, that the sign "%" causes the error. But how can I write the script, so that it is useable ?

thanks in advance bye hans

P.S. My complete code is:

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\!"
5
  • I just tried running wget -nv -O index.html "https://de.wikibooks.org/w/index.php?title=Spezial%3APrefixindex&namespace=0&from=$buch" on my machine with GNU bash, version 4.4.20(1)-release and it worked fine. The unterminated quotation must be elsewhere in your get_wikibooks.sh file. Aug 24, 2020 at 19:08
  • 1
    What is the exact content of line 18 of get_wikibooks.sh?
    – John1024
    Aug 24, 2020 at 19:11
  • I'm using bash 4.4.20 and that line downloads an index.html file without an error message. My $buch variable is obviously empty. Could the value of that variable be where the problem is in your case? Aug 24, 2020 at 19:12
  • With our without the $buch in your line, there should not be such an error. Try adding set -x to get a debug output of what is executed. And please add the script you execute around line 18
    – Ron
    Aug 24, 2020 at 19:29
  • 1
    The error message says you have an "Unterminated quoted string". That is not caused by a % sign. You need to show us line 18 which, it would seem, is after the wget command.
    – John1024
    Aug 24, 2020 at 20:16

2 Answers 2

1

The code has quoting errors. Consider this line:

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

The tr command is tr '"' '\n’. The final quote in that command is a unicode (not ASCII) backquote. This is not valid as a shell-single-quote.

Replace tr '"' '\n’ with tr '"' '\n'.

After you have done that, cut and paste your code into shellcheck.net and correct any remaining errors (important) or warnings (might be important) that it identifies.

0

Issuing the same command on it's own works fine. However since you have $ within the URL, it might be counted a as variable since you re using " try:

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

note the ' single quotes.. unless $buch is a variable within your bash script.

1
  • 2
    I'm pretty sure $buch IS a variable, but if it is NOT a variable, then you can put set -u at the top of the bash script and you will get an error if a variable is referenced before being set. (That way you'll know if bash is trying to resolve $buch as a variable when it shouldn't be.) Aug 24, 2020 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .