0% found this document useful (0 votes)
12 views

Unix-User-Bash-Shell

Uploaded by

sidikiensias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unix-User-Bash-Shell

Uploaded by

sidikiensias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

bash comme exemple de shell

Prof. Amine Berqia

Email :
amine.berqia@ensias.um5.ac.ma
Unix
Bash as an example of a shell

Variables: There are three types of shell variables


key parameters
positional parameters
special parameters.
Unix
Bash as an example of a shell
Special variables or parameters are given a specific meaning:

$# the number of arguments passed to the program


$ references all positional parameters
$0 the name of the program or script to be executed
$$ the number of the process to be executed
$! the number of the last process running in the background
$? the status of the last command not executed in the background
$1 contains the first parameter
$2 contém o segundo parâmetro;
….
Unix
Bash as an example of a shell
Operators:
-lt less than
-le less or equal
-eq equal to
-gt greater than
-ne not equal to
...
Unix
Bash as an example of a shell

Make a script in BASH to read a name and write Hello name.

#!/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 ]!

There are three different types of tests that can be performed in


bash: tests on strings; tests on numbers; tests on files.
Unix
Bash as an example of a shell
A script to check if two names are different or not:

#!/bin/bash

if [ $1 != $2 ] then
echo " The 2 names are different!" else
echo "The 2 names are identicals!"
fi

$ ./names.sh Ahmed Ali

The 2 names are different!


Unix
Bash as an example of a shell
A script to check whether a student is admitted to the exam or not:

#!/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:

#!/bin/bash -e $file: fot file.

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

In an if, it is possible to do several tests


at the same time

&& ||

#!/bin/bash

if [ $# -ge 1 ] && [ $1 = 'Maroc' ]


…..
Unix
Bash as an example of a shell
If - elif - else

#!/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

for var in val while [ test ]


do do
.... echo 'Action'
done Done

for i in `seq 1 10`; until [ test ]


do do
.... echo 'Action'
done done
Unix
Bash as an example of a shell
We'll ask the user to say "Yes" and repeat this
action until they've done what we wanted.
Let's create an exwhile.sh script:

#!/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

Make a bash script called exfor1.sh using the for control


structure that loops through the /bin and /etc directories for 2
seconds and displays them. Finally, return to the home
directory.

#!/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

Implement a counter in bash called


exuntil1.sh using the until control structure
with values ​between 1 and 20.
#!/bin/bash
inf=1
sup=20
corrente=$inf

until [ $corrente -gt $sup ]; do


echo "Valor corrente: " $corrente
let "corrente = corrente + 1"
done
Unix
Bash as an example of a shell
A function is a set of instructions, allowing
you to perform multiple tasks with different
input parameters.
You are not limited in the number of
functions. However, they should not have the
same name.
You can reuse the global variables initialized
in your script inside or outside your function.
You can declare variables local to your
functions.
A good function is a function that handles a
specific, recurring element in your script.
Unix
Bash as an example of a shell
In Bash, there are two ways to declare a function:
myFunction () function myFunction

{ {
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

As in C, the index of a classical


array always starts at 0 and its
maximum value is that of the
largest positive integer that can
be represented.
Unix
Bash as an example of a shell
To designate an element of a classic array, use the
syntax: array1[index]
$ declare -a tab
tab=(val0 val1 ...)

$ read tab[1] tab[3] tab=( [indice]=val ... )


ola bonjour
$ tab[0]=hello
$
-a option of the read or readonly command. Each
inserted word becomes an element of the classic array:
$ read -a tab
ola bonjour hello
$
declare -ra tab?
Declare –p ?
Unix
Bash as an example of a shell
We got the value of an element from an array
using the syntax : ${tab[indice]}
$ echo ${tab[1]}
ola

$ echo ${tab[1**2+1]}
hello

qualquer expressão aritmética válida para


calcular o índice de um elemento
Unix
Bash as an example of a shell

a variable not previously defined as an array can be


interpreted as a classical array :
$ var=hello
$ echo ${var[0]}
hello
$ var=( hi "${var[0]}" )
$ echo ${var[1]}
????

To get the length of $an array element ${# tab[índice]}


$ echo ${#var[0]}
5
Unix
Bash as an example of a shell

To delete one or more classic arrays: unset tab …

To add one or more elements to a classic array:

tab+=( val1 val2 … )


tab=( val0 val1 … "${tab[@]}")

…..

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy