[英]Execute gcloud commands in a bash script
gcloud init
command doesn't offer login prompt during a bash script execution. 在执行bash脚本期间,
gcloud init
命令不提供登录提示。
But it offered the login after I typed exit
command manually after script ended. 但是在脚本结束后我手动键入了
exit
命令后,它提供了登录名。
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: 我的bash脚本:
#!/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? 在bash脚本执行过程中如何获得登录提示?
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. 您发现自己的原始文件的第一个错误(至少是它的原因而不是原因)是
exec -l $SHELL
阻止进度。 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. 此外,
exec
用生成的进程替换当前进程。 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. 如果没有将对
exec
的调用包装在单引号中,则当您退出启动的$SHELL
时,您的函数将完全退出shell脚本。 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. 但是,
exec
只是替换了反引号添加的子外壳,因此您剩下一个子进程,该子进程可以安全地退出并将您返回到父/主脚本。
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 输出
bar foo baz
等。(在运行该命令之前运行set -x
,以查看实际上正在运行的命令。)因此,当您编写时
`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). 您的意思是“运行
gcloud init --console-only
然后获取其输出并用该命令替换命令”,然后它将尝试将输出作为命令本身运行(这可能不是您想要的)。 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. 尽管
chown
和gcloud init
可能不会返回任何内容,所以这里碰巧没有问题,因此生成的命令行为空。
Somehow the exec -l $SHELL
did all the mess. 高管
exec -l $SHELL
搞砸了所有的麻烦。 I changed it to source ~/.bashrc
and now it works. 我将其更改为
source ~/.bashrc
,现在可以使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.