Problem in passing IFS array to SQL Query

Hi,

I have created a shell script that reads line from text file and insert into DB table. I have used IFS to separate the line text. Looks IFS is splitting text properly but while passing one of the values that has special characters in it to query, it is giving weird issue. Below is my script looks like

IFS.OLD=$IFS
IFS='#'
while read line
do
read -r -A a <<< "$line"
echo "flag is- ${a[0]}"
if [ "${a[0]}" = "Flag" ];
then
echo "Header"
elif [ "${a[0]}" = "NU" ];
then
echo "User - Insert"
echo "company is- ${a[4]}"
sqlplus -s $DB_PATH  <<EOF
set head off feedback off
INSERT INTO USERS (EMAIL, FULL_NAME, CONTACT_PHONE_NUMBER,COMPANY, USER_ID) VALUES ( '${a[1]}', '${a[2]}','${a[3]}','${a[4]}',999527 );
commit;
exit;
EOF
fi

Below is my text file data

Flag#NEW#EMAIL#Name#Phno#Comp#***
NU#john@gmail.com#John Mathew#1122333#A&N Services, Inc.

when I run the script, output is coming as below

flag is- Flag
Header
flag is- NU
User - Insert
company is- A&N Services, Inc.
 Enter value for n: old   1: INSERT INTO USERS (EMAIL, FULL_NAME, CONTACT_PHONE_NUMBER,COMPANY, USER_ID) VALUES ( 'john@gmail.com', 'John Mathew','1122333','A&N Services, Inc.',999527 )
 new   1: INSERT INTO USERS (EMAIL, FULL_NAME, CONTACT_PHONE_NUMBER,COMPANY, USER_ID) VALUES ( 'john@gmail.com', 'John Mathew','1122333','Acommit; Services, Inc.',999527 )

I really do not understand why it is showing output like this but I investigated and found that this issue is coming due to the parameter COMPANY in the query. It has special characters. If I remove and change to simple text then it is not giving this issue and record is inserting successfully. Please suggest if any solution.

Look here

Awesome! It is resolved. I was really trying from 2 days to find out.... Thank you so much for the link.

2 Likes