2

I'm a scripting noob and trying to get this find/replace working that injects salt strings in a wp-config.php file, replacing multiple placeholders.

    #!/bin/bash -e
    function salt() {
        uniqSalt=$(cat /dev/urandom | env LC_CTYPE=C tr -dc "a-zA-Z0-9!@#$%^&*()-_ []{}<>~\`+=,.;:/?|" | fold -w 64 | head -n 1)
        echo $uniqSalt
    }
    perl -pi -e "s/put your unique phrase here/$(salt)/g" wp-config.php

This works, but it only generates the salt once so they are all the same - of course it needs to generate it again for every match.

Is it possible to modify this to do what I want? Or is there a better way?

Thanks.

1 Answer 1

2

You don't need bash and unnecessary piping through multiple system utilities,

perl -i -pe'
  BEGIN {
    @chars = ("a" .. "z", "A" .. "Z", 0 .. 9);
    push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|";
    sub salt { join "", map $chars[ rand @chars ], 1 .. 64 }
  }
  s/put your unique phrase here/salt()/ge
' wp-config.php
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant - thanks @Сухой27 , that's much more elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.