Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash脚本中执行gcloud命令_Bash_Gcloud - Fatal编程技术网

在bash脚本中执行gcloud命令

在bash脚本中执行gcloud命令,bash,gcloud,Bash,Gcloud,gcloud init命令在bash脚本执行期间不提供登录提示 但它在我在脚本结束后手动键入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 c

gcloud init
命令在bash脚本执行期间不提供登录提示

但它在我在脚本结束后手动键入
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)?  
我的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

如何在bash脚本执行期间获得登录提示?

不知何故,
exec-l$SHELL
造成了所有的混乱。我将它改为
source~/.bashrc
,现在它可以工作了。

发布的代码片段存在许多问题

正确的代码片段(可能是):

原始版本的第一个错误是,
exec-l$SHELL
正在阻止进度,这是您自己发现的(至少不是原因)。这是因为您已经运行了一个交互式shell,该shell正在等待您的输入,而函数正在等待该进程退出,然后再继续

此外,
exec
将当前进程替换为生成的进程。你在这里真幸运。如果您没有将对
exec
的调用包装在单引号中,那么当您退出它启动的
$shell
时,您的函数将完全退出shell脚本。然而,实际上,
exec
只是替换了backticks添加的子shell,因此留下了一个子进程,可以安全地退出并返回到父/主脚本

第二个问题是backticks运行它们环绕的命令,然后用输出替换它们自己。这就是为什么

echo "bar `echo foo` baz"
输出
bar-foo-baz
等(在运行该命令之前运行
set-x
,查看实际运行的是什么命令)。因此,当您编写

`gcloud init --console-only`
您所说的是“运行
gcloud init--console only
,然后获取其输出并将命令替换为“该命令将尝试将输出作为命令本身运行”(这可能不是您想要的)。其他方面也是如此


虽然
chown
和可能的
gcloud init
不返回任何内容,因此生成的命令行为空。

每个backtick-ed命令都在自己的shell中运行。因此它们不会交互(特别是
gcloud init--console only
不会影响以后的shell)。此外,这里的倒勾是错误的。他们运行内部命令,然后尝试将这些命令的输出作为命令运行。@EtanReisner您有什么建议?将所有命令放在一个反勾选命令中?比如,backtick
exec-l$SHELL和&gcloud init——仅控制台和&chown-R$USER:$USER~/
backtick?不,这里根本不需要backtick。他们只是错了。。。除非
gcloud init--console only
抛出需要当前shell运行的行,在这种情况下,您可能需要
eval`gcloud init--console only`
,但我不知道它会这样做。您只需要在当前shell中运行这些命令。所以只需像编写普通命令一样编写它们。并且不要运行新的shell(然后需要退出),因为您不需要新的shell会话。@EtanReisner好的,我知道了。非常感谢。创造一个答案,我会投赞成票。
`gcloud init --console-only`