reset number after each month

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

reset number after each month

Roshan
Hi Erman,

can you please check the script below

d=$(date "+%d%m%Y")
n=$(printf "%02d" "$((${n:-0} + 1))")
f="outputSMS_$d_$n.unl"
echo "outputSMS_${d}_${n}.unl"

It will display outputSMS_17052017_01.unl

Every time the script runs the value of n will increment

Thanks

Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
Script runs successfully.
What is your problem? What is your question?
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

Roshan
This post was updated on .
everytime I run the script(.sh file)

#formatting output.unl
d=$(date "+%d.%m.%Y")
n=$(printf "%02d" "$((${n:-0} + 1))")
f="outputSMS_${d}_${n}.unl"

only outputSMS_17.05.2017_01.unl is produced
Every time I run this script, it should create a new file with n parameter incremented

for example:
first time
outputSMS_17.05.2017_01.unl

second time
outputSMS_17.05.2017_02.unl

etc..
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

Roshan
If I run the script directly, it works

archicom>d=$(date "+%d.%m.%Y")
archicom>n=$(printf "%02d" "$((${n:-0} + 1))")
archicom>f="outputSMS_${d}_${n}.unl"
archicom>echo $f
outputSMS_17.05.2017_06.unl
archicom>echo $f
outputSMS_17.05.2017_06.unl
archicom>d=$(date "+%d.%m.%Y")
archicom>n=$(printf "%02d" "$((${n:-0} + 1))")
archicom>f="outputSMS_${d}_${n}.unl"
archicom>echo $f
outputSMS_17.05.2017_07.unl
archicom>d=$(date "+%d.%m.%Y")
archicom>n=$(printf "%02d" "$((${n:-0} + 1))")
archicom>f="outputSMS_${d}_${n}.unl"
archicom>echo $f
outputSMS_17.05.2017_08.unl
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
okay.. It is because its is different when you execute the command as a script.
In every script execution, your n variable gets initialized. Think like, every script execution is done in a seperate shell.
So, everytime you execute the script, your n becomes 1
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
I offer you the following solution , execute your script with "."

example:  . ./your_script

This way, your script will be executed it in the context of the current shell.

But if you disconnect and reconnect to the server , then again your "n" variable will be reinitialized.(as your shell be a new one)
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
if you want your n variable to not get initialized even after you disconnect from the server, then you need to modify the script, the design of it.. You need to store the value of $n in a file and read it in your script before setting a value to it.

Also, I guess you want your $n should be reset to 1, when the day changed. So you have to write a "if" for it as well.
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

Roshan
Thanks for support
I have modified my script and it is working now

n=`cat fileread.txt`
echo $n
d=$(date "+%d%m%Y")
n=$(printf "%02d" "$((${n} + 1))")
f="outputSMS_${d}_${n}.unl"
echo $f
echo $n
sed_param=s/Var1=.*/Var1=${n}/
sed -i "$sed_param" fileread.txt

Can you please suggest what I can include in my if statement?

Another way I can reset $n to 1 is clear fileread.txt and echo Var1=00 >fileread.txt in a script. Then I can schedule it in crontab.
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
write your "d"variable in to a file everytime you execute this script. Also everytime you run this script check it like you do for n. (do the check from the file before you write d to a file)
If d is changed, reset your "n" variable to 1.
This is what you need to do.
By doing this you will have;

17 May:
 outputSMS_17052017_01.unl
 outputSMS_17052017_02.unl
 outputSMS_17052017_03.unl
 outputSMS_17052017_04.unl
 outputSMS_17052017_05.unl

18 May:

 outputSMS_18052017_01.unl  --> as you see n goes back to 1
 outputSMS_18052017_02.unl
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

Roshan
If I compare the current date to a previous date(d2), if they are not equal, I overwrite the date d2 with actual date(d) and reset text file(value) to -01. Else proceed as usual
 
d=$(date "+%d.%m.%Y")
d2=21.05.2017
read n < fileread.txt
n=$(printf "%02d" "$((${n} + 1))")
f="outputSMS_${d}_${n}.unl"
 
if [${d} -ge ${d2}]
then
${d2}=${d}
echo -01 > fileread.txt
read n < fileread.txt
n=$(printf "%02d" "$((${n} + 1))")
f="outputSMS_${d}_${n}.unl"
for file in /archicom/data/dw*mob*.dat
do
cat $file >>/archicom/data/ctlfiles/$f
done
 
else
echo $n > fileroshan.txt
for file in /archicom/data/dw*mob*.dat
do
cat $file >>/archicom/data/ctlfiles/$f
done
fi
 
I am getting
.sh: line 7: [22.05.2017: command not found
 
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
The problematic line is "if [${d} -ge ${d2}] "

put spaces after [ and before ] . Do like the following;

if [ ${d} -ge ${d2} ]
Reply | Threaded
Open this post in threaded view
|

Re: reset number after each month

ErmanArslansOracleBlog
Administrator
You will still get error.
Modify your script.
you are putting characters in to d2, but you are trying to use equal or greate in the if statement. This won't happen, because -ge requires 2 numbers.
you will get ->  22.05.2017: integer expression expected

rewrite the script accordingly. Also why you are using greate or equal.. Time goes forward :) a simple string comparison is enough for this.