0

I have defined a variable as list where i want to add the result derived from sql. My shell script looks like the below-

list=""


setLiveTables()
{
  liveTablesList=`"SELECT CalculatorName FROM TableList"`
  if echo "$liveTablesList"; then
    echo "${liveTablesList}" | while read line
    do
       trimLine=`echo $line | sed 's/ *$//g'`
       listCalc+=""${trimLine}","
    done
    list=$listCalc
}

doSomething() 
{
  setLiveTables
  # Do some curl call with data $list
  # Here list appears empty
}

doSomething

in doSomething function i get value of list always empty wven whe i get response from sql. If i Log listCalc inside setLiveTables, i do get the data in it.

Aditya Sethi
  • 10,486
  • 11
  • 26
  • 31
  • 1
    That `liveTablesList=` line tries to run `"SELECT CalculatorName FROM TableList"` as a shell command; I'm pretty sure you want to run that as an SQL command. You should get an error message about this when it runs. Also, `if echo "$liveTablesList"` doesn't really make sense; it prints the variable (which is probably empty, since it was loaded from an invalid command), and if that prints successfully (it will) it runs the `then` clause. I'm pretty sure this is not what you had in mind. – Gordon Davisson Jan 27 '23 at 09:09
  • Assume that is just for context. my sql is executing and i am getting the desired result. In logs i can see data. – Aditya Sethi Jan 27 '23 at 09:18
  • 2
    It's going to be hard to figure out the problem when the code you post has different problems than your actual code. Please include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) in your question, that someone else can run and see the *same* problem you're seeing, not different problems. Also, as a general troubleshooting technique, try putting `set -x` before the problem section to get an execution trace and see what bash thinks is happening as it runs. – Gordon Davisson Jan 27 '23 at 09:24
  • Look at https://stackoverflow.com/questions/16854280/a-variable-modified-inside-a-while-loop-is-not-remembered, variable modifications inside `while` are picky. – Nic3500 Jan 27 '23 at 19:08
  • [Shellcheck](https://www.shellcheck.net/) finds multiple problems with the code in the question, including the problem (variable modified in subshell) that causes `list` to always be empty. Running [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code saves a lot of time. – pjh Jan 27 '23 at 19:45

1 Answers1

0

There are two possible solutions:

  1. Disable lastpipe
shopt -u lastpipe # <========= SEE HERE

list=""


setLiveTables()
{
  liveTablesList=`"SELECT CalculatorName FROM TableList"`
  if echo "$liveTablesList"; then
    echo "${liveTablesList}" | while read line
    do
       trimLine=`echo $line | sed 's/ *$//g'`
       listCalc+=""${trimLine}","
    done
    list=$listCalc
}

doSomething() 
{
  setLiveTables
  # Do some curl call with data $list
  # Here list appears empty
}

doSomething
  1. Remove the pipe
list=""


setLiveTables()
{
  liveTablesList=`"SELECT CalculatorName FROM TableList"`
  if echo "$liveTablesList"; then
    while read line # <==== SEE HERE
    do
       trimLine=`echo $line | sed 's/ *$//g'`
       listCalc+=""${trimLine}","
    done <<< "${liveTablesList}" # <==== SEE HERE
    list=$listCalc
}

doSomething() 
{
  setLiveTables
  # Do some curl call with data $list
  # Here list appears empty
}

doSomething

As others have mentioned, use ShellCheck to catch these sorts of errors earlier

hyperupcall
  • 869
  • 10
  • 21