Red Hat Certified System Administration 8



Chapter 19: An Introduction to Bash Shell Scripting


Sommaire

  • Bash Scripting
  • Conditional Structures
  • Iterative Structures
  • Scripting Steps


Bash Scripting



Conditional Structures



Iterative Structures



Scripting Steps





Exercice : Table de Multiplication

Ecrire un script qui demande a l'utilisateur de saisir la valeur de la table puis affiche la table de multiplication correspondante

Resolution

[root@redhat ~]# mkdir SCRIPTS
[root@redhat ~]# touch SCRIPTS/table
[root@redhat ~]# chmod SCRIPTS/table
[root@redhat ~]# vi SCRIPTS/table

Edit and run file
[root@redhat ~]# SCRIPTS/table

Script
# !/bin/bash
echo "Saisir la table que vous voulez:"
read a
echo "TABLE DE MULTIPLICTION PAR $a"
for b in 0 1 2 3 4 5 6 7 8 9 10 11 12
do
c=`expr $a \* $b`
echo "$a x $b = $c"
done


Exercice : Testeur de fichiers executables

Ecrire un script qui prend en argument le nom d'un fichier puis affiche si le fichier est executable ou pas

Resolution

[root@redhat ~]# touch SCRIPTS/test
[root@redhat ~]# chmod 777 SCRIPTS/testor

Script
#!/bin/bash

if [ -x $1 ]
then echo "YES $1 est un executable"
else echo "$1 n'est pas executable"
fi


Eninam BAGNABANA     |         |     bagnadavid@outlook.fr     |