Skip to main content
deleted 233 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 237

I am writing a bash script to process input from html form. I have two issues which I can't seem to be able to figure out. I am a beginner to Unix.

I've googled, searched and tried many different examples but I am not able to find a solution.

If someone can guide me on howneed to properly implement a command into if statement condition and write a if statement with multiple conditions, I would very much appreciate it.

I am writing a bash script to process input from html form. I have two issues which I can't seem to be able to figure out. I am a beginner to Unix.

I've googled, searched and tried many different examples but I am not able to find a solution.

If someone can guide me on how to properly implement command into if statement condition and write if statement with multiple conditions, I would very much appreciate it.

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

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

added 505 characters in body
Source Link
James
  • 21
  • 3

I am writing a bash script forto process input from html form. I have two issues which I can't seem to be able to figure out. I am a beginner to Unix.

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!"

I am writing a bash script for html form. I have two issues which I can't seem to be able to figure out. I am a beginner to Unix.

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.

I am writing a bash script to process input from html form. I have two issues which I can't seem to be able to figure out. I am a beginner to Unix.

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!"

Source Link
James
  • 21
  • 3

How to write bash script while using command as condition in if statement?

I am writing a bash script for html form. I have two issues which I can't seem to be able to figure out. I am a beginner to Unix.

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.

#! /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've googled, searched and tried many different examples but I am not able to find a solution.

If someone can guide me on how to properly implement command into if statement condition and write if statement with multiple conditions, I would very much appreciate it.