Wait for input while executing bash script

8,494

you can use read command to read from STDIN.

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

NUM: number of characters to read from STDIN
VAR: variable to save the input

if you don't know the number of characters or want to read unlimited characters from STDIN, you can remove -n NUM. this causes read command to ended by ENTER and save the result(authorization code) to $VAR.

Share:
8,494

Related videos on Youtube

PRATEEK AGRAWAL
Author by

PRATEEK AGRAWAL

Updated on September 18, 2022

Comments

  • PRATEEK AGRAWAL
    PRATEEK AGRAWAL over 1 year

    I have seen this question: Pause execution and wait for user input , but my problem is different.

    I have a Command:

    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
    

    This command provides a link and we get a authorization code on that link. This command waits until we type that authorization code on terminal and after typing the authorization code, further processing happens.

    The output of this command as follow:

    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:
    

    Now If I write this command in a bash script called script.sh and execute that script, then it doesn't wait for user input of authorization code and further processing happens.

    I want that it should wait for authorization code if executing in a bash script.

    How can I do it??

  • PRATEEK AGRAWAL
    PRATEEK AGRAWAL about 6 years
    No, the line "Enter the authorization code:" comes automatically while executing the command. So I don't need to read it in a variable. I just need to type it in the terminal.
  • Ghasem Pahlavan
    Ghasem Pahlavan about 6 years
    you can read input by read VAR without any text. if you don't need the authorization code, why do you want to get the code from the input?