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?

5
  • 2
    Check your code for non-printable characters: cat -A file Commented Jul 11, 2021 at 18:08
  • I didnt get you! Could you please elaborate? Commented Jul 11, 2021 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. Commented Jul 11, 2021 at 18:29
  • I see a lot of M-bM-^@M-^K but how to remove them ? Commented Jul 11, 2021 at 18:40
  • stackoverflow.com/a/43108392/8577085 To remove all non printable characters from file Commented Jul 12, 2021 at 8:43

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.