2017-10-01 1 views
0

mon code est quelque chose comme çacomment obtenir la commande d'entrée suivante basée sur la comparaison de commande précédente [bash script]

#!/bin/bash 
for i in $1 $2 $3 $4; do 
if [[("$i" == '-f')]] ; then #I'm searching if the user input the -f switch 
if [ -f "$((i+1))" ]; then # I'm trying to increment the position of $i to get the input that follows the -f switch and check the existence of the file 
echo "$((i+1)) file found" 
else 
echo "$((i+!)) file not found" 
fi 
fi 
done 

ma question est de savoir comment obtenir après une entrée spécifique l'entrée de l'utilisateur qui est dans mon cas -f. est-ce possible de faire dans bash scripting. un indice sur la façon de faire. Merci.

Répondre

0

Vous devriez certainement utiliser getopts, mais vous pouvez simplement mettre un drapeau tout en réitérant:

#!/bin/bash 

unset FLAG 
for x; do 
     if test -n "$FLAG"; then 
       FILENAME=$x 
       unset FLAG 
       continue 
     fi 
     case $x in 
     -f) FLAG=1;; # Set flag for next iteration 
     *) echo "processing argument $x" 
     esac 
done 
echo "FILENAME=$FILENAME" 

C'est terriblement fragile, et je me sens un peu coupable suggérant qu'elle. Vraiment, utilisez getopts au lieu d'essayer de lancer votre propre analyse d'arguments.

+0

Merci. C'est très utile. Vous êtes un gars intelligent incroyable. – Muzy