Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a BASH scripting question. I'd like the mention that I am new to BASH scripting; however, I am not new to programming. 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

share|improve this question
    
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

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
    
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. –  sigmavirus24 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! –  sigmavirus24 Jan 27 '13 at 21:13
    
Thank you @sigmavirus24! –  djthoms Jan 28 '13 at 8:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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