Bash Shortcuts

Updated on June 26th, 2021 at 1:10 am

What is it?

When I was using my Mac Book for programming server-side applications, I used the Apache webserver that was built into macOS to run them. This meant that I needed to know all of the Unix based commands to run and manage the server. After using the commands for a while, I learned about using aliases in the Bash Profile file and eventually, wrote shortcut aliases for all of the commands I needed to run Apache. I didn’t stop there though. I soon created aliases for MySQL and even shortcuts to edit the Bash Profile file. I also wanted to make it possible to change what program I used to edit the files (such as VIM, Nano, or TextEdit), so functionality for that was soon implemented.

Eventually, I decided that I should reorganize the contents of the Bash Profile file in order to make the script cleaner. The result was several large tree-like functions of nested conditions. Commands like starting or shutting down the Apache server could now be done with simple commands like apache start or apache stop. This greatly sped up my workflow when working with my web and database servers.

screenshot of bash shortcuts console
A screenshot of the console when typing “serverhelp”

What does it do?

What started as a few simple aliases eventually lead to several functions with nested conditions. Most of the commands revolve around the embedded web server in my computer as well as the MySQL database. These somewhat tedius commands were soon replaced with smaller and simpler commands.

For the bashprofile file, typing the command bashprofile would open the file with the VIM editor. Adding a flag would tell the script which text utility to open the the file in, such as -v for VIM, -n for Nano, and -t for TextEdit.

For the Apache web server, all of the commands start with apache followed by a command and sometimes a flag. apache start and apache stop start and stop the server (If the -f flag is used, it will do it forcibly). apache followed by restart, status, version, and test will restart Apache, show the status of Apache, the version of Apache, or test the config files respectively. apache edit followed by a flag will edit the httpd.conf file in a text editor, apache hosts followed by a flag will edit the the hosts file in a text editor, and apache hosteidit followed by a flag will edit the httpd-vhosts.conf file in a text editor.

For MySQL, the command mysql followed by either start, stop, or restart will turn on, turn off, or restart the server respectively.

Lastly, serverssh aws was used as a shortcut for connecting to my remote EC2 Server at one of the AWS data centers.

How does it work?

Normally, the BashRC and Bash Profile files in the user home directory (on a Unix based system) are designed for startup configurations for the bash shell. They may hold some aliases for ease of use, but I went a step further and created several multi-conditional functions to access the various aliases that I have created to manage some of the web services on my computer. The file is shown below:

# Bash Help Message
BASHHELP="\nbashprofile\t\t\t\tOpen ./bash_profile in vim"
BASHHELP="${BASHHELP}\n\tbashprofile -v\t\t\tOpen ./bash_profile in vim"
BASHHELP="${BASHHELP}\n\tbashprofile -n\t\t\tOpen ./bash_profile in nano"
BASHHELP="${BASHHELP}\n\tbashprofile -t\t\t\tOpen ./bash_profile in TextEdit"
BASHHELP="${BASHHELP}\nbashprofile save\t\t\tRefresh ./bash_profile\n"

# Apache Help Message
APACHEHELP="\napache start\t\t\t\tStart Apache server"

APACHEHELP="${APACHEHELP}\napache stop\t\t\t\tStop Apache server"
APACHEHELP="${APACHEHELP}\n\tapache stop -f\t\t\tForce Stop Apache server"

APACHEHELP="${APACHEHELP}\napache restart\t\t\t\tRestart Apache server"
APACHEHELP="${APACHEHELP}\n\tapache restart -f\t\tForce Restart Apache server"

APACHEHELP="${APACHEHELP}\napache status\t\t\t\tShow status of Apache server"
APACHEHELP="${APACHEHELP}\napache version\t\t\t\tShow version of Apache server"
APACHEHELP="${APACHEHELP}\napache test\t\t\t\tTest config files in Apache server"

APACHEHELP="${APACHEHELP}\napache edit\t\t\t\tEdit httpd.conf in vim"
APACHEHELP="${APACHEHELP}\n\tapache edit -v\t\t\tEdit httpd.conf in vim"
APACHEHELP="${APACHEHELP}\n\tapache edit -n\t\t\tEdit httpd.conf in nano"
APACHEHELP="${APACHEHELP}\n\tapache edit -t\t\t\tEdit httpd.conf in TextEdit"

APACHEHELP="${APACHEHELP}\napache hosts\t\t\t\tEdit hosts file in vim"
APACHEHELP="${APACHEHELP}\n\tapache hosts -v\t\t\tEdit hosts file in vim"
APACHEHELP="${APACHEHELP}\n\tapache hosts -n\t\t\tEdit hosts file in nano"
APACHEHELP="${APACHEHELP}\n\tapache hosts -t\t\t\tEdit hosts file in TextEdit"

APACHEHELP="${APACHEHELP}\napache hostsedit\t\t\tEdit httpd-vhosts.conf in vim"
APACHEHELP="${APACHEHELP}\n\tapache hostsedit -v\t\tEdit httpd-vhosts.conf in vim"
APACHEHELP="${APACHEHELP}\n\tapache hostsedit -n\t\tEdit httpd-vhosts.conf in nano"
APACHEHELP="${APACHEHELP}\n\tapache hostsedit -t\t\tEdit httpd-vhosts.conf in TextEdit\n"

