How could I further shorten the following script who's purpose is to create a database && WordPress instance and then change permissions per the domain given and restart the server?

${domain} stands for the domain I pass as an argument in script execution. ${drt} stands for document root (/var/www/html).

echo "What's your DB root password?" && read -s dbrp
echo "What's your DB user password?" && read -s dbup

echo "CREATE USER "${domain}"@"localhost" IDENTIFIED BY \"${dbup}\";" | mysql -u root -p"${dbrp}"
echo "CREATE DATABASE ${domain};" | mysql -u root -p"${dbrp}"
echo "GRANT ALL PRIVILEGES ON ${domain}.* TO ${domain}@localhost;" | mysql -u root -p"${dbrp}"

cd ${drt}
curl -L http://wordpress.org/latest.tar.gz | tar -zxv -C ${domain}/
cp ${domain}/wp-config-sample.php ${domain}/wp-config.php
sed -i "s/database_name_here/${domain}"/g ${domain}/wp-config.php
sed -i "s/username_here/${domain}/g" ${domain}/wp-config.php
sed -i "s/password_here/${dbup}/g" ${domain}/wp-config.php

chown -R ${domain}:${domain} ${domain}/* && chmod -R a-x,a=rX,u+w ${domain}/* && systemctl restart nginx.service

My aim is to have it shortened by 2-3 rows, but I think it's likely impossible.

I thought about:

1) Removing the cd ${drt} syntax and start adding ${drt} everywhere I need.

2) Unite the first 2 seds this way:

sed -i "s/_name_here/${domain}"/g ${domain}/wp-config.php

I would like to know if there is something else that could be shortened (united) that you recognize.

  • 1
    Learn perl, save yourself a lot of hassle, reduce lines of code, and eliminate a whole class of possible errors (whitespace + improper quoting), AND have your code be beautifully elegant rather than an unreadable abomination. Also, get rid of the questions asking for passwords (there's your two line improvement) and take them as args. Finally, you seem to be doing a lot of work re-inventing things that your distro probably invented 20+ years ago....maybe learn to work WITH your OS rather than AROUND it. – cas Jan 16 '18 at 0:36
  • @cas I prefer python nowadays...the comment with vs around is spot on – Rui F Ribeiro Jan 16 '18 at 0:41
  • as a replacement for scripts that run sed lots of times, perl makes more sense. it's sh+sed+awk+tr+grep + a whole lot more (including CPAN modules for doing anything and everything you could possibly think of - including mysql connections, untarring files, fetching URLs) all in one language geared for text processing & systems administration tasks. but yeah, python would work too. – cas Jan 16 '18 at 0:44
  • @cas I don't think Bash scripts are "unreadable abominations". What do you mean by passing password quote marks as arguments? And what did I reinvent that my OS does? I did the best I know at the moment... – Arcticooling Jan 16 '18 at 7:43
  • bash scripts in general aren't. your script above is heading towards becoming one (some would say it's already there). re: passwords - take them as args on the command line rather than asking a question and reading input (e.g. $1 for domain, $2 for rootpw, $3 for the userpw - run as scriptname domain rootpass userpass). – cas Jan 16 '18 at 7:54

4 lines into one:

cp ${domain}/wp-config-sample.php ${domain}/wp-config.php
sed -i "s/database_name_here/${domain}"/g ${domain}/wp-config.php
sed -i "s/username_here/${domain}/g" ${domain}/wp-config.php
sed -i "s/password_here/${dbup}/g" ${domain}/wp-config.php

to (EDIT fixed regex following comment)

sed "s/[a-z_]*name_here/${domain}/g;s/password_here/${dbup}/g" ${domain}/wp-config-sample.php > ${domain}/wp-config.php

Then again, I do not know if your sample config file has other strings that match "name_here". You can have multiple sed replacements separated by ; on one line.

Prompting for passwords is a great security measure, using them on the command line later, however, defeats that ... so you might also get away with specifying the passwords on the command line, in the environment of the process.

dbrp=mySillySecret dbup=mySecret myShortenedWordpressScript.sh example.com
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.