执行bash脚本时等待输入

PRA*_*WAL 4 bash scripts input

我见过这个问题: Pause execution and wait for user input,但我的问题是不同的。

我有一个命令:

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
Run Code Online (Sandbox Code Playgroud)

此命令提供一个链接,我们在该链接上获得授权代码。此命令等待,直到我们键入授权码上端子和键入后授权码,进一步的处理发生。

该命令的输出如下:

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:
Run Code Online (Sandbox Code Playgroud)

现在,如果我在bash脚本调用写这个命令script.sh和执行该脚本,然后它不等待用户输入授权码和进一步的处理情况。

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

我该怎么做??

Gha*_*van 5

您可以使用read命令从STDIN.

read -n NUM -p "Enter the authorization code:" VAR
Run Code Online (Sandbox Code Playgroud)

NUM:要从STDIN
VAR读取的字符数:保存输入的变量

如果您不知道字符数或想从中读取无限个字符STDIN,您可以删除-n NUM. 这会导致read命令以ENTER结束并将结果(授权代码)保存到$VAR.