1

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.

subtleseeker
  • 4,415
  • 5
  • 29
  • 41
  • Have a look at [read](https://ss64.com/bash/read.html). – PhilMasteG Jun 17 '18 at 11:59
  • @PhilMasterG Thanks for the quick reply! I read it but didn't got the point. I am fairly new to this stuff. Could you please be a little more specific? – subtleseeker Jun 17 '18 at 12:09
  • @PhilMasterG I have editted the question. Hope this explains my question better! I think you were telling about `-p` in `read`. Actually, I want to automate the output using the script. – subtleseeker Jun 17 '18 at 12:13
  • Okay I might have misunderstood your question - you want to run a command in combination with your script and as soon as that command prints "What is your name?" to standard output, you want to automatically reply with "Tony"? If that is the case please clarify your question with what you mean by "the command". – PhilMasteG Jun 17 '18 at 12:14
  • Is it not possible to prepare the entire answer at once? If the program you are communicating with takes newline-delimited input, you could just pipe it something like `\n\n\nTony\n\nStark\n`. – Norrius Jun 17 '18 at 12:17
  • Have a look at [this question](https://stackoverflow.com/questions/40791622/how-can-i-respond-to-prompts-in-a-linux-bash-script-automatically) for answers about interactive prompt automation. "Expect" is suggested there. – PhilMasteG Jun 17 '18 at 12:19
  • @Norrius It is not the case here. After the `init`, where I mentioned `bla blah`, I have no idea what and for how much lines the output will be. It could change depending on different situations. – subtleseeker Jun 17 '18 at 12:20
  • @PhilMasterG I have already looked `expect`. The problem occurs that I can't run bash commands in `expect` script. – subtleseeker Jun 17 '18 at 12:26
  • @subtleseeker I think you should edit your question and elaborate more about your exact use case and problem, because I can currently not see where you are heading. – PhilMasteG Jun 17 '18 at 12:29
  • @PhilMasterG I have updated the question with an example. Please have a look. Hope this explains it in a much better way! – subtleseeker Jun 17 '18 at 13:22
  • So you want to automate an interactive prompt. How exactly is that different from the question I mentioned in a previous comment? You can always, using bash, dynamically create an "Expect"-Script and execute it. Inside that script you can define the command to run and the answers to different prompts. – PhilMasteG Jun 17 '18 at 13:26

0 Answers0