Petit truc bash

On 30 January 2008, in computing, by ayoli

Voici un petit truc pour manipuler des arguments passés à un script bash de la sorte :

./my_script -arg value

D’abord on teste si un argument a bien été donné :


if [ $# -eq 0 ]; then
  1. pas d’argument, on imprime un petit message d’aide et on sort
    echo -e “usage : ./my_script -arg [value|other_value]”
    exit 1
    fi

Après, voici la partie de code qui va manipuler l’argument et sa valeur :

if [ ”$1” = ”-arg” ]; then
shift;# on va à la valeur, $1 vaut maintenant la chaine passée apres -arg
case ”$1” in
“value”) echo -e ” ici on fait quelque chose avec la valeur value” ;;
“other_value”) echo -e ” ici on fait quelque chose avec la valeur other_value”;;
  • echo -e “cas par défaut, peut etre la valeur manque ou n’est pas celle attendue” ;;
    esac
    fi

Tagged with:  

Leave a Reply

Free WordPress Theme

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!