Turning a bash script into a busybox script

点点圈 提交于 2019-12-12 03:30:05

问题


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?

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

回答1:


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.

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:

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 ./ash if in the PWD, or ash if 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.sh or (better) ./script.sh if the she-bang of the file is #!/bin/ash



来源:https://stackoverflow.com/questions/34501165/turning-a-bash-script-into-a-busybox-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!