I am learning to write bash scripts when I ran across this doubt. Have a look at the script below. It is not the actual script but is perfect to put my point.
#!/bin/bash
firstName = "Tony"
lastName = "Stark"
init
#The command prompts here:
# > Hello!
# > bla blah
# > What is your name?
echo firstName
# > bla blah
# > Now you may enter your last name
echo lastName
Problem: So what I want to achieve is to echo only when the command prompts for What is your name?
. I want to wait until prompt asks this specific question and then echo
the stuff automatically. I am trying to automate this data using the script.
I tried the read
command but it didn't work as thought. Is there a good way to do it? Anybody willing to shed some light is greatly appreciated. Thanks in advance!
Edit: Consider this for an instance. Please don't look at the security risks for this example. Lets automate git push.
#!/bin/bash
username: "un"
password: "pd"
git add -A
git commit -m "Update"
# returns a commit message in a second or so. It can throw an error. So `read` can not know how many lines to read. Execute the next command after reading whatso ever is the result(for now)
read message #ignore whatever the message is
git push origin master
# Now it asks for username. What I want is to echo the username on when it outputs: `Username for 'https://github.com':`
echo username
# Asks `Password for un:`
echo password
I know it has many security and functional loopholes. But I want to know is the particular way to automate echoing after a specific question is asked.