2

I'm trying to create a bash script that converts a bunch of pdfs into text in order to extract some information but the shell gives me this error:

./AutoBib.sh: line 8: syntax error near unexpected token `pdftotext'
./AutoBib.sh: line 8: `    pdftotext $1 temp.txt'

Here there is an example of my function:

function doi{

    pdftotext $1 temp.txt
    cat temp.txt | grep doi: | cut -d: -f 2 | head -n 1 >> dois.txt
    rm -rf temp.txt
}
doi $PDF

Where the variable PDF is taken in input. Before adding the function it worked, I used to write in my script:

pdftotext $PDF tempo.txt
codeforester
  • 39,467
  • 16
  • 112
  • 140
Giuseppe Minardi
  • 411
  • 4
  • 16

2 Answers2

5

From Bash manual:

The braces are reserved words, so they must be separated from the list by blanks or other shell metacharacters.

function ... is an outdated syntax for defining Bash functions. Use this instead:

doi() {
   ...
}

Since () are meta characters, you don't need a space in this case (though spaces make your code prettier):

doi(){
  ...
}

Extending this a little, remember that we need a whitespace (space, tab, or newline) after { and before `}' in command grouping, like this:

{ command1; command2; ... }
codeforester
  • 39,467
  • 16
  • 112
  • 140
2

You need a space after the name of your function and before the {:

function doi {
            ^
damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • 2
    Right. (Well, strictly speaking, you don't. Bash lets you define a function named `doi{` -- but then you'd need a `{` token after the function name.) The issue is that `doi{` is a single token, not an identifier `doi` followed by a `{` token. – Keith Thompson Nov 14 '18 at 23:14