You are on page 1of 6

Bash Scripting

Bash Scripting

A shell program, called a script, is an easy-to-use tool


for building applications by "gluing together" system
calls, tools, utilities, and compiled binaries. Virtually
the entire repertoire of UNIX commands, utilities, and
tools is available for invocation by a shell script.
If that were not enough, internal shell commands, such
as testing and loop constructs, lend additional power
and flexibility to scripts.
Shell scripts are especially well suited for
administrative system tasks and other routine
repetitive tasks not requiring the bells and whistles of
a full-blown tightly structured programming language.
Script files begins with
#!/bin/bash

Arguments
Scripts arguments
$1 first argument …. $9 nineth argument
$0 script file name
$? Last command result =0 if its runs correctly =1 in
error case
$# arguments number

Arguments Nature testing


Syntax

test –option argument test argument nature =0 if it’s


option nature
options
f file
d directory
b block
x executable
w writable
r readable
e file existence (test if file exists)

Comparison testing
Syntax
test arg1 -option arg2
options
eq equals
ne different
le less than or equals
ge greater than or equals
lt less than
gt greater than

Calculs
expr val1 op val2
operandes
+ addition
- substraction
/ division
\* multiplication
exple res =`expr 2 \* 2`

Functions
read var wait input value and store it in the variable
read -p "Dispalyed-Text" var wait input value and
store it in the variable

Conditional Structures

Conditional Structures

if Structure
Cette structure est utilisée pour exécuter une action
sous condition
Syntaxif condition
then action
else actionsinon
fi

case Structure
Cette structure est utilisée en cas d'un grand nombre
de condition pour éviter l'abus de la structure "if"
Syntaxcase var in
value1) Instruction-1
value2) Instruction-2
esac
Iterative Structures

Iterative Stuctures

• for
• while
• Until
for
La boucle "for" est utilisée pour les actions itératives
dont le nombre d'itérations est connu
Syntax
for var in value1 value2 value3
do
action effectuée à chaque itération
done

while
La boucle "while" est utilisée pour les actions itératives
dont le nombre d'itérations n'est pas connu
Syntaxwhile condition
do
action
done
until
The counterpart of while is until, which opens an
iteration that lasts until the condition is true.

Scripting Steps

Scripting Steps

1. Create Script file


2. Edit file
3. Set x permission on file
4. Execute with ./filename
5. add Script Directory to PATH (Optional: Execxute
script everywhere by just typing script filename )

You might also like