How do you take a single line of input from the user in a shell script?
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
E-mail this to a friend
Printable version
Related Other Helpful FAQs:
- Linux script to prompt for password
- Howto Display text files in a shell script
- Shell Scripting: Convert Uppercase to Lowercase
- Shell scripting: read one line at a time from keyboard
- How do I block an IP on my Linux server?
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!




November 13th, 2007 at 6:38 am
thanks. this thing worked for my java apps.