3

I am developing a simple BASH script to watch a given compass project. My script is shown below.

I would like to allow a user to optionally enter a custom path or use the default http_path value. I'm not sure how to set the path variable equal to the http_path variable where my default Apache directory is located using my current script. If any one has better suggestions and/or resources for learning how to write BASH scripts, please share!

Note: I have searched this site, Google, and stackoverflow for an answer. If you find one with a solution to my issue, please let me know!

#!/bin/bash
# This script will watch a given compass project
http_path=/var/www/

echo -n "Enter in a new path and press. To use the default path $http_path press [ENTER] twice"

while read path; do
    if [ -z "${path}" ]; then
        clear  
        # these two lines enable the script to work if I do not
        # I do not prompt for default path. It's been tested and works
        # echo "That was empty, do it again!"
        # echo -n "Enter in a compass project directory and press [ENTER]"
        echo "Are you sure you would like to use the default path? Press [ENTER] to continue..."
        $path=$http_path
    else
        echo "Checking now..."
        break
    fi
done

echo -n "Enter in a project to watch and prss [ENTER]"

while read project; do
    if [ -z "${project}" ]; then
        clear  
        echo "That was empty, do it again!"
        echo -n "Enter in a compass project name and press [ENTER]"
    else
        echo "Checking now..."
        break
    fi
done

echo "Watching" $project "project in directory" $path
echo "To exit project press Command/Control + C"

cd $path
compass watch $project

See problem solution here

  • Are you referring to DocumentRoot here? – Nils Jan 27 '13 at 20:29
  • To the document root of my apache server. /var/www/ is where all of my php scripts are located – djthoms Jan 27 '13 at 20:29
  • 1
    I would grep for it in the httpd_conf and set it automatically. – Nils Jan 27 '13 at 20:30
  • Awesome idea but it differs from dirstro to distro. Some it's in /etc/apache2 and for others it might be in /etc/httpd, any way to dynamically figure out where the httpd.conf or apache2.conf file might be? – djthoms Jan 27 '13 at 20:59
  • @djthoms, who says it has to be dynamical? Could edit the script when installing. – vonbrand Jan 28 '13 at 1:24
2

When assigning a value to path, leave off the leading $ sign in the first while loop.

You only need to do: path=$http_path.

Edit

I only realized after I posted that you also want resources to learn bash. I personally found Advanced Bash-Scripting Guide to be useful. It is a bit dated if I remember correctly, but it is more than sufficient. Also #bash on Freenode is a great place. They're not always the friendliest but they are definitely extraordinarily knowledgable.

Also, never be afraid to test out something on the command-line. That's your interactive interpreter (if you've done any ruby or python programming).

Edit 2

The actual problem was that after hitting enter the user wouldn't exit the loop so the code needed to be changed to:

while read path; do
    if [ -z "${path}" ]; then
        clear
        echo "Are you sure you would like to ..."
        read prompt
        if [ -n "${prompt}" ] ; then
            continue
        fi
        path=$http_path
        break
    else
        # etc.
    fi
done
  • Alright, good to know! It seems it is getting stuck at that point where the user would press enter again to use the default path. I can press enter for days and nothing happens. – djthoms Jan 27 '13 at 20:30
  • Oh I entirely missed that. You're doing an echo without a subsequent read. You're not checking their input and as such the while loop is just running constantly. You need to read and break after that echo. – Ian Stapleton Cordasco Jan 27 '13 at 20:31
  • The addition of the break worked wonderfully! I have been testing I just got stuck on that one part. I'm mainly a PHP developer but I have experience in Python and Java. – djthoms Jan 27 '13 at 21:06
  • 1
    @djthoms you might want to make sure that what you read in after that echo is of zero-length. If it isn't, you might want to continue to prompt them for input. Check my edited answer. Cheers! – Ian Stapleton Cordasco Jan 27 '13 at 21:13
  • Also you might want to check if the path they're providing actually exists in advance, e.g., if ! [ -d "${path}" ] ; echo "ERROR: Path does not exist" ; fi – Ian Stapleton Cordasco Jan 28 '13 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.