Search code examples
shellshdb2-luw

Shell Script: how to Input Variable value from file to run sql with the supplied variable


I have a text file with two columns and 'n' number of data. I'm running one shell script that fetches the data from the file and input variable defined in the shell script.

My Data Arranges as follows

HX_EXT TX_EXT HX_GAMER TX_GAMER HX_SALE TX_SALE HX_PERF TX_PERF HX_ACCESS TX_ACCESS ... ... and so on**

Below Shell script fetch the data from input file with the variable defined..

#!/bin/bash
set -x
FILE_PATH=/home/db2git1
INP_FILE=$FILE_PATH/FINAL_FILE.OUT
db2 connect to GIT1;
db2 -x "SELECT substr(SCHEMANAME, 1, 50) FROM SYSCAT.SCHEMATA where SCHEMANAME LIKE 'HX_%'" > SOURECSCHEMA.OUT

cp SOURECSCHEMA.OUT DESTINATIONSCHEMA.OUT
sed -i 's/IM1_/DMI_/g' DESTINATIONSCHEMA.OUT
paste SOURECSCHEMA.OUT DESTINATIONSCHEMA.OUT > FINAL_FILE.OUT
chmod +x SCHEMA_MIMMIC.sh
chmod +x DESTINATIONSCHEMA.OUT
chmod +x SOURECSCHEMA.OUT


while IFS= read -r line
do
VAR1=$(echo "$INP_FILE"|cut -f 1 -d '|')
VAR2=$(echo "$INP_FILE"|cut -f 2 -d '|')
done

db2 "CALL SYSPROC.ADMIN_COPY_SCHEMA("$VAR1","$VAR2",'COPY',NULL,'','','ERRORSCHEMA','ERRORTAB')"

~

But It ends with the below error(Particularly no output until press enter).

+ cp SOURECSCHEMA.OUT DESTINATIONSCHEMA.OUT
+ sed -i s/IM1_/DMI_/g DESTINATIONSCHEMA.OUT
+ paste SOURECSCHEMA.OUT DESTINATIONSCHEMA.OUT
+ chmod +x SCHEMA_MIMMIC.sh
+ chmod +x DESTINATIONSCHEMA.OUT
+ chmod +x SOURECSCHEMA.OUT
+ IFS=
+ read -r line

++ echo /home/db2git1/FINAL_FILE.OUT
++ cut -f 1 -d '|'
+ VAR1=/home/db2git1/FINAL_FILE.OUT
++ echo /home/db2git1/FINAL_FILE.OUT
++ cut -f 2 -d '|'
+ VAR2=/home/db2git1/FINAL_FILE.OUT
+ IFS=
+ read -r line

++ echo /home/db2git1/FINAL_FILE.OUT
++ cut -f 1 -d '|'
+ VAR1=/home/db2git1/FINAL_FILE.OUT
++ echo /home/db2git1/FINAL_FILE.OUT
++ cut -f 2 -d '|'
+ VAR2=/home/db2git1/FINAL_FILE.OUT
+ IFS=
+ read -r line

Debug mode not throwing any error and stuck at the output as pasted above.

How to fix this error?


Solution

  • I guess you missed out on passing the file name to the while loop. Also, when you're trying to read the VAR1 and VAR2, I think the echo command should print the contents of line variable and not the file name.

    while IFS= read -r line
    do
    VAR1=$(echo "$line"|cut -f 1 -d '|')
    VAR2=$(echo "$line"|cut -f 2 -d '|')
    done < $INP_FILE
    

    Hope this helps :)