003 scripting

14

Click here to load reader

Transcript of 003 scripting

Page 1: 003 scripting

Scripting

Creating and Running Scripts Basic Script Architecture echo Variables read Conditional statements Loops Examples

AGENDA

SHATRIX

Page 2: 003 scripting

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

Page 3: 003 scripting

Basic Script Architecure

#!(shell-path) variables definition read variables from user loops conditional statements print output to terminal pipe & redirection to other files

#!/bin/bash echo ”Welcome” read var1 echo $var1

Page 4: 003 scripting

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”

Page 5: 003 scripting

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

Page 6: 003 scripting

read

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

read  ­p  ”Enter your name”  nameecho  ”Your name is $name”

Page 7: 003 scripting

Conditional Statements

if statement

if [ condition ]

 then

   things to do

 elif [ another­cond. ]

 then

   things to do

 else

   things to do

fi

#!/bin/bashx=5if [ $x = 5 ]then

echo ”right”else

echo ”wrong”fi

Page 8: 003 scripting

Conditional Statements

case statement

case $variable­name in

   value_1)

      things to do;;

   value_2)

      things to do;;

   *)

      default action;;

esac

#!/bin/bashx=5case $x in4)echo ”x=4”;;5)echo ”x=5”echo ”ok”;;*)echo ”I don't know”;;esac

Page 9: 003 scripting

Loops

for loop

for  VARIABLE  in  ARRAY

do

    things on each value

done

#!/bin/bashfor i in 1 2 3 4 5do

echo ”current value is $i”done

Page 10: 003 scripting

Loops

while loop

while  [ condition ]

do

    things to do

done

equal (=)

not equal (!=)

Less than (­lt)

Greater than (­gt)

#!/bin/bashVAR=0while [ $VAR ­lt 3 ]; do   echo $VAR   VAR=$[$VAR+1]done

Page 11: 003 scripting

Examples (User Login)

#!/bin/bashuser_name="shatrix"password="13"read ­p "User Name: " login_namestty ­echoread ­p "Password: " login_passstty echoecho ""if [ $login_name = $user_name ];  then

if [ $login_pass = $password ];  thenecho "Now you are logged in....."

elseecho "Error, wrong password, try again"

fielse

echo "Error, user name doesn't exist"fi

Page 12: 003 scripting

Examples (Calculator)

#!/bin/bashread ­p "Enter First Number: " f_numread ­p "Enter Second Number: " s_numread ­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

Page 13: 003 scripting

Examples (Modified Calculator)

#!/bin/bashread ­p "Enter First Number: " f_numread ­p "Enter Second Number: " s_numread ­p "Enter Operation: " opcase $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

Page 14: 003 scripting

Examples (PhoneBook)