I see that similar questions were answered before but my scenario is a little bit different:
- I need to store a command in a variable in order to I can log it before running
- the command contains variables that can contain space between the words
- the result of the execution must assign to a new variable
This is the command that works properly from the terminal:
java -jar ${ORACLE_HOME}/bin/sql-command-line-tool/sql-runner-0.2.2-with-dependencies.jar -v -U ${DB_USERNAME} -P ${DB_PASSWORD} -j jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}/${DB_NAME} "select 1 from dual"
Variables:
- DB_HOST=oracle-db
- DB_PORT=1521
- DB_NAME=ORCLPDB1.localdomain
- DB_USERNAME="SYS as SYSDBA"
- DB_PASSWORD=Oradoc_db1
Test:
$ echo $DB_USERNAME
"SYS as SYSDBA"
This is how I want to use but this does not work:
echo "checking whether database server is up and running..."
_COMMAND="java -jar ${ORACLE_HOME}/bin/sql-command-line-tool/sql-runner-0.2.2-with-dependencies.jar -v -U ${DB_USERNAME} -P ${DB_PASSWORD} -j jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}/${DB_NAME} \"select 1 from dual\""
echo "command: $_COMMAND"
_RESULT=$($_COMMAND)
_SHOW_MESSAGE=true
while [[ ${_RESULT} ]]
do
if [ $_SHOW_MESSAGE = "true" ]; then echo "the database server is not running yet, waiting..."; fi
if [ $VERBOSE = "false" ]; then _SHOW_MESSAGE="false"; fi
sleep 0.5
_JSON=$($_COMMAND)
done
echo "database server is up and running"
Error:
Unmatched arguments from index 4: 'SYSDBA"', '"select', '1', 'from', 'dual"'
The jar I use is available from here.
I have tried to use single and double quotes in different ways, tried to use eval
but non of them worked properly.
What I missed?