2
\$\begingroup\$

Wrote a small bash-script today that downloads the latest swedish wordpress archive , extracts it and writes wp-config.php

Would appreciate any feedback that could improve it / prettify it.

The script assumes that the database name & the username is always the same.

#!/bin/bash
#Download wordpress tar.gz
echo "Laddar ner senaste wordpress på svenska"
tarfile=$(curl -s https://sv.wordpress.org/ | grep -ioE "https:\/\/sv.wordpress.org\/wordpress-[0-9].[0-9].[0-9]-sv_SE.tar.gz")
wget $tarfile -O senaste_wp.tar.gz
tar xvfz senaste_wp.tar.gz -C ./
rm senaste_wp.tar.gz
# Move the wordpress files to this dir
mv wordpress/* .
rm -r wordpress
# Get wordpress salts
salts=$(curl https://api.wordpress.org/secret-key/1.1/salt/)
# Read userinput
read -p "Ange databasanvändare : " dbuser
read -p "Ange databaslösenord : " dbpass
# Write wp-config.php
cat > wp-config.php << EOF
define('DB_NAME', '$dbuser');
define('DB_USER', '$dbuser');
define('DB_PASSWORD', '$dbpass');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
$salts
\$table_prefix = 'wp_';
define('WP_DEBUG', false);
if ( !defined('ABSPATH') )
 define('ABSPATH', dirname(__FILE__) . '/');
require_once(ABSPATH . 'wp-settings.php');
EOF
asked Mar 7, 2016 at 19:45
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You don't need to escape slashes for grep, you can simplify to:

tarfile=$(curl -s https://sv.wordpress.org/ | grep -ioE "https://sv.wordpress.org/wordpress-[0-9].[0-9].[0-9]-sv_SE.tar.gz")

curl and wget are too similar tools. Consider using only one of them, to reduce the number of dependencies of your script.

Instead of this:

wget $tarfile -O senaste_wp.tar.gz
tar xvfz senaste_wp.tar.gz -C ./
rm senaste_wp.tar.gz

You can pipe directly to tar, without needing a temporary file, like this:

curl $tarfile | tar xvz

Lastly, your script is not using anything Bash specific, so you could change the shebang line to !#/bin/sh.

answered Mar 7, 2016 at 23:27
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.