Thursday, January 19, 2012

Unix Tools - How to make script program

1. How make a script file with vi :
_Form_
_Out put only

# # is a command >> My new script
echo "Hello worl !!!!"
# echo is output tool
# _from : echo "......"

_Using Variable and out value of variable

# variable_name=value
name=keovessna
echo "My name is $name!!!"
# $variable_name to show value of variable

_Read value to varibale and out put value of variable

# read variable_name
echo "Your name :"
read name
echo "Name is $name"

_More full command

# a program calculate data of function degree 2 (x1,x2)
# using -e for more keyword in echo ex. \n \t \a \c
echo -e "Function degree 2 (x1,x2)\n"
echo -e "Axˆ2 + Bx + C = 0\n"
echo "A = "
read A
echo "B = "
read B
echo "C = "
read C
# data = b*b-4*a*c
data=`expr $B \* $B - 4 \* $A \* $C`
echo -e "data = $data\n"

2.How to run script after you wrote your script, it is not set as execution permission. So you have to make it as execution permission by >>

chmod 755 script_name
_or_
chmod +x script_name

Then you can run this script with some shell script tool such as :

bash script_name
_or_
sh script_name
_or_
./script_name

No comments:

Post a Comment