Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
How about: cat myscript.sh | ssh some_user@myserver bash -s - –  Ketan Feb 19 at 17:08
    
@Ketan Same story. :/ –  Łukasz Zaroda Feb 19 at 17:10

1 Answer 1

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

share|improve this answer
    
But I can login to this machine and login to mysql as root without any problems. How is it different from above command? –  Łukasz Zaroda Feb 19 at 17:19
    
ERROR 1045 (28000) said it. –  Mohsen Pahlevanzadeh Feb 19 at 17:20
    
how do you login? via mysql? –  Mohsen Pahlevanzadeh Feb 19 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. –  Łukasz Zaroda Feb 19 at 17:28
    
wow, be sure you have permission problem from mysql, your mysql configured for localhost. modify it. –  Mohsen Pahlevanzadeh Feb 19 at 17:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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