KEMBAR78
003 scripting | PDF
SHATRIX
Scripting

  A       Creating and Running Scripts
          Basic Script Architecture
  G       echo
  E       Variables
  N        read
       


          Conditional statements
  D       Loops
  A       Examples
Creating and Running Scripts

   Scripts are normal text files with x permission
       touch  script­name
       chmod  777  script­name
       chmod  u+x  script­name
   To run a script:
       full path
       ./script­name
       sh  script­name
       bash  script­name
Basic Script Architecure

   #!(shell-path)                       #!/bin/bash
        variables definition             echo ”Welcome”
                                          read var1
        read variables from user         echo $var1
        loops
        conditional statements
        print output to terminal
        pipe & redirection to other files
echo

   echo         displays a line of text
       echo  ”string”
       echo  $variable­name
       echo  ”string  $var­name”
       echo  ”string  $var­name  another­string”
       echo $(command)




           echo ”Welcome Linux users”
Variables
   Define a variable
       variable­name=VALUE
   Print a variable to screen
       echo  $variable­name
   System variables:
       Normal: has to be changed with each user
                   HOME     PWD    SHELL
       Environmental: changes with the login shell only
                   login shell with (su ­)
                   PATH
   env
read

   read       take a value from the user to a variable
       read  variable­name
       read  ­p  ”string”  variable­name


           read  ­p  ”Enter your name”  name
           echo  ”Your name is $name”
Conditional Statements
    if statement         #!/bin/bash
if [ condition ]          x=5
                          if [ $x = 5 ]
 then
                          then
   things to do              echo ”right”
 elif [ another­cond. ]   else
                             echo ”wrong”
 then                     fi
   things to do
 else
   things to do
fi
Conditional Statements

   case statement
                         #!/bin/bash
case $variable­name in   x=5
   value_1)              case $x in
      things to do;;     4)
                         echo ”x=4”;;
   value_2)              5)
      things to do;;     echo ”x=5”
                         echo ”ok”;;
   *)
                         *)
      default action;;   echo ”I don't know”;;
esac                     esac
Loops

    for loop
for  VARIABLE  in  ARRAY
do
    things on each value
done
                #!/bin/bash
                for i in 1 2 3 4 5
                do
                   echo ”current value is $i”
                done
Loops
    while loop
while  [ condition ]
do
                          #!/bin/bash
    things to do
                          VAR=0
done                      while [ $VAR ­lt 3 ]; do
                             echo $VAR
                             VAR=$[$VAR+1]
    equal (=)            done
    not equal (!=)
    Less than (­lt)
    Greater than (­gt)
Examples (User Login)
  #!/bin/bash
  user_name="shatrix"
  password="13"
  read ­p "User Name: " login_name
  stty ­echo
  read ­p "Password: " login_pass
  stty echo
  echo ""
  if [ $login_name = $user_name ];  then
     if [ $login_pass = $password ];  then
        echo "Now you are logged in....."
     else
        echo "Error, wrong password, try again"
     fi
  else
     echo "Error, user name doesn't exist"
  fi
Examples (Calculator)
  #!/bin/bash
  read ­p "Enter First Number: " f_num
  read ­p "Enter Second Number: " s_num
  read ­p "Enter Operation: " op

  case $op in
  +)
      echo "$f_num + $s_num = $[$f_num+$s_num]";;
  ­)
      echo "$f_num ­ $s_num = $[$f_num­$s_num]";;
  x)
      echo "$f_num x $s_num = $[$f_num*$s_num]";;
  /)
      echo "$f_num / $s_num = $[$f_num/$s_num]";;
  esac
Examples (Modified Calculator)
    #!/bin/bash
    read ­p "Enter First Number: " f_num
    read ­p "Enter Second Number: " s_num
    read ­p "Enter Operation: " op
    case $op in
    +)
        echo "$f_num + $s_num = $[$f_num+$s_num]";;
    ­)
        echo "$f_num ­ $s_num = $[$f_num­$s_num]";;
    x)
        echo "$f_num x $s_num = $[$f_num*$s_num]";;
    /)
        if [ $s_num != 0 ];  then
          echo "$f_num / $s_num = $[$f_num/$s_num]"
        else
          echo "Error, division by zero"
        fi;;
    *)
        echo "Wrong operation, please try again !!!"
    esac
