shelllings

a practical way to learn shell
Log | Files | Refs | README | LICENSE

14_conditionals.sh (1267B)


      1 #!/bin/sh
      2 
      3 # As with other programming languages, we can evaluate if conditions or
      4 # statements are true or false. In this exercise we will be dealing with
      5 # strings and basic if/else structure. Below are 3 examples of this.
      6 #
      7 # $ option="yes"
      8 # $ if [ "$option" = "yes" ]; then
      9 # $		echo "option enabled"
     10 # $ fi
     11 #
     12 # $ password="shellling123"
     13 # $ if [ "$name" = "shellling123" ]; then
     14 # $		echo "access granted"
     15 # $ else
     16 # $		echo "access denied"
     17 # $ fi
     18 #
     19 # $ printf "Enter name: "
     20 # $ read name
     21 # $ if [ "$name" = "shellling" ]; then
     22 # $		echo "Welcome!"
     23 # $ elif [ "$name" = "shelllings" ]; then
     24 # $		echo "Welcome all!"
     25 # $ else
     26 # $		echo "You are not welcome here."
     27 # $ fi
     28 #
     29 # Notice that there is spaces between the square brackets and that, for
     30 # strings, there are quotation marks around the variables. There are
     31 # more options than just '='
     32 #
     33 # =     # strict string comparison of equals
     34 # !=    # strict string comparison of does not equal
     35 # -z    # checks if string is empty
     36 # -n    # checks if string is not empty
     37 # 
     38 # Fix the incomplete script below, leave all the echo lines unchanged
     39 
     40 if # check if user input is or is not empty here
     41 	if [ "$1" = "password123" ] then
     42 		echo "Welcome"
     43 	else
     44 		echo "Access not granted"
     45 else
     46 	echo "Password not entered"