0

I am trying to store SQL query result into an array in a shell script but I face an invalid identifier error when I run my .sh

Could you please check what matter in my code?

#!/usr/bin/ksh

echo Start Executing SQL commands

array=$(sqlplus -s apps/apps << eof
SET PAGESIZE 0;
SELECT directory_name from all_directories where directory_name like '%XXBP%';
eof)


printf '%s\n' "${array[@]}"

This is the error I get:

Screenshot of the error i get

I know the problem comes from my operator % but I need it to restrict the result of my query.

dazageek
  • 24
  • 4
  • I see like search field '%XXBP%' but in error message "%XXBP%" double quotes thats is the issue ... it should be in single quotes – kanagaraj Nov 07 '18 at 14:51
  • I wrote my code with single quote as you can see above – dazageek Nov 07 '18 at 14:53
  • Wow. That is a major ksh88 bug, Good reason to use ksh93. As an aside, your array variable is not declared as an array (`typeset -a`) so the ${array[@]} gives the contents of the variable. ${array} or $array would work just as well. – user1683793 Nov 08 '18 at 03:16

1 Answers1

2

This sounds like this very specific ksh bug where here-documents silently convert single-quotes to double-quotes. You could try the workaround in that answer, e.g.

#!/usr/bin/ksh

echo Start Executing SQL commands

# put the single-quotes in a variable to prevent the here-document from converting them to double
STR="'%XXBP%'"

array=$(sqlplus -s apps/apps << eof
SET PAGESIZE 0;
SELECT directory_name from all_directories where directory_name like $STR;
eof)

printf '%s\n' "${array[@]}"
kfinity
  • 8,581
  • 1
  • 13
  • 20