执行 bash 脚本时等待输入

执行 bash 脚本时等待输入

我曾经看到过这样一个问题: 暂停执行并等待用户输入,但我的问题不同。

我有一个命令:

google-oauthlib-tool --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless --client-secrets src/.config/client_secret_71131800372-dop7miagipbqink2ecnr33so61q3li0t.apps.googleusercontent.com.json

此命令提供了一个链接,我们得到一个授权码在该链接上。此命令会等待我们输入该授权码在终端上输入授权码,进行进一步处理。

该命令的输出如下:

Please visit this URL to authorize this application: 

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=71131800372-dop7miagipbqink2ecnr33so61q3li0t.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fassistant-sdk-prototype&state=jFHZmMINesXNFbPNR6ZeJcuDVzpmYq&prompt=consent&access_type=offline
Enter the authorization code:

现在,如果我在名为脚本并执行该脚本,那么它不会等待用户输入授权码并进行进一步的处理。

我希望它应该等待授权码如果在执行bash 脚本

我该怎么做??

答案1

您可以使用read命令来读取STDIN

read -n NUM -p "Enter the authorization code:" VAR

数量:要读取的字符数STDIN
风险自回归:用于保存输入的变量

如果你不知道字符数或想要读取无限个字符STDIN,你可以删除-n NUM。这会导致read命令以进入并将结果(授权码)保存到$VAR

相关内容