Shell Script

Aus MeinWiki
Wechseln zu: Navigation, Suche

Funktionen

Allgemeine Funktionen

  • Get script path
   # get script path
   GetScriptPath()
   {
       echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
   }
  • Write logfile
   # write logfile
   # $1 Description
   # $2 Logfiledestination
   WriteLog ()
   {
       echo $(date "+%y%m%d-%H%M%S")' '$1 >> $2
   }
  • Change from upper to lower char
   # change from upper to lower char
   # $1 Variable to change
   # $2 Logfiledestionation
   LowerChar ()
   {
       WriteLog  'LowerChar '$1 $2
       local myResult=`echo $1 | tr 'A-Z' 'a-z'`
       echo "$myResult"
   }
  • Read file to parameter
   # read file to parameter
   # $1 Filepath
   # $2 Logfiledestination
   ReadFile()
  {
       local myResult=
       WriteLog 'ReadFile '$1 $2
       if [ -e $1 ]; then
               myResult=$(cat $1 | grep -v '#' |\
               while read myResult;
                       do
                                       echo $myResult
                       done
                       )
       else
           WriteLog 'ReadFile Error File '$1 $2
           exit 1
       fi
       echo "$myResult"
  }
  • Check Prozess and wait
   # Check Prozess and wait
   # $1 wait prozzes
   # $2 waiting time
   # $3 Logfile
   CheckWaitProzessSh()
   {
       WriteLog 'CheckWaitProzess '$1 $3
       local myProzess=(`ps -ef | grep 'sh' | grep $1 | awk '{print $7}'`)
       # echo ${#myProzess[*]}
       while [[ ${#myProzess[*]} -gt 1 ]]
       do
           sleep $2
           myProzess=(`ps -ef | grep 'sh' | grep $1 | awk '{print $7}'`)
           echo -ne '.'
       done
       echo 
   }
  • Check wait system live
   # Check wait system live
   # $1 Systemname / IP-Adresse
   # $2 Testcyclus
   # $3 waiting time
   # $4 Logfile
   CheckWaitSystem()
   {
       WriteLog 'CheckWaitSystem '$1 $4
       local systemAlive=
       local i=0
       while  $2 -gt $i 
       do
           sleep $3
           systemAlive=(`ping $1 -c 1 |grep received | awk '{print $4}'`)
           if [ $systemAlive -ne 1 ]; then
               i=$2
           fi
           i=$[$i+1];
           echo -ne '.'
       done
       echo 
   }
  • Check system alive
   # Check system alive
   # $1 Systemname / IP-Adresse
   # $2 Logfile
   CheckAliveSystem()
   {
       WriteLog 'CheckAliveSystem '$1 $2
       local systemAlive=
       systemAlive=(`ping $1 -c 1 |grep received | awk '{print $4}'`)
       if [ $systemAlive -eq 1 ]; then
           echo true
       else
           echo false
       fi
   }