Need help in shell script

I want to create a one click install script of wordpress, the script is throwing error and i am unable to processd. Need guidance.
when the user creates a directory using php, the directory is created and a shell script is executed inside the folder and wordpress is installed automatically… need help in fixing the code, I tried but I have just started to code the shellscript…

the code is below - for creating the folder is below,

#!/bin/bash
clear

DB_NAME="$(openssl rand -base64 12)"
read -r\ "{DB_NAME}";

DB_USER="$(openssl rand -base64 12)"
read -r\ "{DB_USER}";

DB_PASS="$(openssl rand -base64 12)"
read -r\ "{DB_PASS}";

DB_HOST="localhost"
read -r\ "{DB_HOST}";

UserName="$(openssl rand -base64 12)"
read -r\ "{UserName}";

UserPassword="$(openssl rand -base64 12)"
read -r\ "{UserPassword}";

DB_PASS="$(openssl rand -base64 12)"
read -r\ "{DB_PASS}";

echo "============================================"
echo "          WordPress Install Script          "
echo "============================================"
echo
echo "=============Database details==============="


echo -n "Database Host : "
read -r\ "{$DB_HOST}"

echo -n "Database Name: "
read -r\ "{DB_NAME}"

echo -n "Database User : "
read -r\ "{DB_USER}"

echo -n "Database Password : "
read -r\ "{DB_PASS}"

echo
echo "=============Admin details=================="

echo -n "Site url (e.g http://linuxtweaks.in/) : "
read siteurl

read -e -p "Site Name:" -i "Ricardo" SiteName
read $SiteName

# echo -n "Site Name (e.g linuxtweaks) : "
# read sitename

read -e -p "Site Name:" -i "info@linuxtweaks.in" Email
read $Email

# echo -n "Email Address (e.g info@linuxtweaks.in) : "
# read wpemail

# echo -n "Admin User Name : "
# read wpuser

read -e -p "User Name:" -i "$UserName" username
read $Username

read -e -p "User Password:" -i "$UserPassword" username
read $UserPassword

# echo -n "Admin User Password : "
# read wppass

# echo -n "run install? (y/n) : "
# read run

read -e -p "run install? (y/n) :" -i "y" run
read $run

if [ "$run" == n ] ; then
exit
else
echo
echo "============================================"
echo "A robot is now installing WordPress for you."
echo "============================================"
echo "Downloading wordpress latest version..."
curl -O https://wordpress.org/latest.tar.gz
echo "Extracting tarball wordpress..."
tar -zxvf latest.tar.gz
#copy file to parent dir
cp -rf wordpress/* .
#remove files from wordpress folder
rm -R wordpress
#create wp config
echo
echo "Creating database configuration file..."
cp wp-config-sample.php wp-config.php
#set database details with find and replace
sed -i "s/localhost/$DB_HOST/g" wp-config.php
sed -i "s/database_name_here/$DB_NAME/g" wp-config.php
sed -i "s/username_here/$DB_USER/g" wp-config.php
sed -i "s/password_here/$DB_PASS/g" wp-config.php
#create uploads folder and set permissions
mkdir wp-content/uploads
chmod 777 wp-content/uploads
echo
echo "Installing wordpress..."
wp core install --url="$siteurl" --title="$SiteName" --admin_user="$Username" --admin_password="$UserPassword" --admin_email="$wpemail"
#remove zip file
rm latest.tar.gz
#remove bash script
rm wp-install.sh

#
#   Install plugins
#   ------------------
#

#   Wordfence Security
echo "Fetching Wordfence Security plugin...";
wget --quiet https://downloads.wordpress.org/plugin/wordfence.zip;
unzip -q wordfence.zip;
mv wordfence/ wp-content/plugins/
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#   WordPress SEO a.k.a. Yoast
echo "Fetching WordPress SEO (a.k.a. Yoast) plugin...";
wget --quiet https://downloads.wordpress.org/plugin/wordpress-seo.zip;
unzip -q wordpress-seo.zip;
mv wordpress-seo/ wp-content/plugins/
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#
#   Remove default WP plugins
#   ------------------
#
echo "Removing default WordPress plugins..."
rm -rf wp-content/plugins/akismet
rm -rf wp-content/plugins/hello.php
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#
#   Remove older WordPress default themes
#   ------------------
#
echo "Removing default WordPress themes..."
rm -rf wp-content/themes/twentyfifteen
rm -rf wp-content/themes/twentysixteen
rm -rf wp-content/themes/twentyseventeen
rm -rf wp-content/themes/twentynineteen
rm -rf wp-content/themes/twentytwenty
echo "${GREEN}Done! ✅${NC}"
printf '\n'

echo "========================="
echo "Installation is complete."
echo "========================="
echo
echo "Thankyou...installed successfully"
fi

this is not running , need help in fixing the code, I tried but I have just started to code the shellscript…

It looks like you are trying to prompt the user for input but at the same time you already created some random values for the variables. What are you trying to achieve?

I want to create a auto installer by running the script. Here the dynamic values should be assigned to the places according to the variables I created, like

{DB_NAME} , {DB_USER}, {DB_PASS},{DB_HOST},{UserName}, {UserPassword},{DB_PASS} in the Admin block section and in the install wordpress section…

in the front end I will be having a text box to create a folder / directory and inside this the script should install wordpress and configure automatically including the creation of database in the php myadmin.

the code for creating the folder is below

‘’’

<?php if(isset($_POST['createfolder']) && !empty($_POST['createfolder'])){ $folder_name = $_POST['createfolder']; if (!empty($folder_name) && !file_exists($folder_name))/* Check folder exists or not */ { /* Create folder by using mkdir function */ @mkdir($folder_name, 0777); /* Success Message */ echo "Folder Created : $folder_name"; $output=exec('cp chmod +x ./wordpres.sh ./$folder_name'); echo "$output"; }else{ echo "Folder Already exist!"; } } ?>

