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
🐧 4 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
thanks. this thing worked for my java apps.
Hi Vivek
Thanks for the tips this helps me lot.
thanks
Hi , If we need to get amny words as a input from console . What is the option ?
For example ..
read -p ” Enter your Message” message
user input is here ..
Hr meeting today at 11.30 PM . Please gather.