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
\$\endgroup\$
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.

\$\endgroup\$

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.