Aucun résumé des modifications |
Aucun résumé des modifications |
||
| (4 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 1 : | Ligne 1 : | ||
{{#seo:|keywords=bash,script}} | |||
https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Gnu-bash-logo.svg/langfr-290px-Gnu-bash-logo.svg.png | https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Gnu-bash-logo.svg/langfr-290px-Gnu-bash-logo.svg.png | ||
{{sommaire à droite}} | {{sommaire à droite}} | ||
| Ligne 9 : | Ligne 9 : | ||
== Récupérer le répertoire du script courant == | == Récupérer le répertoire du script courant == | ||
{{syntaxhighlight| lang=bash |code= | |||
CURDIR=$(dirname $(readlink -f $0)) | CURDIR=$(dirname $(readlink -f $0)) | ||
< | }}<br> | ||
== Rediriger les sorties == | == Rediriger les sorties == | ||
Attention, bien mettre 2>&1 à la fin de la ligne | Attention, bien mettre 2>&1 à la fin de la ligne | ||
{{syntaxhighlight| lang=bash |code= | |||
prog > log.txt 2>&1 | prog > log.txt 2>&1 | ||
< | }}<br> | ||
== Un équivalent de sprintf == | == Un équivalent de sprintf == | ||
{{syntaxhighlight| lang=bash |code= | |||
file=$(printf 'FILE=_%s_%s.dat' $val1 $val2) | file=$(printf 'FILE=_%s_%s.dat' $val1 $val2) | ||
< | }}<br> | ||
== Vérifier le retour d'une commande == | == Vérifier le retour d'une commande == | ||
{{syntaxhighlight| lang=bash |code= | |||
grep "test" test.txt | grep "test" test.txt | ||
if [ $? -gt 0 ]; then | if [ $? -gt 0 ]; then | ||
echo "ERROR!" | echo "ERROR!" | ||
fi | fi | ||
< | }}<br> | ||
== Debug de bash == | == Debug de bash == | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
set -x | set -x | ||
... | ... | ||
< | }}<br> | ||
== Ligne de Commande == | == Ligne de Commande == | ||
=== Test du nombre d'arguments de la ligne de commande === | === Test du nombre d'arguments de la ligne de commande === | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
if [ $# -ne 2 ] | if [ $# -ne 2 ] | ||
| Ligne 46 : | Ligne 51 : | ||
exit | exit | ||
fi | fi | ||
< | }}<br> | ||
=== Récupérer les options de la ligne de commande === | === Récupérer les options de la ligne de commande === | ||
{{syntaxhighlight| lang=bash |code= {{#!: | |||
#!/bin/bash | #!/bin/bash | ||
| Ligne 79 : | Ligne 85 : | ||
done | done | ||
shift $(($OPTIND - 1)) | shift $(($OPTIND - 1)) | ||
}}}} | |||
== Fichier== | == Fichier== | ||
===Supprimer tous les fichiers d'un répertoire à l'exception d'un fichier=== | ===Supprimer tous les fichiers d'un répertoire à l'exception d'un fichier=== | ||
{{syntaxhighlight| lang=bash |code= | |||
shopt -s extglob | shopt -s extglob | ||
rm -rf -- !(.git) | rm -rf -- !(.git) | ||
< | }}<br> | ||
=== Récupérer le nom et l'extension d'un fichier === | === Récupérer le nom et l'extension d'un fichier === | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
F=”thisfile.txt” | F=”thisfile.txt” | ||
| Ligne 95 : | Ligne 103 : | ||
echo ${F%.*} | echo ${F%.*} | ||
thisfile | thisfile | ||
< | }}<br> | ||
=== Tester si un fichier existe === | === Tester si un fichier existe === | ||
{{syntaxhighlight| lang=bash |code= | |||
if [ ! -f /tmp/foo.txt ] | if [ ! -f /tmp/foo.txt ] | ||
then | then | ||
echo the file does not exist | echo the file does not exist | ||
fi | fi | ||
< | }}<br> | ||
=== Tester si un lien existe === | === Tester si un lien existe === | ||
{{syntaxhighlight| lang=bash |code= | |||
if [ ! -L /tmp/foo.txt ] | if [ ! -L /tmp/foo.txt ] | ||
then | then | ||
echo the link does not exist | echo the link does not exist | ||
fi | fi | ||
< | }}<br> | ||
=== Ecrire en binaire dans un fichier === | === Ecrire en binaire dans un fichier === | ||
{{syntaxhighlight| lang=bash |code= {{#!: | |||
#!/bin/bash | #!/bin/bash | ||
echo -n "Suspend2" | dd of=/my_hibernation_image bs=1 conv=notrunc | echo -n "Suspend2" | dd of=/my_hibernation_image bs=1 conv=notrunc | ||
}} | |||
}} | |||
== Variables == | == Variables == | ||
=== Définir la valeur par défaut === | === Définir la valeur par défaut === | ||
{{syntaxhighlight| lang=bash |code= | |||
#DIR prend la valeur du premier argument de la ligne de commande ou tmpdir | #DIR prend la valeur du premier argument de la ligne de commande ou tmpdir | ||
DIR=${1:-$tmpdir} | DIR=${1:-$tmpdir} | ||
< | }}<br> | ||
=== Tester une valeur === | === Tester une valeur === | ||
{{syntaxhighlight| lang=bash |code= | |||
if [ "$VAR" == "Test" ] | if [ "$VAR" == "Test" ] | ||
then | then | ||
echo "var is Test" | echo "var is Test" | ||
fi | fi | ||
< | }}<br> | ||
== Vérifier si une variable est vide (non affectée) == | == Vérifier si une variable est vide (non affectée) == | ||
{{syntaxhighlight| lang=bash |code= | |||
if [ -z "$1" ] | if [ -z "$1" ] | ||
then | then | ||
| Ligne 143 : | Ligne 158 : | ||
echo "variable is set" | echo "variable is set" | ||
fi | fi | ||
< | }}<br> | ||
=== Comment récupérer une variable de nom variable === | === Comment récupérer une variable de nom variable === | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
| Ligne 153 : | Ligne 168 : | ||
C=${!B} | C=${!B} | ||
echo $C | echo $C | ||
< | }}<br> | ||
== Comment lire un mot de passe == | == Comment lire un mot de passe == | ||
on utilise l'option -s de read | on utilise l'option -s de read | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
read -s password | read -s password | ||
< | }}<br> | ||
== Comment faire des opérations mathématiques sur des variables == | == Comment faire des opérations mathématiques sur des variables == | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
| Ligne 172 : | Ligne 189 : | ||
$ C=$((A+B)) | $ C=$((A+B)) | ||
$ echo $C | $ echo $C | ||
< | }}<br> | ||
== Comment utiliser les expressions régulières == | == Comment utiliser les expressions régulières == | ||
{{syntaxhighlight| lang=bash |code= | |||
#!/bin/bash | #!/bin/bash | ||
if [[ $NUMBER =~ ^[0-9]+$ ]]; then | if [[ $NUMBER =~ ^[0-9]+$ ]]; then | ||
| Ligne 184 : | Ligne 202 : | ||
echo ceci n'est pas un nombre | echo ceci n'est pas un nombre | ||
fi | fi | ||
< | }}<br> | ||
== Liens externes == | == Liens externes == | ||
{{grebox-jck |class=col-lg-8|content= | |||
<favorites path="ShellLinux/Bash"/> | <favorites path="ShellLinux/Bash"/> | ||
}} | |||
[[Category:Commandes_Linux]] | [[Category:Commandes_Linux]] | ||
Voir .bashrc
98-petits-trucs-pour-configurer-l-historique-du-bash
CURDIR=$(dirname $(readlink -f $0))
Attention, bien mettre 2>&1 à la fin de la ligne
prog > log.txt 2>&1
file=$(printf 'FILE=_%s_%s.dat' $val1 $val2)
grep "test" test.txt
if [ $? -gt 0 ]; then
echo "ERROR!"
fi
#!/bin/bash
set -x
...
#!/bin/bash
if [ $# -ne 2 ]
then
exit
fi
#!/bin/bash
while getopts lvxad:s:u:h opt
do
case $opt in
d) DISPLAY="$OPTARG:0.0"
DIS_CMD="-display $DISPLAY"
export DISPLAY
;;
s) SSH_CMD="ssh $OPTARG"
HOSTNAME=$OPTARG
;;
h) ACTION=HELP
echo "help"
;;
x) ACTION=XXREF
;;
a) ACTION=XXREF_ALL
;;
l) ACTION=LIST
;;
u) USER=$OPTARG
echo "user:" $USER
;;
v) opt_verbose=1
;;
esac
done
shift $(($OPTIND - 1))
shopt -s extglob
rm -rf -- !(.git)
#!/bin/bash
F=”thisfile.txt”
echo ${F#*.}
txt
echo ${F%.*}
thisfile
if [ ! -f /tmp/foo.txt ]
then
echo the file does not exist
fi
if [ ! -L /tmp/foo.txt ]
then
echo the link does not exist
fi
#!/bin/bash
echo -n "Suspend2"| dd of=/my_hibernation_image bs=1 conv=notrunc
#DIR prend la valeur du premier argument de la ligne de commande ou tmpdir
DIR=${1:-$tmpdir}
if [ "$VAR" == "Test" ]
then
echo "var is Test"
fi
if [ -z "$1" ]
then
echo "unset variable"
else
echo "variable is set"
fi
#!/bin/bash
A="abcd dcba"
B=A
C=${!B}
echo $C
on utilise l'option -s de read
#!/bin/bash
read -s password
#!/bin/bash
$ A=2
$ B=3
$ C=$((A+B))
$ echo $C
#!/bin/bash
if [[ $NUMBER =~ ^[0-9]+$ ]]; then
echo ceci est un nombre
else
echo ceci n'est pas un nombre
fi