シェル変数を使用してSQLステートメントを印刷しようとしましたが、期待した結果は得られません。
pdate=`date +%d-%b-%Y`
query='"select \* from table_name where partition_date='"$pdate"' and \$CONDITIONS"'
echo $query
予想出力:
"select * from table_name where partition_date="30-Nov-2018" and \$CONDITIONS"
実際の出力:
"select \* from table_name where partition_date=30-Nov-2018 and \$CONDITIONS"
ベストアンサー1
希望の出力を取得するには、次の手順を実行します。
pdate=$(date +%d-%b-%Y)
query='"select * from table_name where partition_date="'"$pdate"'" and \$CONDITIONS"'
# ............^^ no backslash ........................^..........^ quotes as plain chars
echo "$query"
Bashの代替は次のとおりですprintf
。引用を簡素化できます。
printf -v query '"select * from table_name where partition_date="%s" and \$CONDITIONS"' "$pdate"