torsdag 5 februari 2015

Linux - BASH if/then/else Example

Update verify.sh 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:

./verify.sh

You have updated verify.sh and added an else statement to existing if command to create if..else..fi 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