Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

How to give password in shell script?

+1
−2
#!bin/sh

	cd qpixel
	sudo systemctl enable mysql
	mysql -u root -p
	rails s

That's my bash script. When it will run mysql -u root -p it will ask for password. So, I have to print my password. But, I had tried with echo print none of them works. How can I input password?

Related question I found in SO. Answers of SO didn't help me either.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Of course the linked SO answer will work with sudo (1 comment)

1 answer

+4
−0

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

How can I use exit? (1 comment)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!