Execute gcloud commands in a bash script

srgbnd

gcloud init command doesn't offer login prompt during a bash script execution.

But it offered the login after I typed exit command manually after script ended.

vagrant@vagrant-ubuntu-trusty-64:~$ exit
logout
Welcome! This command will take you through the configuration of gcloud.

Settings from your current configuration [default] are:
Your active configuration is: [default]


Pick configuration to use:
 [1] Re-initialize this configuration [default] with new settings 
 [2] Create a new configuration
Please enter your numeric choice:  1

Your current configuration has been set to: [default]

To continue, you must log in. Would you like to log in (Y/n)?  

My bash script:

#!/usr/bin/env bash

OS=`cat /proc/version`

function setupGCE() {
curl https://sdk.cloud.google.com | bash
`exec -l $SHELL`
`gcloud init --console-only`
`chown -R $USER:$USER ~/`
}


if [[ $OS == *"Ubuntu"* || $OS == *"Debian"*  ]]
then
sudo apt-get -y install build-essential python-pip python-dev curl
sudo pip install apache-libcloud
setupGCE
fi

How can I get the login prompt during the bash script execution?

Etan Reisner

There are a number of issues with the posted snippet.

The correct snippet is (probably):

function setupGCE() {
    curl https://sdk.cloud.google.com | bash
    gcloud init --console-only
    chown -R $USER:$USER ~/
}

The first error with the original, which you discovered yourself (the what of it at least it not the why), is that exec -l $SHELL is blocking progress. It does that because you've run an interactive shell that is now waiting on you for input and the function is waiting for that process to exit before continuing.

Additionally, exec replaces the current process with the spawned process. You got lucky here actually. Had you not wrapped the call to exec in single quotes your function would have exited the shell script entirely when you exited the $SHELL it launched. As it is, however, exec just replaced the sub-shell that the backticks added and so you were left with a child process that could safely exit and return you to the parent/main script.

The second issue is that backticks run the command they surround and then replace themselves with the output. This is why

echo "bar `echo foo` baz"

outputs bar foo baz, etc. (Run set -x before running that to see what commands are actually being run.) So when you write

`gcloud init --console-only`

what you are saying is "run gcloud init --console-only then take its output and replace the command with that" which will then attempt to run the output as a command itself (which is likely not what you wanted). Similarly on the other lines.

This happens to not have been problematic here though as chown and likely gcloud init don't return anything and so the resulting command line is empty.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash script to spawn and execute commands

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute git commands from bash script?

From Dev

bash script execute commands after ssh

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

How to execute the following commands in a bash script?

From Dev

Bash script to execute multiple Unix commands

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

Bash Script execute commands from a file but if cancel on to jump on next one

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

Shell out into zsh and execute commands from bash script

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

Bash script does not execute commands until after delay

From Dev

If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

From Dev

If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

From Dev

Can a bash shell script open a text file with less and then execute less commands from the script?

From Dev

Howto switch / chage user id witin a bash script to execute commands in the same script?

From Dev

Optional commands for bash script

From Dev

Python script to execute external commands

From Dev

Bash tries to execute commands in heredoc

From Dev

Execute Bash commands Python way

From Dev

How does bash execute commands

From Dev

Execute bash commands in a curses window

From Dev

Go: execute bash script

From Dev

How to execute a bash script?

From Dev

Bash Script - Will not completely execute

Related Related

  1. 1

    Bash script to spawn and execute commands

  2. 2

    Cannot execute shell commands in bash script

  3. 3

    How to execute git commands from bash script?

  4. 4

    bash script execute commands after ssh

  5. 5

    Bash script to read from a file and execute commands

  6. 6

    Bash script to read from a file and execute commands

  7. 7

    How to execute the following commands in a bash script?

  8. 8

    Bash script to execute multiple Unix commands

  9. 9

    How to execute sudo commands with Expect & send commands in bash script?

  10. 10

    Bash Script execute commands from a file but if cancel on to jump on next one

  11. 11

    Bash script: How to execute commands consecutively without waiting for the previous one?

  12. 12

    Shell out into zsh and execute commands from bash script

  13. 13

    How to execute commands in docker container as part of bash shell script

  14. 14

    Bash script: How to execute commands consecutively without waiting for the previous one?

  15. 15

    How to execute shell commands in a loop from within a bash script?

  16. 16

    Bash script does not execute commands until after delay

  17. 17

    If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

  18. 18

    If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

  19. 19

    Can a bash shell script open a text file with less and then execute less commands from the script?

  20. 20

    Howto switch / chage user id witin a bash script to execute commands in the same script?

  21. 21

    Optional commands for bash script

  22. 22

    Python script to execute external commands

  23. 23

    Bash tries to execute commands in heredoc

  24. 24

    Execute Bash commands Python way

  25. 25

    How does bash execute commands

  26. 26

    Execute bash commands in a curses window

  27. 27

    Go: execute bash script

  28. 28

    How to execute a bash script?

  29. 29

    Bash Script - Will not completely execute

HotTag

Archive