1

I'm building a script that needs to dynamically generate certain values, store them into variables and then use them as commands or input the values into a function for use later.

The problem is once the final string is fairly long and contains spaces. The issue I run into is the shell thinking the value from the variables is just one long string. So when trying to re-use this value the shell thinks this is a single word effectively.

./sql_list_test.sh: line 14: mysql -uroot -pPassword test -B -e  "SELECT LastModified,HeaderLogoImage FROM Merchant LastModified >= '1970-01-01 00:00:00.000'" | sed "/NULL/d ; /N/d ; s//merchantimages///g ; s/.*///g ; /^s*$/d" > /merchantimages_Merchant_HeaderLogoImage.list: No such file or directory

So the string is formatted correctly and a copy+paste into shell shows the command works fine.

How do you go about this with bash?

And no it's not a matter of "You haven't put your variable into double quotes"

Here is an example script of how that string/var is built

#!/bin/bash
MYSQL_CMD='mysql -uroot -pPassword test -B -e'
PREFIX='merchantimages'
SED_CMD="sed \"/NULL/d ; /\\N/d ; s/\/${PREFIX}\///g ; s/.*\///g ; /^\s*$/d\""
TIMESTAMP='1970-01-01 00:00:00.000'

SQL_LIST=$(grep $PREFIX $HOME/sql_cmd_list | envsubst)

while read CMD; do
  echo "Run this: $CMD"
MYSQL_RUN() {
  "$CMD"
}
MYSQL_RUN
done <<< $SQL_LIST

Here is a line from the sql_cmd_list file which is where values get pulled from

$MYSQL_CMD "SELECT LastModified,HeaderLogoImage FROM Merchant LastModified >= '$TIMESTAMP'" | $SED_CMD > $FILE_LOCATION/merchantimages_Merchant_HeaderLogoImage.list
No Fun
  • 15
  • 3

0 Answers0