A Simple Bash Script

13, Jan 2009


I just wrote this very simple bash script the other night to simplify running my local webserver. It speeds things up just a little bit, as I no longer have to type out the path to the server to start/stop it. Nonetheless, the convenience of it is undeniable.

# Allow Access to LAMPP server through sudo command
function lampp() {
    if [[ $1 == '' ]]; then
            echo 'This script must be run in one of the following manners:'
            echo 'lampp start  - start the apache server'
            echo 'lampp status - print the current status of the server'
            echo 'lampp stop   - stop the apache server'
            if [ $1 = 'start' ]; then
                    sudo /opt/lampp/lampp start
            elif [ $1 = 'status' ]; then
                    sudo /opt/lampp/lampp status
            elif [ $1 = 'stop' ]; then
                    sudo /opt/lampp/lampp stop
                    echo "The value provided is not one of the expected entries."

How to use it

For this script to work on your computer, you must already have the lampp web server installed. If it happens to be in a different directory than /opt, simply change those references in the script. You will will also have to add yourself to the sudoers file. To do so, open a terminal, and type:

su -

Your individual sudoers file may differ, but you're looking for a section similar to this:

## The COMMANDS section may have other options added to it.
## Allow root to run any commands anywhere
root            ALL=(ALL)       ALL

Add your user name below the root account, and choose to either give yourself full access to the system through the sudo command, or to limit what options your account has. Either way, once you're done, write your changes and close the sudoers file.

Next, log off as the root user, and return to your own account's home directory. I've created a new file to hold all of the bash functions that I'm writing called - predictably, .bash_functions. Go ahead and create that file, and paste the script above into it. Once that's done, save and close that file. Now, you need to edit your .bashrc file so that it is able to find the new .bash_functions file. Open .bashrc, and add the following lines to the file:

# Import user functions
if [ -f ~/.bash_functions ]; then
    . ~/.bash_functions

The .bashrc file is called during the login procedure, and this little statement tells the system to check to see if .bash_functions exists in the user's home directory, and if so to then allow access to it's content. My .bashrc file also references a separate file that I've created to hold all the aliases I've created. That file is included in the same manner as the .bash_functions file. This process helps keep my .bashrc file very clean, thereby making it easier to find what I'm looking for, should I need to edit it. It is much easier to look through smaller files than through one large one, and when the naming scheme of the files is sensible (.bash_aliases, .bash_functions, etc.), you can quickly eliminate some of them without ever looking.


This space intentionally left blank.

Have Something to Say?

Questions? Comments? Concerns? Let me know what you’re thinking.

  • You can use Markdown formatting here.