torsdag 5 februari 2015

Linux - BASH if/then/else Example

Update as follows

##!/bin/bash  read -p "Enter a password" pass  if test "$pass" = "jerry"  then       echo "Password verified."  else       echo "Access denied."	  fi

Save and close the file. Run it as follows:


You have updated and added an else statement to existing if command to create structure. If $pass (i.e. password) is equal to "jerry", then "Password verified." is displayed. However, with else statement, the script can display "Access denied." message on screen. This ensures that your script will always execute one of the code block as follows:

if command is successful  then    print "Password verified message."  else    # if condition is false    print "Access denied message."  fi


Skicka en kommentar