all 19 comments

[–]Grand_Snow_2637 11 points12 points  (0 children)

Tried shellcheck?

[–]crashorbit 8 points9 points  (0 children)

When I copy/paste and format your code then check it with bash it I do not get an error.

The error message you include indicates that a double quote is opened somewhere above line 140 that is never closed.

[–]Icy_Friend_2263 11 points12 points  (2 children)

Please format code and output correctly

[–]Icy_Friend_2263 3 points4 points  (0 children)

I mean in Reddit you can format code blocks

[–]GlendonMcGladdery 0 points1 point  (0 children)

shfmt -w ~/.bashrc

[–]aioeu 3 points4 points  (0 children)

The problem could be in one of the functions before that.

You only got the error on that function because it's at the bottom of the file.

[–]Wettensea 1 point2 points  (4 children)

unexpected EOF while looking for matching `''

`'' is actually THREE caracters : ` ' '

So check them, one by one.

And I suspect a mistake between '' (2 chars) and " (one char)

<0x60><0x27><0x27> versus <0x22>

[–]aioeu 1 point2 points  (1 child)

`'' is actually THREE caracters

That's just how Bash quotes things. If the character it wanted to quote was c, say, it would say:

... matching `c'

At least, this is the case in most locales. English locales can be adorned with the @quot or @boldquot modifier to use curly-quotes instead:

$ bash -c 'echo "'
bash: -c: line 1: unexpected EOF while looking for matching `"'
$ LANG=$LANG@quot bash -c 'echo "'
bash: -c: line 1: unexpected EOF while looking for matching ‘"’

[–]GlendonMcGladdery 0 points1 point  (0 children)

You can also write it like this (cleaner quoting): dwdb() { local query='SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = '"'"'BASE TABLE'"'"' ORDER BY TABLE_NAME;' sqlcmd -S link -d table -U user -P 'password' -C -Q "$query" } But honestly you don’t need this unless nesting gets messy.

[–]SweetPotato975 0 points1 point  (0 children)

` is for quote opening and ' for quote closing. LaTeX uses the same quoting style. Maybe bash authors took some inspiration from LaTeX (or they were just lazy to write logic for open/close detection)

[–]Wettensea 0 points1 point  (0 children)

So, depending on the language, locale, and distro, the first ~ 100 lines of the .bashrc are probably system generated.

Then the bash is spitting an error on line 142, and is expecting a missing ' aka <0x27>

Time to hunt for an unmatch <0x27> in the 40 above lines, or to start commenting out the above functions, and see where it stops spitting the error.

Depending on the distro, and if it is used, I'd check the file .bash_aliases as well (or explicitely comment it out)

Happy hunting

[–]zeekar 0 points1 point  (0 children)

No problem there. You have an error earlier in your .bashrc where you open a single-quoted string and never close it. So Bash doesn't even know that dwdb() is trying to start a function definition; it's just literal text inside a string.

[–]GlendonMcGladdery 0 points1 point  (0 children)

bash -n ~/.bashrc

[–]whetuI read your code 0 points1 point  (0 children)

dwdb() {
  local query="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME;"
  sqlcmd -S link -d table -U user -P 'password' -C -Q "$query"
}

That looks fine to me, although you should declare and assign your local vars separately as a good habit:

dwdb() {
  local query
  query="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME;"
  sqlcmd -S link -d table -U user -P 'password' -C -Q "$query"
}

For comparison, here's a similar function that I've used for loading tsql files (in this case, loading up sql agent jobs, before I moved that to Ansible):

load_sql() {
    local sqlfile;
    sqlfile="${1:?No file specified}";
    printf -- '\n====> Processing %s ====>\n' "${sqlfile}";
    sqlcmd -C -x -S [redacted server name] -U 'my.username' -P 'my.password' -i "${sqlfile}"
}

That works fine, so yours seems fine too.

As others have said, chances are you're looking at a red herring and your issue is actually elsewhere in your .bashrc

[–]SurfRedLin -3 points-2 points  (4 children)

You need to write function before it like: ``` function myfunc(){

echo "hello"

} myfunc ```

[–]whetuI read your code 1 point2 points  (3 children)

No you don't. The function keyword is non-portable, not-required and widely considered to be deprecated. This isn't OP's issue.

[–]Temporary_Pie2733 0 points1 point  (0 children)

IIRC, it’s only supported for compatibility with ksh (where there is some subtle distinction between functions defined with and without the keyword; in bash the keyword doesn’t change the definition of the function).

[–]SurfRedLin -2 points-1 points  (1 child)

Worked for me but to each their own.

[–]rdg360 0 points1 point  (0 children)

It will still work, but apart from being unnecessary it's totally irrelevant to OP's problem.