| My Recent Shell Scripting Posted by: John in Linux on Aug 15, 2008 |
Recently I have been working on some shell scripts to aid in the installation of applications. One of the scripts I have been working on is a LAMP (linux, apache, mysql, and php) installer. For almost two years I ran a local WAMP server (same as LAMP but the operating system is Windows) which was installed by clicking some executable (XAMPP / VertigoServ). However, Linux shows no sympathy for lazy Windows users, and thus forces us to use a restrictive package manager or install form source. Moreover, since I often need to recompile php to add new modules, I have decided to make my own LAMP installer. Below are some snippets of code that I have so far found useful in my shell script.
Determine if a user is root:
Determine if a directory exists:
Pass arguments to the shell script:

Here you are passing the script the argument “php.” This would cause the script to only run the php function.
Determine if a user is root:

- if [ "$(whoami)" != "root" ]; then
- echo "Sorry, you are not root."
- exit 1
- fi
if [ "$(whoami)" != "root" ]; then
echo "Sorry, you are not root."
exit 1
fi
echo "Sorry, you are not root."
exit 1
fi
Determine if a directory exists:

- if [ ! -e /usr/local/src ]; then
- mkdir /usr/local/src
- fi
if [ ! -e /usr/local/src ]; then
mkdir /usr/local/src
fi
mkdir /usr/local/src
fi
Pass arguments to the shell script:

- if [ "$1" == "apache" ]; then
- apache
- elif [ "$1" == "php" ]; then
- php
- else
- apache
- php
- fi
if [ "$1" == "apache" ]; then
apache
elif [ "$1" == "php" ]; then
php
else
apache
php
fi
apache
elif [ "$1" == "php" ]; then
php
else
apache
php
fi
john@pluto:~#./myscript php
Here you are passing the script the argument “php.” This would cause the script to only run the php function.