Comments on How to give password in shell script?
Parent
How to give password in shell script?
That's my Bash script:
#!bin/sh
cd qpixel
sudo systemctl enable mysql
mysql -u root -p
rails s
When it executes the line mysql -u root -p
it will ask for password. To do so, I have tried with echo
and print
, but none of them works.
How can I input a password?
Related question I found in SO. Answers of SO didn't help me either.
Post
That's my bash script.
Actually, no, it isn't. Assuming for a second that the shebang line is #!/bin/sh
(not #!bin/sh
as you have it in the question), it's a sh shell script, not a bash shell script. Assuming that your /bin/sh is actually bash (which it need not be; for example, Debian uses dash
as sh
), bash will start in a different mode when invoked as sh
. Other shells may do things differently when invoked as sh
or as some other name, or they might not.
The distinction is unlikely to make any difference with such a simple shell script as you have in the question, but for more advanced features, it can make a very real difference.
That said, ignoring the fact that this is through a shell script, it seems that your question actually is "how do I invoke the mysql
client such that it does not prompt interactively for a MySQL user's password, but authenticates using a password?".
The answer to that is in the mysql(1)
man page.
A bare mysql -p
asks the MySQL client to enter password authentication mode.
The -p
can also take a password, either as --password=qwerty
or -pqwerty
(note the absence of a space separating -p
and the password).
The man page also notes that passing a password on the command line is insecure (this answer of mine has a more in-depth discussion on why that is).
Note that when invoked in this manner, the shell executing the script will wait for the mysql
process to exit before continuing with the rails
command. That's probably not what you actually want. To execute a command in the background and continue with the execution of the script without waiting for that command to actually finish, you would use &
to background the process, but since mysql
is an interactive tool, that would make little sense here.
2 comment threads