【问题标题】:Turning a bash script into a busybox script将 bash 脚本转换为 busybox 脚本
【发布时间】:2015-12-28 21:40:52
【问题描述】:
我正在开发一个只有busybox(ash?)并且不支持bash 的设备。但是,我需要在它上面运行下面的 bash 脚本。有可能还是busybox根本不支持脚本?
#!/bin/bash
domain="mydomain.com"
record="11019653"
api_key="key1234"
ip="$(curl http://ipecho.net/plain)"
echo content="$(curl \
-k \
-H "Authorization: Bearer $api_key" \
-H "Content-Type: application/json" \
-d '{"data": "'"$ip"'"}' \
-X PUT "https://api.digitalocean.com/v2/domains/$domain/records/$record")"
【问题讨论】:
标签:
bash
curl
embedded
busybox
ash
【解决方案1】:
您的脚本中没有任何问题可能会灰飞烟灭。
好吧,除了你在做echo content="...."。这将在输出中打印单词content=,并用引号将命令的输出连接起来。
如果你想设置一个变量的值然后打印它,这样做:
#!/bin/ash
domain="mydomain.com"
record="11019653"
api_key="key1234"
ip="$(curl http://ipecho.net/plain)"
content="$(curl \
-k \
-H "Authorization: Bearer $api_key" \
-H "Content-Type: application/json" \
-d '{"data": "'"$ip"'"}' \
-X PUT "https://api.digitalocean.com/v2/domains/$domain/record/$record")"
echo "$content"
如果与名称 ash 链接,程序busybox 将充当外壳:
ln -s /bin/busybox /bin/ash
即使在您当前的目录(或测试目录)中:
ln -s /bin/busybox ash
然后,您可以通过在 PWD 中键入 ./ash 或在 PATH 目录中键入 ash 来启动它。如果你愿意,你可以从中测试命令和脚本,或者使用 bash 作为你的 shell,如果文件的 she-bang 是#!/bin/ash,则使用ash script.sh 或(更好)./script.sh 启动脚本