Script Template

#!/bin/bash
#
#       $Id: BashMitFHS.html,v 1.2 2003/04/19 21:45:11 matthias Exp $
#
#                       Script-Beschreibung/-Zweck
#
#                       verwendete Programme: rm
#
#-----------------------------------------------------------------------------
# -x = debugging on (xtrace)
# -h = Remember  the  location of commands as they
#      are  looked  up  for  execution.   This  is
#      enabled by default.
# -p = Turn on privileged mode.  In this mode, the
#      $ENV and $BASH_ENV files are not processed,
#      shell functions are not inherited from  the
#      environment, and the SHELLOPTS variable, if
#      it appears in the environment, is  ignored.
#
# (read 'man set' for full info on options)
set -h -p -x

#
# konfigurierbare Variablen:
#

#-----------------------------------------------------------------------------
# interne Arbeitsvariablen:
declare -r Me="${0##*/}"                       # Pfadnamen entfernen
#declare -r Who="${Me#*.}"                      # Datei-Endung entfernen
declare -r Who=$(basename $Me .sh)              # Datei-Endung entfernen
#declare -r ConfigFile="~/${Who}.conf"          # Konfigurations-Datei: privat
declare -r ConfigFile="./${Who}.conf"          # Konfigurations-Datei: privat
# oder:
# declare -r ConfigFile="/etc/${Who}.conf"     # Konfigurations-Datei: System
declare -r MyPID="$$"                          # PID des aktuellen Prozesses
declare -r MyFiles="/tmp/${Who}"                # Grund-Name fuer tmp.-Dateien
declare -r WorkFlag="${MyFiles}.MenAtWork"     # LOCK-Datei

# logfiles fuehren
exec 1>> /var/log/${Who}.log
exec 2>> /var/log/${Who}.err

#
# weitere Variablen-Deklarationen
#

#-----------------------------------------------------------------------------
# Unterprogramme/Funktionen
#

#-----------------------------------------------------------------------------
# Main()
#
# andere Programm-Kopie aktiv
[ -f ${WorkFlag} ] && exit 1

# Aufraeumen sicherstellen
trap " rm -f ${MyFiles}* ; exit " 0 1 2 3 9 15

# Aktivitaet signalisieren
echo ${MyPID} >${WorkFlag}
# sollte nie passieren ...
[ -w ${WorkFlag} ] || exit 1

# Konfig-Datei einlesen
[ -s ${ConfigFile} ] && . ${ConfigFile}

# Parse args
# wenn argumente uebergeben werden 
# den folgen while-block einkommentieren
#while [ "$1" ]; do
#   optarg=""
#   opt=""
#   case "$1" in
#       -*=*) 
#          opt=`echo "$1" | sed 's/=.*//'`
#          optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
#       ;;
#        *) 
#          opt=$1
#       ;;
#   esac
#
#   case $opt in
#       -u)
#          shift; # shift the '-u'
#          MYUSER=$1
#          shift; # shift the val
#       ;;
#
#       --user)
#          MYUSER=$optarg;
#          shift; # shift the '--user=val'
#       ;;
#
#       -h|*)
#          echo "my help text "
#          exit 1
#       ;;
#    esac
#done

#
# Anweisungen
#



#-----------------------------------------------------------------------------
#_EoF_

Generate Random String

Generate 20 byte long random string containing characters: A-Za-z0-9_

tr -dc A-Za-z0-9_ < /dev/urandom | head -c 20 | xargs echo

Generate Password function for .bashrc

genpasswd() {
   local l=$1
   [ "$l" == "" ] && l=20
   tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}

Parse Arguments

getopts (bash built-in)

while getopts "fhm" OPTION; do
   case $OPTION in
      f) FORCE=1 ;;
      h) print_help; exit ;;
      m) BASE="MM" ;;
      *) echo "Unknown Argument $OPTION given"; exit 1;;
   esac
done

while Loop

#!/bin/sh

while [ "$1" ]; do
   optarg=""
   opt=""
   case "$1" in
       -*=*) 
          opt=`echo "$1" | sed 's/=.*//'`
          optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
       ;;
        *) 
          opt=$1
       ;;
   esac

   case $opt in
       -u)
          shift; # shift the '-u'
          MYUSER=$1
          shift; # shift the val
       ;;

       --user)
          MYUSER=$optarg;
          shift; # shift the '--user=val'
       ;;

       -h|*)
          echo "my help text "
          exit 1
       ;;
    esac
done

echo "\$MYUSER = $MYUSER"
echo "\$Q = $Q"

exit 0

select Loop

select opt in $@; do
   if [ "$opt" = "Quit" ]; then
      echo done
      exit
   elif [ "$opt" = "Hello" ]; then
      echo Hello World
   else
      clear
      echo bad option
   fi
done

Leave a Reply