Using sed
in the code below it will increment x
by 1
within the script, each time the script is run, up to 50
and then set x
back to 1
. You can set the command to process the logfile in the else
branch of the if
statement along with whatever other code you want to run in each branch.
#!/bin/bash
x=1
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
Here I run the script to show x
gets incremented and reset back to 1
after 50 runs:
$ cat testscript
#!/bin/bash
x=1
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$ ./testscript
$ cat testscript
#!/bin/bash
x=2
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$
As you can see x=1
has became x=2
within the script.
I now manually set x=2
to x=50
and saved the script to show it resets to x=1
.
$ cat testscript
#!/bin/bash
x=50
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$ ./testscript
$ cat testscript
#!/bin/bash
x=1
y=$((x+1))
z=1
if [ $x -lt 50 ]; then
# Do something...
sed -i -e "s/x=$x/x=$y/" "$0"
else
# Do something...
# Delete logfile...
sed -i -e "s/x=$x/x=$z/" "$0"
fi
$