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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

How to give password in shell script?

+2
−4

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.

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

2 comment threads

Kinda difficult to know what you want to do based on what you're writing... do you want to get prompt... (1 comment)
Of course the linked SO answer will work with sudo (1 comment)

1 answer

+12
−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.

History
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 »