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.