Q. I need to write a shell script to prompt databasename, username and password from the user. How do I take a single line of input from the user in a shell script?
A. You need to use read command in a shell script to read single line of input frm the user in a shell.
One line is read from the standard input (keyboard), or from file descriptor FD if the -u option is supplied, and the first word is assigned to the first NAME, the second word to the second NAME, and so on, with leftover words assigned to the last NAME.
Syntax:
read {variable-name}
read -p {'Prompt text'} {variable-name}
Read single line of input from the user and store to variable called dbname:
read dbname
OR
read -p 'Enter Database name : ' dbname
To display variable value, enter:
echo $dbname
Sample shell script
#!/bin/bash read -p 'Enter mysql database name : ' dbname read -p 'Enter username : ' dbuname read -s -p 'Enter password : ' dbpass mysql -uroot -p'Password'<<EOFMYSQL CREATE DATABASE $dbname; GRANT ALL ON $dbname.* TO $dbuname@localhost IDENTIFIED BY "$dbpass"; EOFMYSQL
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 3 comments… read them below or add one }
thanks. this thing worked for my java apps.
Hi Vivek
Thanks for the tips this helps me lot.
thanks