5
\$\begingroup\$

I am writing a simple script to automate my regression testing. I am new to bash scripting and my goal is achieve the most efficient and clean code. I have written a simple function to allow me to select which subject I would like to test. This will loop until I select the exit option. Again, this is quite simple; however I am looking for some feedback on how I can improve.

BIO=biology

showMenu () {
    echo "What subject would like to regression test?"
    echo "1) Physics"
    echo "2) Sociology"
    echo "3) Biology"
    echo "4) Quit"
}

while [ 0 ]
do
    showMenu
    read CHOICE
    case "$CHOICE" in

    "1")
        read SUBJECT
        SUBJECT=physics
        echo "LOG: REGRESSION TESTING $SUBJECT" $SUBJECT
        GENERATE_CSS=`lessc /home/developer/oer.exports/css/ccap-$BIO.less > /home/developer/oer.exports/css/ccap-$BIO.css`
        echo "LOG: CSS GENERATION COMPLETE"
        GENERATE_PDF=`python collectiondbk2pdf.py -v -s ccap-$BIO -d ./test-ccap/col$SUBJECT $SUBJECT.pdf -t tempdir/$SUBJECT`
        echo "LOG: GENERATING $SUBJECT PDF WITH $BIO CSS STYLE"
        ;;
    "2")
            read SUBJECT
        SUBJECT=sociology
        echo "LOG: REGRESSION TESTING SOCIOLOGY" $SUBJECT
        GENERATE_CSS=`lessc /home/developer/oer.exports/css/ccap-$BIO.less > /home/developer/oer.exports/css/ccap-$BIO.css`
        echo "LOG: CSS GENERATION COMPLETE"
        GENERATE_PDF=`python collectiondbk2pdf.py -v -s ccap-$BIO -d ./test-ccap/col$SUBJECT $SUBJECT.pdf -t tempdir/$SUBJECT`
        echo "LOG: GENERATING $SUBJECT PDF WITH $BIO CSS STYLE"
        ;;
    "3")
        echo "Biology"
        DIFF=`diffpdf -a --debug=2 /tmp/pdf/diff bio.pdf bio_slicer.pdf`
        echo "LOG: DIFF DEV vs STAGING NOW AVAILABLE FOR PDF VIEW. "
        ;;      
    "4")
        echo "Exiting now..."
        exit
        ;;

    esac
done
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$
  1. while : is a standard idiom for "loop forever" in bash. The colon (:) is a synonym for true.
  2. Prefer $(command) to `command` ("backticks"). The former allows you to nest; the latter does not.
  3. Though in your case, you don't need the backticks at all. These are for capturing the output of the command, which you don't use anywhere.
  4. It isn't clear why you have the read SUBJECT inside each case -- you throw away the value in the very next statement.
  5. Don't do this: lessc ccap-$BIO.less > ccap-$BIO.css. (It's a classic mistake in shell scripting; everyone's been bit by it.) This will nuke the file -- it will be truncated by the shell, overwriting the contents before lessc has the chance to read it.

Aside from your code: if your goal is to regenerate the output files whenever one of the input files has changed, you should consider learning make. With a 10-20 line makefile, you can automatically regenerate whichever of the output files need updating whenever one of the input files change.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for you comments and suggestions. Points 2 & 5 were particularly valuable. \$\endgroup\$ Commented Oct 18, 2012 at 15:23
2
\$\begingroup\$

Take a look at the select command. The selection loop it provides is slightly different from what you have, but take a look at the following:

PS3="What subject would you like to regression test? "
select choice in Physics Biology Sociology Quit; do
    case $choice in
      Physics)
         echo "Do your Physics code here"
         ;;
      Biology)
         echo "Do your Biology code here"
         ;;
      Sociology)
         echo "Do your Physics code here"
         ;;
      Quit)
         exit
         ;;
    esac
done
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.