
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