elifUnfortunately, there are several problems with this very (Domain and web hosting)
Monday, April 30th, 2007elifUnfortunately, there are several problems with this very simple script. It will take any answer except yesas meaning no. We can prevent this by using the elifconstruct, which allows us to add a second condi- tion to be checked when the elseportion of the ifis executed. Try It Out Doing Further Checks with an elifWe can modify our previous script so that we report an error message if the user types in anything otherthan yesor no. We do this by replacing the elsewith elifand then adding another condition. #!/bin/shecho Is it morning? Please answer yes or no read timeofdayif [ $timeofday = yes ] thenecho Good morning elif [ $timeofday = no ]; thenecho Good afternoon elseecho Sorry, $timeofday not recognized. Enter yes or no exit 1fiexit 0How It WorksThis is quite similar to the previous example, but now the elifcommand tests the variable again if thefirst ifcondition is not true. If neither of the tests is successful, an error message is printed and thescript exits with the value 1, which the caller can use in a calling program to check if the script was suc- cessful. A Problem with VariablesThis fixes the most obvious defect, but a more subtle problem is lurking. Let s try this new script, butjust press Enter (or Return on some keyboards) rather than answering the question. We ll get this errormessage: [: =: unary operator expectedWhat went wrong? The problem is in the first ifclause. When the variable timeofdaywas tested, itconsisted of a blank string. So the ifclause looks likeif [ = yes ] which isn t a valid condition. To avoid this, we must use quotes around the variable: if [ $timeofday = yes ] 36Chapter
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services