Difference between revisions of "Trillex bash 1"

From Teknologisk videncenter
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
== Bash Commands ==
 
== Bash Commands ==
 
== Bash Scripts ==
 
== Bash Scripts ==
=== Program for greeting users. Doesn't work ===
+
=== Program for greeting users (Advanced - can be made better) ===
  
 
  #!/bin/bash
 
  #!/bin/bash
Line 20: Line 20:
 
         then
 
         then
 
           echo -e "You are early."
 
           echo -e "You are early."
             else if test ($HOUR -lt 11) -a ($MINUTE -lt 59)
+
             else if test $HOUR -lt 11 -a $MINUTE -lt 59
 
               then
 
               then
 
                 echo -e "Good Morning."
 
                 echo -e "Good Morning."
                       else if test ($HOUR -lt 15) -a ($MINUTE -lt 59)
+
                       else if test $HOUR -lt 11 -a $MINUTE -lt 59
 
                         then
 
                         then
 
                           echo -e "Good afternoon."
 
                           echo -e "Good afternoon."
Line 57: Line 57:
 
   then echo -e "Please go home."     
 
   then echo -e "Please go home."     
 
  fi
 
  fi
 +
 +
=== Case Script ===
 +
 +
#!/bin/bash
 +
# Read the username from the terminal
 +
clear
 +
echo -en "Enter username: "
 +
read USERNAME
 +
# Read the password from the terminal not echoing the characters
 +
echo -en "Enter password: "
 +
stty -echo
 +
read PASSWORD
 +
stty echo
 +
clear
 +
# Find the username in the case and check password
 +
case $USERNAME in
 +
        john|John) if test $PASSWORD = "banana"
 +
                then
 +
                        echo "Access Granted"
 +
                else
 +
                        echo "Access Denied...."
 +
                        exit
 +
                fi
 +
                ;;
 +
        eve|Eve) if test $PASSWORD = "apple"
 +
                then
 +
                        echo "Access Granted"
 +
                else
 +
                        echo "Access Denied...."
 +
                        exit
 +
                fi
 +
                ;;
 +
        *)      echo "Access Denied...."
 +
                exit
 +
                ;;
 +
esac
 +
echo "The secret information is 123123123"

Latest revision as of 11:21, 12 February 2009

Bash Scripts and Commands

Bash Commands

Bash Scripts

Program for greeting users (Advanced - can be made better)

#!/bin/bash
clear
echo -e "Welcome to my new bash-script"
echo -e "============================="
echo -e "\n\n"   # \n is the new-line command. So there will be two empty lines here
echo -e "Rune Nordblom\nPRC-Data, Viborg\nDenmark"
echo -e "The time is `date +%H:%M`."
HOUR=`date +%H`
MINUTE=`date +%M`
if test $HOUR -le 7
 then
   echo -e "Please go to sleep."
     else if test $HOUR -le 8
       then
         echo -e "You are early."
           else if test $HOUR -lt 11 -a $MINUTE -lt 59
             then
               echo -e "Good Morning."
                     else if test $HOUR -lt 11 -a $MINUTE -lt 59
                       then
                         echo -e "Good afternoon."
                           else if test $HOUR -gt 16
                            then
                              echo -e "Please go home."     
                                fi
                           fi
                 fi
          fi
fi

Same As Above But Less Advanced and Working

#!/bin/bash
clear
echo -e "Welcome to my new bash-script"
echo -e "============================="
echo -e "\n\n"   # \n is the new-line command. So there will be two empty lines here
echo -e "Rune Nordblom\nPRC-Data, Viborg\nDenmark"
echo -e "The time is `date +%H:%M`."
HOUR=`date +%H`
MINUTE=`date +%M` 
if test $HOUR -lt 7
 then echo -e "Please go to sleep."
 elif test $HOUR -lt 8
 then echo -e "You are early."
 elif test $HOUR -lt 11
 then echo -e "Good Morning."
 elif test $HOUR -lt 15
 then echo -e "Good afternoon."
 elif test $HOUR -gt 16
 then echo -e "Please go home."     
fi

Case Script

#!/bin/bash
# Read the username from the terminal
clear
echo -en "Enter username: "
read USERNAME
# Read the password from the terminal not echoing the characters
echo -en "Enter password: "
stty -echo
read PASSWORD
stty echo
clear
# Find the username in the case and check password
case $USERNAME in
       john|John) if test $PASSWORD = "banana"
               then
                       echo "Access Granted"
               else
                       echo "Access Denied...."
                       exit
               fi
               ;;
       eve|Eve) if test $PASSWORD = "apple"
               then
                       echo "Access Granted"
               else
                       echo "Access Denied...."
                       exit
               fi
               ;;
       *)      echo "Access Denied...."
               exit
               ;;
esac
echo "The secret information is 123123123"