无法通过cloud-init使bash脚本起作用 [英] Cannot make bash script work from cloud-init
问题描述
显然我在这里做错了.
云初始化脚本/etc/cloud/cloud.cfg
...
runcmd:
- [ sh, /opt/cloud-init-scripts/whatever.sh ]
脚本/opt/cloud-init-scripts/whatever.sh
#!/bin/bash
...
. /home/ubuntu/third-party/script.sh --silent
第三方脚本/home/ubuntu/third-party/script.sh
#!/usr/bin/env bash
function some_function() {
...
错误,我进入/var/log/cloud-init-output.log
/opt/cloud-init-scripts/whatever.sh: 3: /home/ubuntu/third-party/script.sh: Syntax error: "(" unexpected
我肯定在这里缺少明显的东西.我在调用第三方脚本时尝试使用 source
,.
和 sh
,尝试在各处更改shebang,但未成功.
I must be missing something obvious here. I tried using source
, .
and sh
when calling the third party script, tried changing shebangs everywhere but no success.
如果我从命令行运行相同的命令,它将起作用.
If I run the same command from command line it works.
推荐答案
您已在 runcmd
下指定了 sh
shell,但将she-bang设置为 bash
.后者并不重要,因为如果您以 sh/opt/cloud-init-scripts/whatever.sh
身份运行,它将以sh shell身份运行.我想您可能正在使用与 sh
shell不兼容的非 POSIX
shell功能.
You have specified sh
shell under runcmd
, but have she-bang set to bash
. The latter does not matter because if you run as sh /opt/cloud-init-scripts/whatever.sh
it will be run with sh shell. I guess you are probably using a non POSIX
shell feature which is incompatible with the sh
shell.
或者如果您打算在 bash
shell中运行脚本,则将 cloud-init
脚本中的 runCmd
更改为
Or alternatively if your intention is to run the script in bash
shell, change the runCmd
in cloud-init
script to
runcmd:
- [ bash, /opt/cloud-init-scripts/whatever.sh ]
这篇关于无法通过cloud-init使bash脚本起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!