-2

Good day.

I want to automatize certain process with linux and bash. And I stuck in a IF statement.

This is the code:

#!/bin/bash
result=$(
 sqlplus -s /nolog << EOF
 CONNECT admin/password@server;

 whenever sqlerror exit sql.sqlcode;
 set echo off
 set heading off
 @processQuery.sql
 exit;
 EOF
)
echo $result
if [ "$result" != "no rows selected" ]
then echo "Please, clean up"
else echo "All Ok"
fi

And the output is:

no rows selected
Please, clean up

Thanks so much for the help.

Update: add

result2=${result:1}

and

if [ "$result2" != "no rows selected" ]

Thank you so much and specialy to @mercury0114 for the answer!

EnzoV
  • 5
  • 4

1 Answers1

0

Are you sure your result variable does not contain spaces or something else? Because for me the following code:

#!/bin/bash
result="no rows selected"
echo $result
if [ "$result" != "no rows selected" ]
then echo "Please, clean up"
else echo "All Ok"
fi

Prints

no rows selected
All Ok

I would double check what the SQL query returns.

Also, consider replacing "$result" by just plain result:

if [ result != "no rows selected" ]
mercury0114
  • 1,341
  • 2
  • 15
  • 29
  • I try with `echo "."$result"."` and I get: `. no rows selected.` I put `" no rows selected"` with the same result. using `result` instend of `$result` no change. Thanks. – EnzoV Jun 28 '20 at 12:08
  • @EnriqueLorenzoVillarMavila so why the space betwen `.` and `n`? Shouldn't it be `.no rows selected.`? – mercury0114 Jun 28 '20 at 12:18
  • Yes. I don't know why appears that space, but no changes in IF statement with that. Maybe I need convert the output of SQLPLUS but I don't know how. – EnzoV Jun 28 '20 at 12:33
  • well to remove the first space do "${result:1}" – mercury0114 Jun 28 '20 at 13:12