Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.94 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #exit : 0 -> Everything went fine
  4. #exit : 1 -> No parameter was set
  5. #exit : 2 -> The indicated parameter is the same as the current STOREINTERVAL
  6. #exit : 3 -> The parameter which represents the new STOREINTERVAL is not allowed
  7.  
  8. #Import config file
  9. source /sensor/etc/sensorConfigurationFile.inc
  10.  
  11. #temporilary store the current store interval value in config file
  12. currentStoreInterval=$STOREINTERVAL
  13.  
  14. #Create array of allowed values for parameter $1
  15. allowedInterval=(0 1 2 5 10 15 30 60 120 360 720)
  16.  
  17. #Check if at least and only 1 parameter was set -> if there is no parameter exit:1
  18. if [ "$#" -ne "1" ]; then
  19.   exit 1
  20. fi
  21.  
  22. #Check if parameter value is the same as the current store interval value in the config file -> if it is exit with exit status code 2
  23. if [ "$currentStoreInterval" -eq "$1" ]; then
  24.   exit 2
  25. fi
  26.  
  27. #Store parameter in variable pStoreInterval
  28. pStoreInterval=$1
  29.  
  30. #define allowedValue and set to 0 to use it later in if statement
  31. allowedValue=0
  32.  
  33. #Loop through allowedInterval Array and verify whether the variable pStoreInterval is in it
  34. for i in "${allowedInterval[@]}"
  35. do
  36.   #If the variable pStoreInterval is in the allowedInterval array, then set allowedValue to 1
  37.   if [ "$i" -eq "$pStoreInterval" ]; then
  38.     allowedValue=1
  39.   fi
  40. done
  41.  
  42. #Verify if allowedValue is equal to 1, if not exit:3
  43. if [ "$allowedValue" -eq "1" ]; then
  44.   #Store interval value is allowed
  45.   #Create a temporary copy of the config file
  46.   `touch $CONFIGLOCATION.sensorConfigurationFile.inc.$$`
  47.   #Loop through the temporary file and change the value of STOREINTERVAL to the new value
  48.   while read line; do
  49.     if [ "$line" == "STOREINTERVAL=$currentStoreInterval" ]; then
  50.       echo "STOREINTERVAL=$pStoreInterval" >> $CONFIGLOCATION.sensorConfigurationFile.inc.$$
  51.     else
  52.      echo $line >> $CONFIGLOCATION.sensorConfigurationFile.inc.$$
  53.     fi
  54.   done < $CONFIGLOCATION$CONFIGNAME
  55.   #Delete the old config file
  56.   rm $CONFIGLOCATION$CONFIGNAME
  57.   #Rename the temporary config file to the actual name
  58.   mv $CONFIGLOCATION.sensorConfigurationFile.inc.$$ $CONFIGLOCATION$CONFIGNAME
  59.  
  60.   #Change value in crontab
  61.   #Convert integer to cronjob syntax
  62.   if [ "$pStoreInterval" -eq "0" ]; then
  63.     cronSyntax="* * * * *"
  64.   fi
  65.  
  66.   if [ "$pStoreInterval" -eq "1" ]; then
  67.     cronSyntax="* * * * *"
  68.   fi
  69.  
  70.   if [ "$pStoreInterval" -eq "2" ]; then
  71.     cronSyntax="*/2 * * * *"
  72.   fi
  73.  
  74.   if [ "$pStoreInterval" -eq "5" ]; then
  75.     cronSyntax="*/5 * * * *"
  76.   fi
  77.  
  78.   if [ "$pStoreInterval" -eq "10" ]; then
  79.     cronSyntax="*/10 * * * *"
  80.   fi
  81.  
  82.   if [ "$pStoreInterval" -eq "15" ]; then
  83.     cronSyntax="*/15 * * * *"
  84.   fi
  85.  
  86.   if [ "$pStoreInterval" -eq "30" ]; then
  87.     cronSyntax="*/30 * * * *"
  88.   fi
  89.  
  90.   if [ "$pStoreInterval" -eq "60" ]; then
  91.     cronSyntax="0 0-23 * * *"
  92.   fi
  93.  
  94.   if [ "$pStoreInterval" -eq "120" ]; then
  95.     cronSyntax="0 0-23/2 * * *"
  96.   fi
  97.  
  98.   if [ "$pStoreInterval" -eq "360" ]; then
  99.     cronSyntax="0 0-23/6 * * *"
  100.   fi
  101.  
  102.   if [ "$pStoreInterval" -eq "720" ]; then
  103.     cronSyntax="0 0-23/12 * * *"
  104.   fi
  105.  
  106.   #Change in crontab
  107.   #Put crontab content to a temporary file
  108.   crontab -l > $CONFIGLOCATION.myCrontab.$$
  109.   #Loop through the temporary file
  110.   while read cronLine; do
  111.     #Change the storeValues line to the new line
  112.     if [[ "$cronLine" =~ "sudo "$SCRIPTLOCATION"storeValues" ]]; then
  113.       echo "$cronSyntax sudo "$SCRIPTLOCATION"storeValues" >> $CONFIGLOCATION"newCrontab."$$
  114.     else
  115.       echo "$cronLine" >> $CONFIGLOCATION"newCrontab."$$
  116.     fi
  117.   done < $CONFIGLOCATION".myCrontab."$$
  118.  #Overwrite the crontab with the new file
  119.   crontab $CONFIGLOCATION"newCrontab."$$
  120.   #Delete the temporary file
  121.   rm $CONFIGLOCATION"newCrontab."$$
  122.   rm $CONFIGLOCATION".myCrontab."$$
  123.   #Restart cron
  124.   /etc/init.d/cron restart
  125.   #Output new values
  126.   #echo "$cronSyntax"
  127.   #echo $pStoreInterval
  128.   exit 0
  129. else
  130.   #If the value is not allowed output this
  131.   echo "The indicated value is not allowed"
  132.   exit 3
  133. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement