linux - 无法使 bash 脚本从 cloud-init 运行

标签 linux bash shell

显然我在这里做错了。

云初始化脚本/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,尝试在所有地方更改 shebangs 但没有成功。

如果我从命令行运行相同的命令,它会起作用。

最佳答案

您在 runcmd 下指定了 sh shell,但将 she-bang 设置为 bash。后者无关紧要,因为如果您以 sh/opt/cloud-init-scripts/whatever.sh 运行,它将使用 sh shell 运行。我猜您可能正在使用与 sh shell 不兼容的非 POSIX shell 功能。

或者,如果您打算在 bash shell 中运行脚本,请将 cloud-init 脚本中的 runCmd 更改为

runcmd:
 - [ bash, /opt/cloud-init-scripts/whatever.sh ]

关于linux - 无法使 bash 脚本从 cloud-init 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46297120/

相关文章:

bash - 通过 stdin 处理输入但文件输入工作正常时出现意外行为

linux - 在文件列表上运行命令

bash - 尝试使用 BOXEE 中的 bash 启动 XBMC

php - php 中的色度键使用 php 的 ImageMagic 扩展

linux - 为什么允许修改argv[0]?

linux - gnome-terminal - 如何获取调用命令的通过/失败状态

打印可用内存和总内存的 Linux 基本脚本

shell - Ubuntu Server 21.10 - 插入 USB 设备时如何运行 Shell 脚本

c# - 在 ASP .NET CORE 中更新 Web 应用程序替换文件或删除文件,更新时如何在应用程序中保留文件

c - 从内核模块获取本地 NIC 的 ip 地址