# MySQL Help Message
MYSQLHELP="\nmysql\t\t\t\t\tOpen MySQL console"
MYSQLHELP="${MYSQLHELP}\nmysql start\t\t\t\tStart MySQL server"
MYSQLHELP="${MYSQLHELP}\nmysql stop\t\t\t\tStop MySQL server"
MYSQLHELP="${MYSQLHELP}\nmysql restart\t\t\t\tRestart MySQL server\n"

# ServerSSH Help Message
SERVERSSHHELP="\nserverssh aws\t\t\t\tStart SSH for AWS Remote Server\n"

function serverhelp() {
  echo -e "\n--Bash Profile--"
  echo -e $BASHHELP
  
  echo -e "\n--Apache--"
  echo -e $APACHEHELP

  echo -e "\n--MySQL--"
  echo -e $MYSQLHELP

  echo -e "\n--ServerSSH--"
  echo -e $SERVERSSHHELP
}

# BashProfile
function bashprofile() {
  if [ $# -eq 0 ]; then
    sudo vim /Users/Ryan/.bash_profile
  else
    case "$1" in
      -v)
        sudo vim /Users/Ryan/.bash_profile
        ;;
      -n)
        sudo nano /Users/Ryan/.bash_profile
        ;;
      -t)
        sudo open -a "TextEdit" /Users/Ryan/.bash_profile
        ;;
      save)
        source /Users/Ryan/.bash_profile
        ;;
      help)
        echo -e $BASHHELP
        ;;
      *)
      echo "Unkown flag: $1"
    esac
  fi
}

# Apache
function apache() {
  if [ $# -eq 0 ]; then
    echo "Missing server command"
  else
    case "$1" in
      start)
        sudo apachectl start
        ;;
      stop)
        if [ $# -eq 2 ]; then
          case "$2" in
            -f)
              sudo apachectl stop
              ;;
            *)
              echo "Unknown flag: $2"
          esac
        else
          sudo apachectl graceful-stop
        fi
        ;;
      restart)
        if [ $# -eq 2 ]; then
          case "$2" in
            -f)
              sudo apachectl restart
              ;;
            *)
              echo "Unknown flag: $2"
          esac
        else
          sudo apachectl graceful
        fi
        ;;
      status)
        sudo apachectl status
        ;;
      version)
        httpd -v
        ;;
      test)
        apachectl configtest
        ;;
      edit)
        if [ $# -eq 2 ]; then
          case "$2" in
            -v)
              sudo vim /etc/apache2/httpd.conf
              ;;
            -n)
              sudo nano /etc/apache2/httpd.conf
              ;;
            -t)
              sudo open -a "TextEdit" /etc/apache2/httpd.conf
              ;;
            *)
              echo "Unkown flag: $2"
          esac
        else
          sudo vim /etc/apache2/httpd.conf
        fi
        ;;
      hosts)
        if [ $# -eq 2 ]; then
          case "$2" in
            -v)
              sudo vim /etc/hosts
              ;;
            -n)
              sudo nano /etc/hosts
              ;;
            -t)
              sudo open -a "TextEdit" /etc/hosts
              ;;
            *)
              echo "Unkown flag: $2"
          esac
        else
          sudo vim /etc/hosts
        fi
        ;;
      hostsedit)
        if [ $# -eq 2 ]; then
          case "$2" in
            -v)
              sudo vim /etc/apache2/extra/httpd-vhosts.conf
              ;;
            -n)
              sudo nano /etc/apache2/extra/httpd-vhosts.conf
              ;;
            -t)
              sudo open -a "TextEdit" /etc/apache2/extra/httpd-vhosts.conf
              ;;
            *)
              echo "Unkown flag: $2"
          esac
        else
          sudo vim /etc/apache2/extra/httpd-vhosts.conf
        fi
        ;;
      help)
        echo -e $APACHEHELP
        ;;
      *)
        echo "Unkown server command: $1"
    esac
  fi
}

# MySQL
function mysql() {
  path="/usr/local/mysql/support-files/mysql.server"
  if [ $# -eq 0 ]; then
    /usr/local/mysql/bin/mysql -u root -p
  else
    case "$1" in
      start)
        sudo $path start
        ;;
      stop)
        sudo $path stop
        ;;
      restart)
        sudo $path restart
        ;;
      help)
        echo -e $MYSQLHELP
        ;;
      *)
        echo "Unkown server command"
    esac
  fi
}

# Remote Servers
function serverssh() {

  # Amazon Personal Server
  host1="bitnami@ip-address"
  keyPath1="/Path/To/Certificates/"
  key1="key.pem"

  if [ $# -eq 0 ]; then
    echo "No server selected"
  else
    case "$1" in
      aws)
        sudo ssh $host1 -i $keyPath1$key1
        ;;
      help)
        echo -e $SERVERSSHHELP
        ;;
      *)
        echo "Unknown server: $1"
    esac
  fi
}