‘’’

Sorry for the delayed reply, I am from India

Regards,
Vijey

I guess I’m confused about why you are declaring variables with random values and then asking for user input? I would go for something like this

read -rp "Enter database name: " DB_NAME
read -rp "Enter database username: " DB_USER
read -rp "Enter database password: " DB_PASS
read -rp "Enter database host: " DB_HOST
read -rp "Enter username: " USER_NAME
read -rp "Enter password: " USER_PASSWORD

You can also just run mkdir to create a folder, instead of mixing bash and php scripts.

Hi, I am not good with shell script.
I want to assign the random values to the assigned variables for me to setup the wordpress.
I dont want to get any user input from the user.

Will the above code assign the values in the corresponding variables or the above code will get the input from the user?? I am confused a little

Regards,
Vijey

Try it, it’s the best way to learn :slight_smile:

Hi, Thanks for the reply and the guidance. The script is not working actually. I think there are mistakes. from the guidance you gave, I changed and ran the script but unable to find the exact error. Need your help in trouble shooting
the code is below

#!/bin/bash

clear

DB_NAME="$(openssl rand -base64 12)"

read -rp "{DB_NAME}";

DB_USER="$(openssl rand -base64 12)"

read -rp "{DB_USER}";

DB_PASS="$(openssl rand -base64 12)"

read -rp "{DB_PASS}";

DB_HOST="localhost"

read -rp "{DB_HOST}";

UserName="$(openssl rand -base64 12)"

read -rp "{UserName}";

UserPassword="$(openssl rand -base64 12)"

read -rp "{UserPassword}";

DB_PASS="$(openssl rand -base64 12)"

read -rp "{DB_PASS}";

echo "============================================"

echo " WordPress Install Script "

echo "============================================"

echo

echo "=============Database details==============="

read -rp "Enter database name: " DB_NAME

read -rp "Enter database username: " DB_USER

read -rp "Enter database password: " DB_PASS

read -rp "Enter database host: " DB_HOST

read -rp "Enter username: " UserName

read -rp "Enter password: " DB_PASS

echo

echo "=============Admin details=================="

echo -n "Site url (e.g http://linuxtweaks.in/) : "

read -rp siteurl

read -e -p "Site Name:" -i "Ricardo" SiteName

read -rp $SiteName

# echo -n "Site Name (e.g linuxtweaks) : "

# read sitename

read -e -p "Site Name:" -i "info@linuxtweaks.in" Email

read -rp $Email

# echo -n "Email Address (e.g info@linuxtweaks.in) : "

# read wpemail

# echo -n "Admin User Name : "

# read wpuser

read -e -p "User Name:" -i "$UserName" username

read -rp $Username

read -e -p "User Password:" -i "$UserPassword" username

read -rp $UserPassword

# echo -n "Admin User Password : "

# read wppass

# echo -n "run install? (y/n) : "

# read run

