Unix-User-Bash-Shell
Unix-User-Bash-Shell
Email :
amine.berqia@ensias.um5.ac.ma
Unix
Bash as an example of a shell
#!/bin/bash
read -p ' Your name : ' name
echo "Hello $name"
Unix
Bash as an example of a shell
mathematical operations
In bash, variables are all strings. Bash itself is
not really capable of manipulating numbers.
#!/bin/bash
let "a = 5"
let "b = 2"
let "c = a + b"
echo $c
Unix
Bash as an example of a shell
To summarize: As in most programming languages, you can create shell variables that
temporarily store values in memory. A variable called variable is accessible by writing
$variable.
The echo command displays text or the contents of a variable on the console.
read waits for keyboard input from the user and stores the result in a variable.
You can perform mathematical operations on numbers using the let command.
Some variables are accessible everywhere, in all scripts: these are the environment
variables. They can be listed with the env command.
The parameters sent to our script (like ./script ) are passed in numbered variables: $1,
$2, $3 ... The number of parameters sent is indicated in the$# variable.
Unix
Bash as an example of a shell
control structures: if and case
if [ test ]
….
fi
You will notice - and it is very important - that there are spaces inside
the [] . We should not write [test] but [ test ]!
#!/bin/bash
if [ $1 != $2 ] then
echo " The 2 names are different!" else
echo "The 2 names are identicals!"
fi
#!/bin/bash
if [ $1 -ge 12 ] then
echo "A" else
echo "NA"
fi
Unix
Bash as an example of a shell
check if the parameter exists:
#!/bin/bash
if [ -z $1 ] then
echo " Parameter does not exist"
else
echo "Parâmetro: $1"
fi
...
Unix
Bash as an example of a shell
a script that asks the user to enter the name of a directory
and checks if it is a directory:
read -p 'Insira um diretório: ' directory -x $file: check if the file is executable
if [ -d $directory ]
then $file1 -nt $file2: check if file1 is
echo "OK" newer than file2.
else
echo "No" $file1 -ot $file2: ????
fi
……
Unix
Bash as an example of a shell
&& ||
#!/bin/bash
#!/bin/bash
if [ $1 = "Pierre" ]
then
echo "Salut Pierre !"
elif [ $1 = "Pedro" ]
then
echo "Ola Pedro"
elif [ $1 = "John" ]
then
echo "Hi
John ?"
else
echo "Who
r u!"
fi
Unix
Bash as an example of a shell
case: test multiple conditions at the
same time
#!/bin/bash
read
opt
case
$opt in
1)date
+%M
%D
%Y;;
2)date
+%D
%T ;;
Unix
Bash as an example of a shell
Loops
For, While, Until
#!/bin/bash
echo "Say Yes“
read response
while [ -z $response ] || [ $response !=
"Yes" ] do
echo "Say Yes"
read response
done
Unix
Bash as an example of a shell
#!/bin/bash
inicial=`pwd`
for dir in /bin /etc do
cd $dir
pwd
sleep 2
done
cd $inicial
Unix
Bash as an example of a shell
{ {
instructions instructions
} }
myFunction myFunction
Unix
Bash as an example of a shell
Function
(sum)
sum ()
#!/bin/bash {
# Exemplo de uma função (soma) local number1=$1
local number2=$2
ARG=2 result=`expr $number1 + $number2`
WRONG_ARGS=-1 return $result
}
if [ $# -ne $ARG ];
then sum $1 $2
echo "`basename $0` numero1 numero2" echo "$1 + $2 = $?."
return $WRONG_ARGS return 0
fi
Unix
Bash as an example of a shell
Arrays
To create one or more empty
classical arrays, you usually use
the -a option of the declare
command
declare –a array1
$ echo ${tab[1**2+1]}
hello
…..