Examples (PhoneBook)

003 scripting

  • 1.
    SHATRIX Scripting A  Creating and Running Scripts  Basic Script Architecture G  echo E  Variables N read   Conditional statements D  Loops A  Examples
  • 2.
    Creating and RunningScripts  Scripts are normal text files with x permission  touch  script­name  chmod  777  script­name  chmod  u+x  script­name  To run a script:  full path  ./script­name  sh  script­name  bash  script­name
  • 3.
    Basic Script Architecure  #!(shell-path) #!/bin/bash  variables definition  echo ”Welcome”  read var1  read variables from user  echo $var1  loops  conditional statements  print output to terminal  pipe & redirection to other files
  • 4.
    echo  echo displays a line of text  echo  ”string”  echo  $variable­name  echo  ”string  $var­name”  echo  ”string  $var­name  another­string”  echo $(command) echo ”Welcome Linux users”
  • 5.
    Variables  Define a variable  variable­name=VALUE  Print a variable to screen  echo  $variable­name  System variables:  Normal: has to be changed with each user  HOME     PWD    SHELL  Environmental: changes with the login shell only  login shell with (su ­)  PATH  env
  • 6.
    read  read take a value from the user to a variable  read  variable­name  read  ­p  ”string”  variable­name read  ­p  ”Enter your name”  name echo  ”Your name is $name”
  • 7.
    Conditional Statements  if statement #!/bin/bash if [ condition ] x=5 if [ $x = 5 ]  then then    things to do echo ”right”  elif [ another­cond. ] else echo ”wrong”  then fi    things to do  else    things to do fi
  • 8.
    Conditional Statements  case statement #!/bin/bash case $variable­name in x=5    value_1) case $x in       things to do;; 4) echo ”x=4”;;    value_2) 5)       things to do;; echo ”x=5” echo ”ok”;;    *) *)       default action;; echo ”I don't know”;; esac esac
  • 9.
    Loops  for loop for  VARIABLE  in  ARRAY do     things on each value done #!/bin/bash for i in 1 2 3 4 5 do echo ”current value is $i” done
  • 10.
    Loops  while loop while  [ condition ] do #!/bin/bash     things to do VAR=0 done while [ $VAR ­lt 3 ]; do    echo $VAR    VAR=$[$VAR+1]  equal (=) done  not equal (!=)  Less than (­lt)  Greater than (­gt)
  • 11.
    Examples (User Login) #!/bin/bash user_name="shatrix" password="13" read ­p "User Name: " login_name stty ­echo read ­p "Password: " login_pass stty echo echo "" if [ $login_name = $user_name ];  then if [ $login_pass = $password ];  then echo "Now you are logged in....." else echo "Error, wrong password, try again" fi else echo "Error, user name doesn't exist" fi
  • 12.
    Examples (Calculator) #!/bin/bash read ­p "Enter First Number: " f_num read ­p "Enter Second Number: " s_num read ­p "Enter Operation: " op case $op in +)     echo "$f_num + $s_num = $[$f_num+$s_num]";; ­)     echo "$f_num ­ $s_num = $[$f_num­$s_num]";; x)     echo "$f_num x $s_num = $[$f_num*$s_num]";; /)     echo "$f_num / $s_num = $[$f_num/$s_num]";; esac
  • 13.
    Examples (Modified Calculator) #!/bin/bash read ­p "Enter First Number: " f_num read ­p "Enter Second Number: " s_num read ­p "Enter Operation: " op case $op in +)     echo "$f_num + $s_num = $[$f_num+$s_num]";; ­)     echo "$f_num ­ $s_num = $[$f_num­$s_num]";; x)     echo "$f_num x $s_num = $[$f_num*$s_num]";; /)     if [ $s_num != 0 ];  then       echo "$f_num / $s_num = $[$f_num/$s_num]"     else       echo "Error, division by zero"     fi;; *)     echo "Wrong operation, please try again !!!" esac
  • 14.