read -e -p "run install? (y/n) :" -i "y" run

read -rp $run

if [ "$run" == n ] ; then

exit

else

echo

echo "============================================"

echo "A robot is now installing WordPress for you."

echo "============================================"

echo "Downloading wordpress latest version..."

curl -O https://wordpress.org/latest.tar.gz

echo "Extracting tarball wordpress..."

tar -zxvf latest.tar.gz

#copy file to parent dir

cp -rf wordpress/* .

#remove files from wordpress folder

rm -R wordpress

#create wp config

echo

echo "Creating database configuration file..."

cp wp-config-sample.php wp-config.php

#set database details with find and replace

sed -i "s/localhost/$DB_HOST/g" wp-config.php

sed -i "s/database_name_here/$DB_NAME/g" wp-config.php

sed -i "s/username_here/$DB_USER/g" wp-config.php

sed -i "s/password_here/$DB_PASS/g" wp-config.php

#create uploads folder and set permissions

mkdir wp-content/uploads

chmod 777 wp-content/uploads

echo

echo "Installing wordpress..."

wp core install --url="$siteurl" --title="$SiteName" --admin_user="$Username" --admin_password="$UserPassword" --admin_email="$wpemail"

#remove zip file

rm latest.tar.gz

#remove bash script

rm wp-install.sh

#

# Install plugins

# ------------------

#

# Wordfence Security

echo "Fetching Wordfence Security plugin...";

wget --quiet https://downloads.wordpress.org/plugin/wordfence.zip;

unzip -q wordfence.zip;

mv wordfence/ wp-content/plugins/

echo "${GREEN}Done! ✅${NC}"

printf '\n'

# WordPress SEO a.k.a. Yoast

echo "Fetching WordPress SEO (a.k.a. Yoast) plugin...";

wget --quiet https://downloads.wordpress.org/plugin/wordpress-seo.zip;

unzip -q wordpress-seo.zip;

mv wordpress-seo/ wp-content/plugins/

echo "${GREEN}Done! ✅${NC}"

printf '\n'

#

# Remove default WP plugins

# ------------------

#

echo "Removing default WordPress plugins..."

rm -rf wp-content/plugins/akismet

rm -rf wp-content/plugins/hello.php

echo "${GREEN}Done! ✅${NC}"

printf '\n'

#

# Remove older WordPress default themes

# ------------------

#

echo "Removing default WordPress themes..."

rm -rf wp-content/themes/twentyfifteen

rm -rf wp-content/themes/twentysixteen

rm -rf wp-content/themes/twentyseventeen

rm -rf wp-content/themes/twentynineteen

rm -rf wp-content/themes/twentytwenty

echo "${GREEN}Done! ✅${NC}"

printf '\n'

echo "========================="

echo "Installation is complete."

echo "========================="

echo

echo "Thankyou...installed successfully"

fi

I am getting the error from the line

read -e -p “Site Name:” -i “Ricardo” SiteName
read -rp $SiteName

I am unable to troubleshoot…

You are trying to run the entire script at once. The longer the script, the harder it is to troubleshoot. Instead, start with smaller bits and test it ever step of the way, adding more code as you go to make sure everything works. If something breaks it will be much easier to find the problem.

Write down a list of steps you need to complete, like taking user input, installing packages, setting up whatever folder structure, permissions, etc.

Sure,
I will do peace by peace and will reply you again… When I ran the script today morning, peace by peace it threw lot of errors, so i ran the entire script… l

Let me check again as guided by you.

thanks a lot for the brotherly guidance.

Regards,
Vijay

Hi,
I have revamped the code a little … need your guidance

#!/bin/bash
clear

# Variables
DB_NAME=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
read -rp "{DB_NAME}";

DB_USER=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
read -rp "{DB_USER}";

DB_PASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
read -rp "{DB_PASS}";

DB_HOST="localhost"
read -rp "{DB_HOST}";

WP_ADMIN_USER=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
read -rp "{WP_ADMIN_USER}";

WP_ADMIN_PASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
read -rp "{WP_ADMIN_PASS}";

DB_PASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
read -rp "{DB_PASS}";

WP_DOMAIN="example.com"
WP_TITLE="My WordPress Site"
read -e -p "WP Site Title:"

# Set up the MySQL database
sudo mysql -u root -e "CREATE DATABASE ${DB_NAME} DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
sudo mysql -u root -e "CREATE USER ${DB_USER}@${DB_HOST} IDENTIFIED BY '${DB_PASS}';"
sudo mysql -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO ${DB_USER}@${DB_HOST};"
sudo mysql -u root -e "FLUSH PRIVILEGES;"

# Download and extract WordPress
# Get the parent directory of the current working directory
sub_dir=$(dirname "$PWD")
# Get the name of the parent directory
parent_dir=$(basename "$sub_dir")

sudo wget https://wordpress.org/latest.zip -O /tmp/wordpress.zip
sudo unzip -q /tmp/wordpress.zip -d /var/www/html/
sudo mv /var/www/html/wordpress/* /var/www/html/
sudo rm -rf /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/${parent_dir}

#
#   Install plugins
#   ------------------
#

#   Wordfence Security
echo "Fetching Wordfence Security plugin...";
wget --quiet https://downloads.wordpress.org/plugin/wordfence.zip;
unzip -q wordfence.zip;
mv wordfence/ wp-content/plugins/
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#   WordPress SEO a.k.a. Yoast
echo "Fetching WordPress SEO (a.k.a. Yoast) plugin...";
wget --quiet https://downloads.wordpress.org/plugin/wordpress-seo.zip;
unzip -q wordpress-seo.zip;
mv wordpress-seo/ wp-content/plugins/
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#
#   Remove default WP plugins
#   ------------------
#
echo "Removing default WordPress plugins..."
rm -rf wp-content/plugins/akismet
rm -rf wp-content/plugins/hello.php
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#
#   Remove older WordPress default themes
#   ------------------
#
echo "Removing default WordPress themes..."
rm -rf wp-content/themes/twentyfifteen
rm -rf wp-content/themes/twentysixteen
rm -rf wp-content/themes/twentyseventeen
rm -rf wp-content/themes/twentynineteen
rm -rf wp-content/themes/twentytwenty
echo "${GREEN}Done! ✅${NC}"
printf '\n'

# Configure WordPress
sudo sed -i "s/database_name_here/${DB_NAME}/" /var/www/html/wp-config-sample.php
sudo sed -i "s/username_here/${DB_USER}/" /var/www/html/wp-config-sample.php
sudo sed -i "s/password_here/${DB_PASS}/" /var/www/html/wp-config-sample.php
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo sed -i "s/example.com/${WP_DOMAIN}/" /var/www/html/wp-config.php
sudo sed -i "s/My WordPress Site/${WP_TITLE}/" /var/www/html/wp-config.php

# Set up Apache virtual host
sudo echo "<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName ${WP_DOMAIN}
    ServerAlias www.${WP_DOMAIN}
    DocumentRoot /var/www/html/
    <Directory /var/www/html/>
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog \${APACHE_LOG_DIR}/${WP_DOMAIN}-error.log
    CustomLog \${APACHE_LOG_DIR}/${WP_DOMAIN}-access.log combined
</VirtualHost>" | sudo tee /etc/apache2/sites-available/${WP_DOMAIN}.conf > /dev/null
sudo a2ensite ${WP_DOMAIN}
sudo a2enmod rewrite
sudo systemctl restart apache2

# Create admin user
sudo wp user create ${WP_ADMIN_USER} admin@${WP_DOMAIN} --role=administrator --user_pass=${WP_ADMIN_PASS} --display_name=${WP_ADMIN_USER}
fi

Where are you stuck exactly? What errors are you getting, if any? What output are you getting versus what you expect to get? What have you tried to make that happen?

It’s unreasonable to present the entire script and expect other people to help without knowing what problem you are having or giving any additional information. Incidentally, answering the questions above on your own will force you to research the issue and learn not only about the topic itself, but also how to ask good questions.

As for your script, why are you asking for user input using the read command while at the same time providing a random value for it? If you want to simply show it on the terminal output so that the user can grab it, you can use echo instead.

I have to insist: break down the entire problem into smaller bits. Make sure each part of the script works before putting it together. And if you must take a step back to learn the basics of bash, that’s perfectly fine.

1 Like

This is the error I am getting… I changed the code a little for me to check where the error is coming
the code is below

#!/bin/bash

# Variables

# Download and extract WordPress
# Get the parent directory of the current working directory
sub_dir=$(dirname "$PWD")
# Get the name of the parent directory
parent_dir=$(basename "$sub_dir")

WP_DOMAIN="$(basename $(dirname $PWD))"
read -rp "{WP_DOMAIN}";

WP_TITLE="My WordPress Site"

DB_NAME=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
DB_USER=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
DB_PASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
DB_HOST="localhost"
WP_DOMAIN="example.com"
WP_TITLE="My WordPress Site"
WP_ADMIN_USER=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
WP_ADMIN_PASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9+*$@#' | fold -w 10 | head -n 1)
DB_COLLATE="utf8mb4_unicode_ci"

# Install necessary packages
# sudo apt-get update
# sudo apt-get -y install apache2 mysql-server php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc unzip

# Set up the MySQL database
sudo mysql -u root -e "CREATE DATABASE ${DB_NAME} DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
sudo mysql -u root -e "CREATE USER ${DB_USER}@${DB_HOST} IDENTIFIED BY '${DB_PASS}';"
sudo mysql -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO ${DB_USER}@${DB_HOST};"
sudo mysql -u root -e "FLUSH PRIVILEGES;"

# Download and extract WordPress
sudo wget https://wordpress.org/latest.zip -O /tmp/wordpress.zip
sudo unzip -q /tmp/wordpress.zip -d /var/www/html/
sudo mv /var/www/html/wordpress/* /var/www/html/
sudo rm -rf /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/

# Configure WordPress
sudo sed -i "s/database_name_here/${DB_NAME}/" /var/www/html/wp-config-sample.php
sudo sed -i "s/username_here/${DB_USER}/" /var/www/html/wp-config-sample.php
sudo sed -i "s/password_here/${DB_PASS}/" /var/www/html/wp-config-sample.php
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo sed -i "s/example.com/${WP_DOMAIN}/" /var/www/html/wp-config.php
sudo sed -i "s/My WordPress Site/${WP_TITLE}/" /var/www/html/wp-config.php
sudo sed -i "s/My WordPress Site/${DB_COLLATE}/" /var/www/html/wp-config.php


#
#   Install plugins
#   ------------------
#

#   Wordfence Security
echo "Fetching Wordfence Security plugin...";
wget --quiet https://downloads.wordpress.org/plugin/wordfence.zip;
unzip -q wordfence.zip;
mv wordfence/ wp-content/plugins/
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#   WordPress SEO a.k.a. Yoast
echo "Fetching WordPress SEO (a.k.a. Yoast) plugin...";
wget --quiet https://downloads.wordpress.org/plugin/wordpress-seo.zip;
unzip -q wordpress-seo.zip;
mv wordpress-seo/ wp-content/plugins/
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#
#   Remove default WP plugins
#   ------------------
#
echo "Removing default WordPress plugins..."
rm -rf wp-content/plugins/akismet
rm -rf wp-content/plugins/hello.php
echo "${GREEN}Done! ✅${NC}"
printf '\n'

#
#   Remove older WordPress default themes
#   ------------------
#
echo "Removing default WordPress themes..."
rm -rf wp-content/themes/twentyfifteen
rm -rf wp-content/themes/twentysixteen
rm -rf wp-content/themes/twentyseventeen
rm -rf wp-content/themes/twentynineteen
rm -rf wp-content/themes/twentytwenty
echo "${GREEN}Done! ✅${NC}"
printf '\n'


# Set up Apache virtual host
sudo echo "<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName ${WP_DOMAIN}
    ServerAlias www.${WP_DOMAIN}
    DocumentRoot /var/www/html/
    <Directory /var/www/html/>
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog \${APACHE_LOG_DIR}/${WP_DOMAIN}-error.log
    CustomLog \${APACHE_LOG_DIR}/${WP_DOMAIN}-access.log combined
</VirtualHost>" | sudo tee /etc/apache2/sites-available/${WP_DOMAIN}.conf > /dev/null
sudo a2ensite ${WP_DOMAIN}
sudo a2enmod rewrite
sudo systemctl restart apache2

# Create admin user
sudo wp user create ${WP_ADMIN_USER} admin@${WP_DOMAIN} --role=administrator --user_pass=${WP_ADMIN_PASS} --display_name=${WP_ADMIN_USER}
#remove zip file
rm latest.tar.gz
#remove bash script
rm wp-install.sh
fi

THE URL OF THE error ---- Screenshot by Lightshot

I took a quick glance, and I can see the “fi” on the last line doesn’t appear to be necessary.

For the two rm statements at the end, I recommend adding an if statement around those to delete those files only if they’re present.

I didn’t take a very long look as of yet but I think fixing those things might be a good place to start.