1

When type in echo $(db2 -x "select count(*) from dict_area") in bash, I get 446.

But when I type in echo $(db2 -x "select count(*) from dict_area";db2 -x "select count(*) from dict_area"), I get

SQL1024N A database connection does not exist. SQLSTATE=08003 SQL1024N A database connection does not exist. SQLSTATE=08003

This has nothing to do with my work. I just want to know how does that happen. Can you help me?

RSFalcon7
  • 4,367
  • 6
  • 30
  • 56
dahui
  • 11
  • 5

1 Answers1

1

The execution in a $() invokes a subshell, and the new subshell could not use the connection in the current shell. That's why you get these errors.

If you want to execute thing in subshell, establishes the connection each time. OR execute them in the current one by writing the output to a file and then processing the output

echo $(db2 connect to sample > /dev/null; db2 -x "select count(*) from dict_area")

OR

db2 connect to sample
db2 -x "select count(*) from dict_area" > /tmp/output
echo $(cat output)
AngocA
  • 295
  • 1
  • 10
  • Thanks for your answer. I know what you mean. But that can not explain `echo $(db2 -x "select count(*) from dict_area")`. And when I type in `echo $(db2 -x "select count(*),$BASH_SUBSHELL from dict_area")`, I get `446 1` while I get ` 446 0` when typing only `db2 -x "select count(*),$BASH_SUBSHELL from dict_area"`. – dahui Dec 23 '14 at 06:55