0
​​​​​defineColumns() {
​​​​​​​    shift
    local dirfun=${​​​​​​​​1:-"/var/log/was/dial"}​​​​​​​​
    local basefun=${​​​​​​​​2:-"$logdir/party_info.$(date +%y%m%d%H%M%S)"}​​​​​​​​
    touch $logbase_bcdb
    info $dirfun $basefun
    # Run steps sequentially
    loadData
}

I am writing a shell script code in which I have written multiple functions. The above function is throwing error as : syntax error near unexpected token `{​ What is wrong in the code?

Aviator
  • 543
  • 1
  • 4
  • 10
  • 2
    Check your code for non-printable characters: `cat -A file` – Cyrus Jul 11 '21 at 18:08
  • I didnt get you! Could you please elaborate? – Aviator Jul 11 '21 at 18:13
  • 1
    @Aviator At least as posted here, your code has a bunch of zero-width space characters. These are not normally visible, but they mess up the shell syntax. Personally, I recommend using `LC_ALL=C cat -vet filename` to make all the normally-invisible stuff in a file visible -- normally, the only change will be a `$` at the end of each line (indicating the linefeed character marking the end of line), but in this case I see `M-bM-^@M-^K` (which is a representation of the UTF-8 code for zero-width space) all over the place. – Gordon Davisson Jul 11 '21 at 18:29
  • I see a lot of M-bM-^@M-^K but how to remove them ? – Aviator Jul 11 '21 at 18:40
  • https://stackoverflow.com/a/43108392/8577085 To remove all non printable characters from file – abhishek phukan Jul 12 '21 at 08:43

1 Answers1

3

There are non-printing characters in the code: Unicode U+200B ZERO WIDTH SPACE. Remove them and you should be fine.

Firstly to see them, you could use cat -A but it shows these characters as M-bM-^@M-^K, which is confusing IMO. I'd rather read the Python ascii() representation, so here's a quick script:

import fileinput

for line in fileinput.input():
    print(ascii(line))

Save that as ascii_lines.py then run with the name of your script:

$ python3 ascii_lines.py filename.sh
'\u200b\u200b\u200b\u200b\u200bdefineColumns() {\n'
'\u200b\u200b\u200b\u200b\u200b\u200b\u200b    shift\n'
'    local dirfun=${\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b1:-"/var/log/was/dial"}\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\n'
'    local basefun=${\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b2:-"$logdir/party_info.$(date +%y%m%d%H%M%S)"}\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\n'
'    touch $logbase_bcdb\n'
'    info $dirfun $basefun\n'
'    # Run steps sequentially\n'
'    loadData\n'
'}\n'

Then to remove them, you could use sed, though it doesn't know Unicode, so I'm using a Bash $'' string here to solve that.

$ sed -i $'s/\u200b//g' filename.sh

Afterwards:

$ python3 ascii_lines.py filename
'defineColumns() {\n'
'    shift\n'
'    local dirfun=${1:-"/var/log/was/dial"}\n'
'    local basefun=${2:-"$logdir/party_info.$(date +%y%m%d%H%M%S)"}\n'
'    touch $logbase_bcdb\n'
'    info $dirfun $basefun\n'
'    # Run steps sequentially\n'
'    loadData\n'
'}\n'
wjandrea
  • 28,235
  • 9
  • 60
  • 81