Turning a bash script into a busybox script

relima picture relima · Dec 28, 2015 · Viewed 10.7k times · Source

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")"

Answer

user2350426 picture user2350426 · Dec 28, 2015

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