Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:27:04+00:00 2026-06-03T11:27:04+00:00

I have a bash script that needs to perform a couple actions in MySQL.

  • 0

I have a bash script that needs to perform a couple actions in MySQL. So far I have something like this:

#!/bin/sh
read -p "Enter your MySQL username: " $sqluname
read -sp "Enter your MySQL password (ENTER for none): " $sqlpasswd
/usr/bin/mysql -u $sqluname -p $sqlpasswd << eof
*some SQL stuff*
eof

This works well until it’s used with a MySQL username that has a blank password. The user hits ENTER at my password prompt, but then they receive another prompt to “Enter your password” again from MySQL.

How can I avoid this second password prompt and make the script deal with both blank and non-blank passwords?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:27:06+00:00Added an answer on June 3, 2026 at 11:27 am

    Check if the password is blank or not, and if it is, then omit the -p switch altogether.

    read -p "Enter your MySQL username: " $sqluname
    read -sp "Enter your MySQL password (ENTER for none): " $sqlpasswd
    if [ -n "$sqlpasswd" ]; then
      /usr/bin/mysql -u $sqluname -p $sqlpasswd << eof
    else
      /usr/bin/mysql -u $sqluname << eof
    fi
    

    Later edit to avoid a whole if-then-else block:

    command="/usr/bin/mysql -u $sqluname"
    [ -n "$sqlpasswd" ] && command="$command -p$sqlpasswd"
    eval $command
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script that looks like this: #!/bin/sh previousRelease=`git describe --tags --match
I have a bash script that looks like the following: #!/bin/bash FILES=public_html/*.php # */
I have a script in Bash called Script.sh that needs to know its own
I have a bash script that has this function in it: function start_vi() {
i have this problem: I have created a bash script that performs some tasks.
I have a sh/bash script that needs to call a batch file with parameters
He all, i have a problem with my bash script. That's my code: #!/bin/bash
I have a bash script that needs to build some sets each containing 1
I have an old shell script which needs to be moved to bash. This
I have a bash script that I need to write my password to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.