You are not logged in.

Dear visitor, welcome to sourceserver.info. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

  • Impact

    Super Moderator

    You have to register first, to connect to this user.

18

Tipp zur Validierung bei Shellscripten

Rating:

by Impact, Saturday, April 30th 2011, 1:32pm

Wenn man Shellscripte schreibt müssen diese stets validiert werden, da kommt man nicht drum herum
Meistens kommt dann eine Ausgabe a´la

Source code

1
So nicht Freundchen -> Exit

Wenn man nun mehrere Inputs so validiert, wird schnell klar das der jenige nur ein Bruchteil davon sieht.
Mein Tippt so etwas zu Lösen ist folgende.

Man Legt Vor der Validierung eine Variable fest, nennen wir sie mal Startup_lock.

Source code

1
STARTUP_LOCK="0"

Danach kommen dann die Validierungen, sofern etwas den Start behindern würde oder nicht passt erhöht man die Variablenzahl.

Source code

1
2
3
4
5
6
7
STARTUP_LOCK="0"

if [ ! "$MEIN_INPUT" == "MEIN_WUNSCH" ]; then
  echo "Nene mein Freund, so passt das nicht."
  # Variable erhöhen
  STARTUP_LOCK="$[$STARTUP_LOCK+1]"
fi

Am Ende muss man nun nur noch prüfen ob die Lockvariable 0 ist

Source code

1
2
3
4
5
 if [ ! "$STARTUP_LOCK" == "0" ]; then
   echo ""
   echo "Es wurden mindestens '$STARTUP_LOCK' Probleme gefunden, Start verhindert."
   exit
fi

Problem gelöst ;)

This article has been read 8,394 times.

Tags: shellscript, Validierung, Variable

Categories: Bash