003 scripting
Click here to load reader
-
Upload
sherif-mousa -
Category
Documents
-
view
612 -
download
0
Transcript of 003 scripting
Scripting
Creating and Running Scripts Basic Script Architecture echo Variables read Conditional statements Loops Examples
AGENDA
SHATRIX
Creating and Running Scripts
Scripts are normal text files with x permission touch scriptname chmod 777 scriptname chmod u+x scriptname
To run a script: full path ./scriptname sh scriptname bash scriptname
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
echo
echo displays a line of text echo ”string”
echo $variablename
echo ”string $varname”
echo ”string $varname anotherstring”
echo $(command)
echo ”Welcome Linux users”
Variables Define a variable
variablename=VALUE Print a variable to screen
echo $variablename 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 variablename read p ”string” variablename
read p ”Enter your name” nameecho ”Your name is $name”
Conditional Statements
if statement
if [ condition ]
then
things to do
elif [ anothercond. ]
then
things to do
else
things to do
fi
#!/bin/bashx=5if [ $x = 5 ]then
echo ”right”else
echo ”wrong”fi
Conditional Statements
case statement
case $variablename 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
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
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
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
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
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
Examples (PhoneBook)