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.
-
1Please add the input you get and the result you expect.Michael Vehrs– Michael Vehrs2016年05月18日 05:38:49 +00:00Commented May 18, 2016 at 5:38
-
3This 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 codecas– cas2016年05月18日 05:45:34 +00:00Commented May 18, 2016 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– James2016年05月18日 06:06:17 +00:00Commented May 18, 2016 at 6:06
-
2So what happens if I enter "You suck; rm -fr /" as first name?Henrik supports the community– Henrik supports the community2016年05月18日 06:10:57 +00:00Commented May 18, 2016 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– Wildcard2016年05月18日 06:16:26 +00:00Commented May 18, 2016 at 6:16
1 Answer 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"
equalstrue
. The output ofgrep
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", sogrep '.'
will match any non-blank input line. To check for an actual period (.
), usegrep '\.'
. Note that this will matchW.
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.