Run a Bash Script Automatically After 10 Mins When the Codition Fails - bash

if [ $FILE_SIZE -ge 5000000000 and $FILE_SIZE -le 10000000000 ]
then
cp $s/* $W
else
#Here I need to wait the script for 10 minutes and re-run the if condition again
echo "file $myfile is out of Limit"
fi

You can use at to reschedule the script from within the script if the exexution fails. In the else block, put something like:
at now + 10 minutes << END
./$0
END

You could use sleep to wait for 10 minutes, like so:
while true ; do
if [ "$FILE_SIZE" -ge 5000000000 -a "$FILE_SIZE" -le 10000000000 ] ; then ; break ; fi
echo "file $myfile is out of Limit"
sleep 600
done
cp $s/* $W

Related

Bash script for macOS Loop if then else

Trying to do a loop with this script below. It works if I break it by deleting Google Chrome from Applications. If I put Chrome back into place it continuously kills the dock and says No matching processes belonging to you were found. Do I have to add something to killall Dock to exit the script or is Done in the wrong spot? Tried multiple things without any luck. Eventually want it to try every 15 minutes until all apps are in Applications and kill the dock so the shortcuts show instead of Question marks. This will happen once all apps get installed and Dock gets restarted.
i=0
while [ $i -lt 4 ]
do
if [[ -d "/Applications/Company Portal.app" && -d "/Applications/Google Chrome.app" ]];
then
killall Dock
else
i=$((i + 1)) | echo "Will tray again...."
sleep 10
fi
done
Update Here is what I eventually came up with, yea messy! But works! Will have to look at it more after see the responses here though.
echo $(date)
# Check to see if script has already run on computer before, if so, then exit.
file=/Users/Shared/.If_Installed_Restart_Dock_Has_Run.txt
if [ -e "$file" ]; then
echo "The If_Installed_Restart_Dock.sh script has run before and exiting..." && exit 0
else
touch /Users/Shared/.If_Installed_Restart_Dock_Has_Run.txt
fi
i=0
while [ $i -lt 6 ]
do
if [[ -d "/Applications/Google Chrome.app" && -d "/Applications/Microsoft Edge.app" ]];
then
killall Dock && exit 0
else
echo "Applications still need installed in order to restart Dock, will check again in 10 minutes for up to an hour, intune will try again in 8hrs..."
fi
i=$((i + 1))
sleep 600
done
Chrome is irrelevant. You're falling victim to the classic blunder. Consider:
#!/bin/bash
i=0
while [ $i -lt 4 ]; do
if echo "in first if, i = $i"; false ; then
echo bar
else
echo "in else, i = $i"
i=$((i + 1)) | echo "Will tray again...." # (1)
echo "after echo i = $i"
i=$((i + 1))
echo "after 2nd echo i = $i"
fi
done
In the above code, the i=$((i + 1)) in line (1) does not increment the variable used in the control loop. Since that command is in a pipe, it is executed in a subshell, and the variable i in the main shell is not incremented. It would probably be better to structure your code as:
#!/bin/sh
i=0
while [ $((i++)) -lt 4 ]; do
if [ -d "/Applications/Company Portal.app" ] && [ -d "/Applications/Google Chrome.app" ]; then
killall Dock
else
echo "Will tray again...."
sleep 10
fi
done
or
#!/bin/bash
for (( i = 0; i < 4; i++ )); do
if [ -d "/Applications/Company Portal.app" ] && [ -d "/Applications/Google Chrome.app" ]; then
killall Dock
else
echo "Will tray again...."
sleep 10
fi
done

Whiptail gauge for any number of shell commands

This is what I have so far and it works in the sense that it give a gauge and I can pass it as many commands as I want. The problem is the $COMMAND never actually executes on the shell.
#!/bin/bash
progressBar() {
declare TODO=("${#}")
NUM_TODO=${#TODO[*]}
STEP=$((100/NUM_TODO))
IDX=0
COUNTER=0
(
while :
do
cat <<EOF
XXX
$COUNTER
${TODO[$IDX]}
XXX
EOF
COMMAND="${TODO[$IDX]} &>/dev/null"
[[ $NUM_TODO -lt $IDX ]] && $COMMAND
(( IDX+=1 ))
(( COUNTER+=STEP ))
[ $COUNTER -gt 100 ] && break
sleep 1
done
) |
whiptail --title "Please wait..." --gauge "Please wait..." 6 70 0
}
progressBar \
"touch bla" \
"cp bla bla-`date +%Y%m%d%H%M`.backup"
I think [[ $NUM_TODO -lt $IDX ]] is round the wrong way, should be $IDX -lt $NUM_TODO.

How can we make a TIMER with (RESET) functionality in bash

I am very new to shell scripting, I have googled many things for this problem. But cannot find a perfect solution to it.
Problem is: It's a log monitoring code with "ERROR", Pattern
How can we make a TIMER in unix scripting. Support I have a timer of 5 minutes, and time keep on decreasing every 1 second. So I want to reset again the timer to 5 minute on certain condition (say $FREQUENCY of ERROR occurrence is greater then say for ex2).
#!/usr/bin/env bash
PATTERN='ERROR'
TIMER=300
FREQUENCY=2
while true;
do grep -i $PATTERN logfile > tmp_log
while [ $TIMER -gt 0 ]
do
sleep 1 &
printf " $TIMER \r" &
TIMER=$[$TIMER-1];
wait
done
if [[ $(wc -l <tmp_log) -ge $FREQUENCY ]]
then
TIMER=300
echo $TIMER
fi
sleep $TIMER
done
#!/bin/bash
PATTERN='ERROR'
TIMER=300
FREQUENCY=2
while true
do
while [ $TIMER -gt 0 ]
do
sleep 1
TIMER=$((TIMER - 1))
grep -i $PATTERN logfile > tmp_log
count=`wc -l tmp_log | awk '{ print $1 }'`
if [ $count -gt $FREQUENCY ]
then
TIMER=300
fi
done
break
done
This is good going for your case.

Loop increasement with 0.5

I have loop which writes to file, but I want to write each 0.5 value to the file. I tried with let count+=0.5 but that didn't work somehow. Is this possible?
Script:
#!/bin/bash
COUNTER=50
count=0
until [ $COUNTER -lt 20 ]; do
echo $count >> value.txt
echo COUNTER $COUNTER
let COUNTER-=1
let count+=0.5
sleep 1
done
bash doesn't do floating-point arithmetic natively; you need to use an external tool. -= is also not a supported operator.
until [ "$COUNTER" -lt 20 ]; do
printf "%0.1f\n" "$count"
echo "COUNTER $COUNTER"
count=$(bc <<< "$count + 0.5")
COUNTER=$((COUNTER - 1))
sleep 1
done > value.txt

How to terminate shell script after a specified time

I have a shell script that checks a file state.txt, deletes fit.bin if state.txt is empty.
I cron this at 2am, I will want the script to stop by 8am irrespective of the value of state.txt, any ideas?
#!/usr/bin/ksh
while true; do
if [[ -s ~/state.txt ]] ; then
echo
else
rm ~/fit.bin
exit
fi ;
sleep 961
done
exit
Here's a "model":
#!/bin/bash
while true
do
NOW=`date '+%H%M'`
echo "Time is now: $NOW"
sleep 3
if [ $NOW -ge 800 ]
then
echo "Bye bye!"
exit 0
fi
done
#!/bin/bash
while [ $( date '+%H%M' ) -lt 800 ]
do
[ -s "~/state.txt" ] && rm ~/fit.bin
sleep 961
done

Resources