Home > Software engineering >  shell's command substitution not work : isSuc=$(db2 "update table_x set x=123"|grep s
shell's command substitution not work : isSuc=$(db2 "update table_x set x=123"|grep s

Time:02-25

This command can be executed successfully in my db2 server:

~]# db2 "update table_x set x=xxx"|grep successfully

With output:

~]# DB20000I  The SQL command completed successfully

but if I execute this command:

~]# isSuc=$(db2 "update table_x set x=123"|grep successfully)

or:

~]# echo $(db2 "update table_x set x=123"|grep successfully)

isSuc does not get a value, and the echo outputs nothing. Why is this?

CodePudding user response:

Suggesting to try this:

 echo $( { db2 "update table_x set x=123" |grep successfully; } 2>&1 )

CodePudding user response:

.../> echo $(db2 "update table_x set x=123")
DB20000I The SQL command completed successfully.

.../> echo $(db2 "update table_x set x=123"|grep DB)
DB21034E The command was processed as an SQL statement because it was not a

.../> echo $(db2 "update table_x set x=123"|cat)
DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL1024N A database connection does not exist. SQLSTATE=08003

.../> echo $(db2 connect to db_name &>/dev/null;db2 "update table_x set x=123"|grep successfully;)
DB20000I The SQL command completed successfully.
  • Related