0 Replies - 599 Views - Last Post: 24 September 2019 - 02:38 PM Rate Topic: -----

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 339
  • Joined: 13-March 18

reviewing my bash script for improvements.

Posted 24 September 2019 - 02:38 PM

I have made my first bash script that would help me manage my web server better.

HEre is the entire list of code.

#!/bin/bash
clear
echo "||======Web Server Manager======||"
echo "||[Status]Software	Version	||"
value=$( dpkg -l | grep -c nginx ) # check for nginx installation. if greater than 0 then its installed.
if [ $value -gt 0 ]; then
	msg="[\e[32m PASS \e[37m]nginx"
	u=$(nginx -v 2>&1)
	var="${u:21:9}"
	echo -e "||$msg		${var}	||"
else
	echo -e "||[\e[31m FAIL \e[37m]nginx"
	echo "||RECOMMENDED [y/n]"
	read instwebserv
	if [ $instwebserv == 'y' ]; then
		apt-get install nginx
	else
		echo "||Nginx Web Server is the only supported software for this script."
	fi
fi
value=$( dpkg -l | grep -c php ) # check for php installation. if greater than 0 then its installed.
if [ $value -gt 0 ]; then
	msg="[\e[32m PASS \e[37m]PHP"
	u=$(php --version 2>&1)
	var="${u:4:8}"
	echo -e "||$msg		${var}||"
else
	echo -e "||[\e[31m FAIL \e[37m]PHP"
	echo "||RECOMMENDED [y/n]"
	read instwebserv
	if [ $instwebserv == 'y' ]; then
		apt-get install php-fpm php-mysql
	else
		echo "||PHP is strongly recommended for this script to function properly."
	fi
fi
value=$( dpkg -l | grep -c phpmyadmin ) # check for php installation. if greater than 0 then its installed.
if [ $value -gt 0 ]; then
	msg="[\e[32m PASS \e[37m]PHPma"
	u=$(dpkg -l | grep phpmyadmin 2>&1)
	var="${u:36:7}"
	echo -e "||$msg		${var}	||"
else
	echo -e "||[\e[31m FAIL \e[37m]PHPma"
	echo "||RECOMMENDED [y/n]"
	read instwebserv
	if [ $instwebserv == 'y' ]; then
		apt-get install phpmyadmin
	else
		echo "||PHPMyAdmin is recommended for easier database management."
	fi
fi
value=$( dpkg -l | grep -c mysql ) # check for mysql installation. if greater than 0 then its installed.
if [ $value -gt 0 ]; then
	msg="[\e[32m PASS \e[37m]MySQL"
	u=$(mysql --version 2>&1)
	var="${u:11:5}"
	echo -e "||$msg		$var	||"
else
	echo -e "||[\e[31m FAIL \e[37m]MySQL"
	echo "||RECOMMENDED [y/n]"
	read instwebserv
	if [ $ instwebserv == 'y' ]; then
		apt-get install mysql-server
		echo "||Do you want to secure this mysql installation? [y/n]"
		read secure_install
		if [ $secure_install == 'y' ]; then
			mysql_secure_installation
		fi
	else
		echo "||MySQL is STRONGLY recommended. Please install to ensure proper execution."
	fi
fi
cd /home/
cmd=$(ls -d */ | cut -d '/' -f 1 2>&1)
echo "||				||"
echo "||User		WWW	MySQL	||"
for i in $cmd; do
	#echo "Value for" $i
	if [ -z "$i" ]; then
		www="\e[31mNo\e[37m"
	else
		www="\e[32mYES\e[37m"
	fi
	query=$(mysql -s -N -uroot -ppass mysql -e "SELECT count(*) FROM user WHERE user='$i';")
	if [[ $query > '0' ]] ; then
		sql="\e[32mYES\e[37m"
	else
		sql="\e[31mNo\e[37m"
	fi
	echo -e "||$i		$www	$sql	||"
done
echo "||				||"
echo "||	Options			||"
echo "||1) Create user.		||"
echo "||2) Delete user.		||"
read -s option
case $option in
	1) echo "||				||"
	echo "||Put new user(8-12)		||"
	read -s newuser
	size=${#newuser}
	if [[ $size -ge 8 && $size -le 12 ]]; then
		echo "||Confirm? (y/n)		||"
		echo "||$newuser			||"
		read -s newuserconfirm
		if [ $newuserconfirm == 'y' ]; then
			useradd -m $newuser
			echo "Added, DB name?"
			read -s dbname
			mysql -s -N -uroot -ppass mysql -e "GRANT ALL PRIVILEGES ON $dbname.* TO '$newuser'@'localhost';"
			mysql -s -N -uroot -ppass mysql -e "FLUSH PRIVILEGES;"
		fi
	else
		echo "User must be 8-12"
	fi
	;;
	2) echo "||				||"
	echo "||Enter user name		||"
	read -s username
	echo "||Confirm: $username?	(y/n)	||"
	read -s deluserconfirm
	if [ $deluserconfirm == 'y' ]; then
		userdel -r -f $username
		mysql -s -N -uroot -ppass mysql -e "DROP USER '$username'@'localhost';"
		#Need to figure out a good way to delete the databases.
		mysql -s -N -uroot -ppass mysql -e "FLUSH PRIVILEGES;"
		echo "User deleted from directory and mysql user."
	fi
	;;
	*) echo "catch all"
	exit 1
	;;
esac



I have a few things to ask.

1. How can i loop this program until im done with it or exit with a command.
2. For handling deleting users is this the correct way to handle deleting a user? Because i get a mail spool error.
3. Any suggestions for improvements is welcomed.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1