Hacking shell scripts
Using sh to install database applications
#!/bin/sh
# PHP Application install script.
# This application was originally written as a PHP script
# I decided that a shell script would be alot more useful
#
# AddText hacked from msec
AddText() {
string=$1
file=$2
quiet=$3
if [[ -z ${string} ]]; then
return;
fi
if [[ -z ${quiet} ]]; then
echo "Creating Database config: ${file}..."
fi
if ! grep -Eqx "^${string}" ${file}; then
echo -e "${string}" >> ${file};
fi
if [[ -z ${3} ]]; then
echo -e "done the file was written to incs/${file}.\n"
fi
}
clear
echo "You have executed the PHP Application installation shell script.";
echo "This script will ask you various questions about how you may want to
install"
echo "the PHP application that has it included in its distribution."
echo ""
echo ""
echo ""
appname=MyPHPApplication
echo "Are you sure you want to install ${appname}"
echo -n "$HOSTNAME > n/y "
read installapp
if [[ ${installapp} != n ]]; then
if [[ -f dbconnect.php ]]; then
echo "Lets install ${appname}"
else
echo "Lets install ${appname}"
touch dbconnect.php
touch dbconnect.txt
fi
echo "Is the MySQL database on this system ?"
echo -n "yes/DNS or IP: "
read hostname
defaulthost=localhost
if [[ ${hostname} != yes && ${hostname} != no ]]; then
AddText "<?php" dbconnect.php quiet
AddText "\$hostname = \"${defaulthost}\";" dbconnect.php quiet
AddText "thostname=\"${defaulthost}\";" dbconnect.txt quiet
else
AddText "<?php" dbconnect.php quiet
AddText "\$hostname = \"${defaulthost}\";" dbconnect.php quiet
AddText "thostname=\"${defaulthost}\";" dbconnect.txt quiet
fi
echo "Would you like to use the default database ?"
echo -n "yes/dbname: "
read answer
default=yourdb
if [[ ${answer} != yes && ${answer} != no ]]; then
AddText "\$dbname = \"${answer}\";" dbconnect.php quiet
AddText "tdbname=\"${answer}\";" dbconnect.txt quiet
else
AddText "\$dbname = \"${default}\";" dbconnect.php quiet
AddText "tdbname=\"${default}\";" dbconnect.txt quiet
fi
echo "Enter UserID for database."
echo -n "userid/root: "
read username
defaultuser=root
if [[ ${username} != root && ${answer} != yes ]]; then
AddText "\$dbuser = \"${username}\";" dbconnect.php quiet
AddText "tdbuser=\"${username}\";" dbconnect.txt quiet
name=${dbuser}
else
AddText "\$dbuser = \"${defaultuser}\";" dbconnect.php quiet
AddText "tdbuser=\"${defaultuser}\";" dbconnect.txt quiet
name=${dbuser}
fi
echo "Enter password UserID."
echo -n "password: "
read password
if [[ ${password} != no && ${answer} != yes ]]; then
AddText "\$dbpasswd = \"${password}\";" dbconnect.php quiet
AddText "tdbpasswd=\"${password}\";" dbconnect.txt quiet
AddText "?>" dbconnect.php
else
AddText "\$dbpasswd = \"\";" dbconnect.php quiet
AddText "tdbpasswd=\"\";" dbconnect.txt quiet
AddText "?>" dbconnect.php
fi
if [[ -f dbconnect.txt ]]; then
. ./dbconnect.txt
echo "mysql -e 'CREATE DATABASE ${tdbname}'"
echo "mysql -h${thostname} -u${tdbuser} -p${tdbpasswd} ${tdbname} < dbsql.mysql"
if [[ ${thostname} == localhost ]]; then
echo "mysql -e 'GRANT SELECT,INSERT,UPDATE,DELETE on ${tdbname}.* TO ${tdbuser}@${thostname} IDENTIFIED by '${tdbpasswd}''";
else
echo "mysql -e 'GRANT SELECT,INSERT,UPDATE,DELETE on ${tdbname}.* TO ${tdbuser}@'${thostname}' IDENTIFIED by '${tdbpasswd}''";
fi
else
echo "file does not exist"
fi
else
clear
echo "Try again later..."
fi
| |