A A A
logo2




Shell scripts are simple text files with commands to execute. The scripts are running by shell. All Bourne Shell scripts should begin with the sequence:

#!/bin/sh


If you want to run a script you should give it suitable rights (command chmod).



Variable



If you want to assign a number or string of characters to the variable use symbol “=”.

variable=3
zmienna=”ala has cat”


If you want to assign a string of characters to the variable put quotation marks around the string.

variable=`date`

Each value on the command line is assigned to the special variables $1, $2, $3. $0 is the name of the script. $# is a count of values on the command line.




Print on screen




If you want to print message on screen use command echo. If you would like to print variable on screen add “$”symbol before variable.

#/bin/sh
zmienna=”Date:”
zmienna2=`date`
echo $variable1 $variable2

This scripts print on screen „Date: --.--.—“



IF



If
Syntax:

if [ warunek ]
then
{
...
}
else
{
...
}
fi

In if condition we can compare two numbers :

number1 -gt number2 - number1 greates then number2
number1 -ge number2 - number1 greatest then or equal number2
number1 -eq number2 - number1 equal number2
number1 -le number2 - number1 little then or equal number2
number1 -lt number2 - number1 little then number2br />
Check or it is a file or a directory

-f filename – if filename is file
-d directoryname –if directoryname is directory
-e filename –if filename exist


Case

Case syntax:

case $variable in:
value)
command1
;;
value2)
command2
;;
*)
command3
;;
esac

Loop



For
Syntax:


for i in list
do
instrukcja
done


Example 1
for i in 123
do
echo $i
done


While
Syntax:


while [ condition ]
do
instrukcja
done



Autor: Aleksandra Przybyło
aleksandraprzybylo@gmail.com