当我遇到这个疑问时,我正在学习编写bash脚本。看看下面的脚本。这不是真正的剧本,但完全可以说明我的观点。
#!/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
问题:所以我想要实现的只是当命令提示What is your name?
时才回显。我想等到提示询问这个具体问题,然后自动echo
这些东西。我正在尝试使用脚本自动化这些数据。
我尝试了read
命令,但它并没有像想象的那样工作。有没有好办法呢?任何愿意发光的人都非常感激。提前谢谢!
编辑:考虑这个实例。请不要查看此示例的安全风险。让我们自动化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
我知道它有很多安全漏洞和功能漏洞。但我想知道在询问特定问题后自动回显的特定方法。