1

I am writing a bash script to process input from html form.

First problem is that I can't get my if statement to work while using command as condition. I am trying to check user input of middle initial and adjust it to make sure it can be combined into $answer.

Here is example of how it is suppose to process.

User inputs three different fields from html form; first name, middle initial, last name.

First name : George
Middle Initial : W.
Last name : Bush

Shell script will process and compare user input and actual answer. Actual answer is "George W. Bush"

User input:
George W. Bush = correct echo "You are correct!"
George W Bush = correct by automatically placing period after "W"
George Bush != wrong and echo "No middle initial!"
Geor W. Bus != wrong and echo "First name and last name are wrong!"

#! /bin/bash
echo Content-type: "text/html"
echo ""

read input #name variable $first, $middle, $last name

first=`user input`
middle=`user input`
last=`user input`

president=`actual answer, full name of president`
answer=`user answer, combination of $first, $middle, $last`

correctF=`actual first name from $president`
correctM=`actual middle initial from $president`
correctL=`actual last name from $president`
#cut by column from $president

First problem starts here.

if [[ $(echo $middle | wc -l) = 1 ]] ; then
    middle=`echo "$middle""."` 
    #check if middle initial variable was entered with just the letter and place a period after
elif [[ $(echo $middle | grep '.') = 'true' ]] ; then
    middle=`echo "$middle"`
    #check if middle initial variable was entered with the period and leave it as is
else
    middle=`'false'`
    #no middle initial variable was entered and is null
fi

Second problem in while loop with if statement.

I am trying to compare the user input $answer to actual answer $president and echo what is wrong with $answer. Nothing is echoed for this section.

while [ "$president" -ne "$answer" ] #not working
    do
            if [ "$first" -ne "$correctF" ]
                    then
                            echo "Wrong first name!"
            elif [ "$middle" -ne "$correctM" ]
                    then
                            echo "Wrong middle initial!"
            elif [ "$last" -ne "$correctL" ]
                    then
                            echo "Wrong last name!"
            elif [ -z "$middle" -ne "$correctM" ]
                    then
                            echo "No middle initial!"
            elif [ "$first" -ne "$correctF" ] && [ "$last" -ne "$correctL" ]
                    then
                            echo "Wrong first and last name!"
            elif [ "$first" -ne "$correctF" ] && [ "$middle" -ne "$correctM" ]
                    then
                            echo "Wrong first name and middle initial!"
            elif [ "$middle" -ne "$correctM" ] && [ "$last" -ne "$correctL" ]
                    then
                            echo "Wrong middle initial and last name!"
            elif [ "$first" -ne "$correctF" ] && [ "$middle" -ne "$correctM"] && [ "$last" -ne "$correctL" ]
                    then
                            echo "You got it all wrong!"
                    else
                            echo "You are correct!"
            fi

I need to properly implement a command into if statement condition and write a if statement with multiple conditions.

  • 1
    Please add the input you get and the result you expect. – Michael Vehrs May 18 '16 at 5:38
  • 3
    This script has so much wrong with it that I don't even know where to begin....except maybe with: if this script is meant to process input from a HTML form, then do some basic research on writing CGI scripts before you write any code – cas May 18 '16 at 5:45
  • I've included examples of how it is suppose to process and show result. It is suppose to process input from a HTML form. Sorry for the confusion. – James May 18 '16 at 6:06
  • 2
    So what happens if I enter "You suck; rm -fr /" as first name? – Henrik May 18 '16 at 6:10
  • Agree with @cas. James, if you are processing input from an HTML form using this script, you are right now laying your server wide open to total destruction. – Wildcard May 18 '16 at 6:16
1

cas says, “This script has so much wrong with it that I don't even know where to begin…”.  Well, I have some ideas on where to begin:

  • Please don’t ask us to debug your script and then post pseudo-code.  Your first code block contains backticks (`) in places where they will not work.
  • Please be more explicit.  “I can’t get my if statement to work” isn’t very helpful to us.  What happens?
  • You should always quote your shell variable references (e.g., "$middle") unless you have a good reason not to, and you’re sure you know what you’re doing.
  • $(echo "$middle" | grep '.') will equal 'true' only if "$middle" equals true.  The output of grep is the content of the line(s) from the input that  match the pattern given as a parameter.
  • . is a pattern that means “any character”, so grep '.' will match any non-blank input line.  To check for an actual period (.), use grep '\.'.  Note that this will match W. but also .W, ..., 3.14159, etc.
  • Why are you doing a while?  Do you expect that code to execute more than once?  Do you expect it to get different results when executed repeatedly on the same data?  Where’s the end of the loop?
  • -eq and -ne should be used only when comparing integers; use = and != for strings.
  • What do you mean by -z STRING1 -ne STRING2?

There may be other problems, but my eyes hurt too much to look at this any longer.

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.