Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
1294 posts
|
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 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
Script runs successfully.
What is your problem? What is your question? |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
1294 posts
|
This post was updated on May 17, 2017; 11:47am.
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.. |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
1294 posts
|
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 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
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 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
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) |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
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. |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
1294 posts
|
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. |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
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 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
1294 posts
|
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 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
The problematic line is "if [${d} -ge ${d2}] "
put spaces after [ and before ] . Do like the following; if [ ${d} -ge ${d2} ] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
5727 posts
|
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. |
Free forum by Nabble | Edit this page |