
hi I need some help with this simple bash script, I ran the script twice, the first time it failed on some bash arithmetic, I then ran it again without changing anything and it worked, so I slightly puzzled as lines 8&11 are the same and sometimes it evaluates it and sometimes not. Steve --------------------------------------------------------------- [bin]$ cat world #!/usr/bin/bash cd /home/thorfinn/projects/image/clouds/ d=`date +%Y-%m-%d` mkdir $d cd $d pwd let tst=`date +%H`*3600+`date +%M`*60+`date +%S` echo $tst sleep 10 let tq=`date +%H`*3600+`date +%M`*60+`date +%S` while [ $tq -gt $tst ] # watch a web graphic of clouds and grab every 30 minutes # the while statement is to stop at midnight. --------------------------------------------------------------- [bin]$ ./world mkdir: cannot create directory `2012-08-19': File exists /home/thorfinn/projects/image/clouds/2012-08-19 ./world: line 8: let: tst=20*3600+42*60+09: value too great for base (error token is "09") ./world: line 13: [: 74539: unary operator expected [bin]$ ./world mkdir: cannot create directory `2012-08-19': File exists /home/thorfinn/projects/image/clouds/2012-08-19 74544 ---------------------------------------------------------------

On 2012-08-19 21:22, Steve Roylance wrote:
hi
I need some help with this simple bash script, I ran the script twice, the first time it failed on some bash arithmetic, I then ran it again without changing anything and it worked, so I slightly puzzled as lines 8&11 are the same and sometimes it evaluates it and sometimes not.
Hi Steve, I can't explain what you're seeing, but I counsel against using let; as far as I'm concerned, it's deprecated. I further counsel against using backticks for evaluation. I've rewritten lines of your script below to show how I'd do them while sticking to the same program logic.
Steve --------------------------------------------------------------- [bin]$ cat world #!/usr/bin/bash cd /home/thorfinn/projects/image/clouds/ d=`date +%Y-%m-%d`
d=$(date +%Y-%m-%d)
mkdir $d cd $d pwd
let tst=`date +%H`*3600+`date +%M`*60+`date +%S`
tst=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) ))
echo $tst sleep 10 let tq=`date +%H`*3600+`date +%M`*60+`date +%S`
tq=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) ))
while [ $tq -gt $tst ] # watch a web graphic of clouds and grab every 30 minutes # the while statement is to stop at midnight.
[...] In addition, I recommend a different approach to your script: --------------------------------------------------------------- #!/usr/bin/bash cd /home/thorfinn/projects/image/clouds/ d=$(date +%Y-%m-%d) # Don't complain if the directory already exists mkdir -p "$d" cd "$d" # I assume this is for diagnostics. pwd # Don't know whether this was just for diagnostics or what, but I've # left it in just in case. There should probably be a sleep within the # below while loop though, if you didn't have one originally. sleep 10 # While the current date is less than midnight tomorrow while [ $(date +%s) -lt $(date -d '12am tomorrow' +%s) ] do # watch a web graphic of clouds and grab every 30 minutes # the while statement is to stop at midnight. done --------------------------------------------------------------- Hope this helps. I'm happy to explain in more detail if this is unclear. -- Regards, Matthew Cengia

