0

I have simple script:

#!/bin/bash

while getopts ":u:" opt; do
  case $opt in
    u)
      USER="${OPTARG}"
    ;;
    \?)
      echo "Unknown flag has been used: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

stty -echo
read -p "New password for user ${USER}: " USER_PWD
stty echo

mysql -uroot -p -e "CREATE DATABASE IF NOT EXISTS ${USER}; GRANT ALL ON *.* TO '${USER}'@'localhost' IDENTIFIED BY '${USER_PWD}'; FLUSH PRIVILEGES;"

I would like to use it on another machine without copying it there. So I'm doing:

cat myscript.sh | ssh myserver bash -s - -u some_user

However it doesn't prompt me for passwords, instead it outputs me only an error:

stty: standard input: Invalid argument Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

What I'm doing wrong? Is it impossible to being prompted when redirecting script like that?

Added: I realized that my script have more problems (like using $USER variable, that is standard environmental variable), however my question really is about prompting. I reduced a problem to this simple script:

#!/bin/bash

read -p "Please write something: " SOME_VARIABLE
echo "Done"

And when I'm doing this:

cat test.sh | ssh user@server bash -s

It just echoes "Done", and doesn't let me to input anything on "read". Is it normal and why is that?

2
  • How about: cat myscript.sh | ssh some_user@myserver bash -s -
    – mkc
    Feb 19 '14 at 17:08
  • @Ketan Same story. :/ Feb 19 '14 at 17:10
0

you don't have any permission to run query from remote machine with root priviliage, your error said it.

8
  • But I can login to this machine and login to mysql as root without any problems. How is it different from above command? Feb 19 '14 at 17:19
  • ERROR 1045 (28000) said it. Feb 19 '14 at 17:20
  • how do you login? via mysql? Feb 19 '14 at 17:21
  • Everything is visible in cited code. I'm logging through SSH and trying to run commands from redirected content of script. One of those commands queries mysql, however instead of asking me for password, it just fails. Feb 19 '14 at 17:28
  • wow, be sure you have permission problem from mysql, your mysql configured for localhost. modify it. Feb 19 '14 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.