将 bash 脚本转换为 busybox 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34501165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:03:44  来源:igfitidea点击:

Turning a bash script into a busybox script

bashcurlembeddedbusyboxash

提问by relima

I am working on a device that only has busybox (ash?) and does not support bash. However, I need to run the bash script below on it. Is it possible or busybox simply does not support scripts?

我正在使用只有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")"

回答by

There is no issue in your script that could break in ash.

您的脚本中没有可能会在灰烬中中断的问题。

Well, except that you are doing echo content="....". That will print in the output the word content=joined to the output of the command in quotes.

好吧,除了你正在做的echo content="...."。这将在输出中打印content=在引号中连接到命令输出的单词。

If you want to set the value of a variable and then print it, do:

如果要设置变量的值然后打印它,请执行以下操作:

#!/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"

The program busybox will act as a shell if linked with the name ash:

如果与名称 ash 链接,则程序 busybox 将充当 shell:

ln -s /bin/busybox /bin/ash

Even in your present directory (or a test one):

即使在您当前的目录(或测试目录)中:

ln -s /bin/busybox ash

Then, you could start it by typing ./ashif in the PWD, or ashif in a PATH directory. You could test commands and scripts from it if you wish, or use bash as your shell and start scripts with ash script.shor (better) ./script.shif the she-bang of the file is #!/bin/ash

然后,您可以通过./ash在 PWD 或ashPATH 目录中键入if来启动它。如果您愿意,您可以从中测试命令和脚本,或者使用 bash 作为您的 shell 并使用ash script.sh或(更好)启动脚本,./script.sh如果文件的 she-bang 是#!/bin/ash