On 08/19/2012 10:22 PM, Matthew Cengia wrote:
On 2012-08-19 21:22, Steve Roylance wrote:
hi
I need some help with this simple bash script, I ran the script twice, the first time it failed on some bash arithmetic, I then ran it again without changing anything and it worked, so I slightly puzzled as lines 8&11 are the same and sometimes it evaluates it and sometimes not.
Hi Steve,
I can't explain what you're seeing, but I counsel against using let; as far as I'm concerned, it's deprecated. I further counsel against using backticks for evaluation. I've rewritten lines of your script below to show how I'd do them while sticking to the same program logic.
Steve --------------------------------------------------------------- [bin]$ cat world #!/usr/bin/bash cd /home/thorfinn/projects/image/clouds/ d=`date +%Y-%m-%d`
d=$(date +%Y-%m-%d)
mkdir $d cd $d pwd
let tst=`date +%H`*3600+`date +%M`*60+`date +%S`
tst=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) ))
echo $tst sleep 10 let tq=`date +%H`*3600+`date +%M`*60+`date +%S`
tq=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) ))
while [ $tq -gt $tst ] # watch a web graphic of clouds and grab every 30 minutes # the while statement is to stop at midnight.
[...]
In addition, I recommend a different approach to your script:
--------------------------------------------------------------- #!/usr/bin/bash cd /home/thorfinn/projects/image/clouds/ d=$(date +%Y-%m-%d)
# Don't complain if the directory already exists mkdir -p "$d" cd "$d" # I assume this is for diagnostics. pwd
# Don't know whether this was just for diagnostics or what, but I've # left it in just in case. There should probably be a sleep within the # below while loop though, if you didn't have one originally. sleep 10
# While the current date is less than midnight tomorrow while [ $(date +%s) -lt $(date -d '12am tomorrow' +%s) ] do # watch a web graphic of clouds and grab every 30 minutes # the while statement is to stop at midnight. done ---------------------------------------------------------------
Hope this helps. I'm happy to explain in more detail if this is unclear.
hi this is a Fedora 17 (32 bit) system and it is the inconsistent response from date that is throwing me at the moment, possibly the spaces is the command line are causing the error, or the leading 0 in the return to the date command. [tmp]$ tst=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) )); echo $tst bash: 00 * 3600 + 09: value too great for base (error token is "09") Steve

On Mon 2012-08-20 00:21:24 UTC+1000, Steve Roylance (roylance@corplink.com.au) wrote:
[tmp]$ tst=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) )); echo $tst bash: 00 * 3600 + 09: value too great for base (error token is "09")
09 is being treated as an octal value because of the leading zero. You can strip the leading zero with sed. ozzmosis@jet:~$ sec=09 ozzmosis@jet:~$ echo $sec 09 ozzmosis@jet:~$ sec=$(echo $sec | sed 's/0*//') ozzmosis@jet:~$ echo $sec 9

Steve Roylance wrote:
this is a Fedora 17 (32 bit) system and it is the inconsistent response from date that is throwing me at the moment, possibly the spaces is the command line are causing the error, or the leading 0 in the return to the date command.
[tmp]$ tst=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) )); echo $tst bash: 00 * 3600 + 09: value too great for base (error token is "09")
You're getting leading zeroes from date, which are being treated as octal by expr. Do your date-time math using unix time -- date +s to generate, date -d@NNNNN to turn it back into a human format. You can also do some arithmetic using date itself, e.g. date -d '1 week ago 1970' (untested--I never found a good document of the syntaxes date -d accepts, so I just guess each time).

On 20/08/2012, Trent W. Buck <trentbuck@gmail.com> wrote:
(untested--I never found a good document of the syntaxes date -d accepts, so I just guess each time).
In case you have not seen it, there is well-hidden documentation of this topic in the info system at: info -f coreutils -n 'Date input formats'

David wrote:
On 20/08/2012, Trent W. Buck <trentbuck@gmail.com> wrote:
(untested--I never found a good document of the syntaxes date -d accepts, so I just guess each time).
In case you have not seen it, there is well-hidden documentation of this topic in the info system at:
info -f coreutils -n 'Date input formats'
Yeah; I guess I really want an EBNF, like you see in RFCs. I have seen that info section, I usually forget about it when I'm trying to solve the immediate issue -- bad twb, no biscuit :-/

trentbuck@gmail.com
I never found a good document of the syntaxes date -d accepts, so I just guess each time.
http://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html http://www.gnu.org/software/coreutils/manual/html_node/Examples-of-date.html The above is pretty definitive. Or you could search for "relative GNU date formats" xor read the source. ALC
participants (6)
-
achalmers@westnet.com.au
-
andrew clarke
-
David
-
Matthew Cengia
-
Steve Roylance
-
Trent W. Buck