Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #exit : 0 -> Everything went fine
- #exit : 1 -> No parameter was set
- #exit : 2 -> The indicated parameter is the same as the current STOREINTERVAL
- #exit : 3 -> The parameter which represents the new STOREINTERVAL is not allowed
- #Import config file
- source /sensor/etc/sensorConfigurationFile.inc
- #temporilary store the current store interval value in config file
- currentStoreInterval=$STOREINTERVAL
- #Create array of allowed values for parameter $1
- allowedInterval=(0 1 2 5 10 15 30 60 120 360 720)
- #Check if at least and only 1 parameter was set -> if there is no parameter exit:1
- if [ "$#" -ne "1" ]; then
- exit 1
- fi
- #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
- if [ "$currentStoreInterval" -eq "$1" ]; then
- exit 2
- fi
- #Store parameter in variable pStoreInterval
- pStoreInterval=$1
- #define allowedValue and set to 0 to use it later in if statement
- allowedValue=0
- #Loop through allowedInterval Array and verify whether the variable pStoreInterval is in it
- for i in "${allowedInterval[@]}"
- do
- #If the variable pStoreInterval is in the allowedInterval array, then set allowedValue to 1
- if [ "$i" -eq "$pStoreInterval" ]; then
- allowedValue=1
- fi
- done
- #Verify if allowedValue is equal to 1, if not exit:3
- if [ "$allowedValue" -eq "1" ]; then
- #Store interval value is allowed
- #Create a temporary copy of the config file
- `touch $CONFIGLOCATION.sensorConfigurationFile.inc.$$`
- #Loop through the temporary file and change the value of STOREINTERVAL to the new value
- while read line; do
- if [ "$line" == "STOREINTERVAL=$currentStoreInterval" ]; then
- echo "STOREINTERVAL=$pStoreInterval" >> $CONFIGLOCATION.sensorConfigurationFile.inc.$$
- else
- echo $line >> $CONFIGLOCATION.sensorConfigurationFile.inc.$$
- fi
- done < $CONFIGLOCATION$CONFIGNAME
- #Delete the old config file
- rm $CONFIGLOCATION$CONFIGNAME
- #Rename the temporary config file to the actual name
- mv $CONFIGLOCATION.sensorConfigurationFile.inc.$$ $CONFIGLOCATION$CONFIGNAME
- #Change value in crontab
- #Convert integer to cronjob syntax
- if [ "$pStoreInterval" -eq "0" ]; then
- cronSyntax="* * * * *"
- fi
- if [ "$pStoreInterval" -eq "1" ]; then
- cronSyntax="* * * * *"
- fi
- if [ "$pStoreInterval" -eq "2" ]; then
- cronSyntax="*/2 * * * *"
- fi
- if [ "$pStoreInterval" -eq "5" ]; then
- cronSyntax="*/5 * * * *"
- fi
- if [ "$pStoreInterval" -eq "10" ]; then
- cronSyntax="*/10 * * * *"
- fi
- if [ "$pStoreInterval" -eq "15" ]; then
- cronSyntax="*/15 * * * *"
- fi
- if [ "$pStoreInterval" -eq "30" ]; then
- cronSyntax="*/30 * * * *"
- fi
- if [ "$pStoreInterval" -eq "60" ]; then
- cronSyntax="0 0-23 * * *"
- fi
- if [ "$pStoreInterval" -eq "120" ]; then
- cronSyntax="0 0-23/2 * * *"
- fi
- if [ "$pStoreInterval" -eq "360" ]; then
- cronSyntax="0 0-23/6 * * *"
- fi
- if [ "$pStoreInterval" -eq "720" ]; then
- cronSyntax="0 0-23/12 * * *"
- fi
- #Change in crontab
- #Put crontab content to a temporary file
- crontab -l > $CONFIGLOCATION.myCrontab.$$
- #Loop through the temporary file
- while read cronLine; do
- #Change the storeValues line to the new line
- if [[ "$cronLine" =~ "sudo "$SCRIPTLOCATION"storeValues" ]]; then
- echo "$cronSyntax sudo "$SCRIPTLOCATION"storeValues" >> $CONFIGLOCATION"newCrontab."$$
- else
- echo "$cronLine" >> $CONFIGLOCATION"newCrontab."$$
- fi
- done < $CONFIGLOCATION".myCrontab."$$
- #Overwrite the crontab with the new file
- crontab $CONFIGLOCATION"newCrontab."$$
- #Delete the temporary file
- rm $CONFIGLOCATION"newCrontab."$$
- rm $CONFIGLOCATION".myCrontab."$$
- #Restart cron
- /etc/init.d/cron restart
- #Output new values
- #echo "$cronSyntax"
- #echo $pStoreInterval
- exit 0
- else
- #If the value is not allowed output this
- echo "The indicated value is not allowed"
- exit 3
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement