Bash: plusieurs conditions pour vérifier un paramètre

Donc, pour exécuter mon script, j’utilise un paramètre comme celui-ci:

./script 789 

Le paramètre est de changer les permissions d’un fichier ou d’un répertoire, et je voudrais vérifier si chaque chiffre est compris entre 0 et 7, alors ce que j’ai essayé est la suivante:

  if [[ ${1:0:1} -ge 0 && ${1:0:1} -le 7 ]] && [[ ${1:1:1} -ge 0 && ${1:1:1} -le 7]] && [[ ${1:2:1} -ge 0 && ${1:2:1} -le 7]] then {go ahead with the code} else echo "Error: each digit in the parameter must be between 0 and 7" fi 

Si c’est vrai, continuez avec le script, sinon affichez un message d’erreur, mais cela ne semble pas fonctionner.

Vous voulez faire correspondre le paramètre avec l’expression régulière [0-7][0-7][0-7] ou [0-7]{3} . En bash, vous pouvez faire:

 [[ "$1" =~ [0-7]{3} ]] || { echo 'invalid parameter' >&2; exit 1; } 

Ou:

 echo "$1" | grep -Eq '[0-7]{3}' || { echo err message >&2; exit 1; } 

cela pourrait être une autre façon

 #!/bin/bash #Stored input parameter 1 in a variable perm="$1" #Checking if inserted parameter is empty, in that case the script will show a help if [ -z "${perm}" ];then echo "Usage: $0 " echo "Ie: $0 777" exit else #if the parameter is not empy, check if each digit is between 0-7 check=`echo $perm| grep [0-7][0-7][0-7]` #if the result of command is empty that means that the input parameter contains at least one digit that's differs from 0-7 if [ -z "${check}" ];then echo "Error: each digit in the parameter must be between 0 and 7" fi fi 

c’est la sortie

 [shell] ➤ ./test9.ksh 999 Error: each digit in the parameter must be between 0 and 7 [shell] ➤ ./test9.ksh 789 Error: each digit in the parameter must be between 0 and 7 [shell] ➤ ./test9.ksh 779 Error: each digit in the parameter must be between 0 and 7 [shell] ➤ ./test9.ksh 777