“user_data” script can't run a sed command

September 28, 2019 111 views
DigitalOcean Automated Setups API

I am creating a DigitalOcean droplet through their API. I am using their “user_data” feature to run commands upon creation.

This issue is that the “sed” command doesn’t work for this, and I can’t understand why. It works perfectly fine if you enter the command through the command line, but not in the script.

The code below is what I am using. Don’t mind the \' I am using PHP so I have to include those.

Anyways, I know that the script is running because the “installed” file is being created successfully. It’s just that the sed command isn’t running for some reason.

'user_data' => '                    
#cloud-config
runcmd:
- sed \'/DB_DATABASE/s/=.*/=test/g\' /var/www/html/app/.env
- sed \'/DB_USERNAME/s/=.*/=test/g\' /var/www/html/app/.env
- sed \'/DB_PASSWORD/s/=.*/=test/g\' /var/www/html/app/.env
- touch /var/www/html/app/storage/installed
'

The expected outcome is for the sed commands to work and change the values accordingly. The actual result is that the command isn’t ran, or just doesn’t work.

If you need more information please let me know!

Thank you

1 Answer

Hello, @Rainn

Looking at this I can see that you’re not using any options for the sed command itself. Usually the -i option should change the data for you, so it will be sed -i

Let me know how this goes.

Have another answer? Share your knowledge.