简体   繁体   中英

How to assign a variable value to a variable from another file?

I have a sql file abc.sql having a SELECT SQL statement

SELECT COL FROM $D_SCHEMA.TABLE;

I have a shell script script.ksh

D_SCHEMA=DEV
EXEC_SQL=`cat abc.sql`
#echo $EXEC_SQL

#Function db_sql, which exports data to a csv file
db_sql "EXPORT TO /app/dev FILE ${EXEC_SQL}"

Now I am not able to assign the value to $D_SCHEMA from the sql file when the content is assigned to a variable in shell script.

Currently the variable is treated as a literal in the shell script.

echo $EXEC_SQL
SELECT COL FROM $D_SCHEMA.TABLE

Expected output

echo $EXEC_SQL
SELECT COL FROM DEV.TABLE

How do I go about solving this?

#!/bin/ksh

# one example using eval

D_SCHEMA=DEV
EXEC_SQL='SELECT COL FROM $D_SCHEMA.TABLE'

echo "$EXEC_SQL"
# output: SELECT COL FROM $D_SCHEMA.TABLE

eval new_var=\""$EXEC_SQL"\"
echo "$new_var"
# output: SELECT COL FROM DEV.TABLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM