0

I have a rather simple bash script.

#!/bin/bash
echo
echo What is the root statement for the new debate?
read body

echo
echo What is your mysql password?
read -s pass

echo
sql='INSERT
        INTO `Debate`   (`unblocked`,   `debaterId`,    `dirty` )
        VALUES          ('"'1'"',       '"'15'"',       '"'0'"' );
    INSERT
        INTO `Statement`    (`body`,            `debateId`,         `debaterId` )
        VALUES              ('"'"${body}"'"',   LAST_INSERT_ID(),   '"'15'"'    );'

#echo mysql -u resolution -p ${pass} -D resolution -e \"${sql//[^a-zA-Z0-9(),;\`\'_]/ }\"
mysql -u resolution -p ${pass} -D resolution -e \"${sql//[^a-zA-Z0-9(),;\`\'_]/ }\"

What this is meant to do should be pretty straight forward but it comes down to "ask user for input on terminal... form sql command with the provided input... insert into database in two tables." It's meant to be a quick hack for adding new debates into the database for testing while development is ongoing.

Instead, mysql prints the help as if I had used -?. No error... just the help text. Everything looks correct when the mysql command is echoed to the terminal. And if I copy and paste the echoed command it works just fine. I've searched google and stack overflow but found nothing about this.

U@H ~  mysql --version
mysql  Ver 15.1 Distrib 10.3.28-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Cliff Armstrong
  • 197
  • 1
  • 12
  • 1
    A command that echoes correctly will (almost) always run incorrectly. This is because echo shows string literals, and if you escape shell syntax to become a string literal then it's no longer shell syntax. A good start would be removing the backslashes before your quotes and running the script with `bash -x yourscript` to show a debug trace – that other guy May 25 '21 at 02:30
  • I am 100% certain the very first thing I tried was unescaped double quotes. But it's working now so apparently I'm suffering from dementia. Thank you. If you'd like to remake your comment as an answer I'll make it as the accepted answer. – Cliff Armstrong May 25 '21 at 02:38
  • You didn't quote `$pass` when passing it to `mysql`, which will fail if the password contains white space. – user1934428 May 25 '21 at 07:22
  • Correct. But also current versions of mariadb-provided mysql refuse to accept a password on the command line... despite what the man page says `-p` will only ever prompt for a password. I knew this... but while desperately trying to figure out why mysql was behaving as if my syntax was off i added password parts of the script. The working version lets mysql ask for a password itself. – Cliff Armstrong May 25 '21 at 08:58

0 Answers0