You are not logged in.

#1 2013-06-18 18:27:22

Divinorum
Member
Registered: 2011-08-16
Posts: 44

[Solved] Auto update of variables

I am modifying another's script by having it call on two variables. The variables need to be able to update automatically after they have been initialized. I sense the solution easy but all my search querys have not yielded any answers. The variables in question are $MONTH and $YEAR. Thank you.

#!/bin/bash
MONTH=$(date --date=now +%m)
YEAR=$(date --date=now +%g)

aurum ()
{
  if [ "$MONTH" == "01" ]; then
  printf "F$YEAR"
elif [ "$MONTH" == "02" ]; then
  printf "G$YEAR"
elif [ "$MONTH" == "03" ]; then
  printf "H$YEAR"
elif [ "$MONTH" == "04" ]; then
  printf "J$YEAR"
elif [ "$MONTH" == "05" ]; then
  printf "K$YEAR"
elif [ "$MONTH" == "06" ]; then
  printf "M$YEAR" 
elif [ "$MONTH" == "07" ]; then
  printf "N$YEAR"
elif [ "$MONTH" == "08" ]; then
  printf "Q$YEAR"
elif [ "$MONTH" == "09" ]; then
  printf "U$YEAR"
elif [ "$MONTH" == "10" ]; then
  printf "V$YEAR"
elif [ "$MONTH" == "11" ]; then
  printf "X$YEAR"
elif [ "$MONTH" == "12" ]; then
  printf "Z$YEAR"
fi
}

C_YAHOO_FINANCE_URL="http://download.finance.yahoo.com/d/quotes.csv?f=sl1k2&s=SI$(aurum).CMX+GC$(aurum).CMX+CL$(aurum).NYM+^TNX+^VIX+^GSPC+^IXIC+^FTSE+^GDAXI+^N225+^HSI"
while true
do
  TICKER=$(curl -s "$C_YAHOO_FINANCE_URL")
  clear
  echo "$TICKER" | while read line;
  do
    if [[ $line == *+* ]]
    then
      echo -e "\e[00;32m$line" | tr -s ',^"' ' '
    else
      echo -e "\e[00;31m$line" | tr -s ',^"' ' '
    fi
  done
  sleep 20s
done

Last edited by Divinorum (2013-06-18 23:57:07)

Offline

#2 2013-06-18 18:31:52

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,485
Website

Re: [Solved] Auto update of variables

Wow… that's a lot of elifs… Why not just use a case statement?

aurum () {
  case "$MONTH" in
    01) printf "F$YEAR";;
    02) printf "G$YEAR";;
    03) printf "H$YEAR";;
    04) printf "J$YEAR";;
    05) printf "K$YEAR";;
    06) printf "M$YEAR";;
    07) printf "N$YEAR";;
    08) printf "Q$YEAR";;
    09) printf "U$YEAR";;
    10) printf "V$YEAR";;
    11) printf "X$YEAR";;
    12) printf "Z$YEAR";;
  esac
}

See? Doesn't that just feel cleaner? If there's some clear pattern of the letters preceding $YEAR, you could make this even cleaner.

All the best,

-HG

Last edited by HalosGhost (2013-06-18 18:40:07)


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#3 2013-06-18 18:42:53

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,404
Website

Re: [Solved] Auto update of variables

Divinorum wrote:

I am modifying another's script by having it call on two variables. The variables need to be able to update automatically after they have been initialized. I sense the solution easy but all my search querys have not yielded any answers. The variables in question are $MONTH and $YEAR.

What do you mean by "having it call on two variables"? What do you mean by "update automatically"? If you want "MONTH" to be updated to the current month they you would just do "MONTH=$(date --date=now +%m)" again, right?

Offline

#4 2013-06-18 19:29:21

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,012

Re: [Solved] Auto update of variables

HalosGhost makes a good point.
This is a good site on bash variables.

You set the MONTH variable when the script starts.
You run the function aurum, which checks what the month is, three times every 20 seconds when C_YAHOO_FINANCE_URL is used, which for a 30 day month is 388800 times a month. I'd try a different approach 'cause that's alot of elifs to go through.


You're just jealous because the voices only talk to me.

Offline

#5 2013-06-18 20:03:27

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,012

Re: [Solved] Auto update of variables

Couldn't help myself...
Checks for a month change by seeing if the days date is less than the last time.
Untested to how it reacts with dates %e being space padded though

#!/bin/bash

# Stock ticker using yahoo

declare this_month=""
declare -i old_day

find_month () {
  MONTH=$(date --date=now +%m)
  YEAR=$(date --date=now +%g)

  case "$MONTH" in
    01) this_month="F$YEAR";;
    02) this_month="G$YEAR";;
    03) this_month="H$YEAR";;
    04) this_month="J$YEAR";;
    05) this_month="K$YEAR";;
    06) this_month="M$YEAR";;
    07) this_month="N$YEAR";;
    08) this_month="Q$YEAR";;
    09) this_month="U$YEAR";;
    10) this_month="V$YEAR";;
    11) this_month="X$YEAR";;
    12) this_month="Z$YEAR";;
  esac
}

find_month

C_YAHOO_FINANCE_URL="http://download.finance.yahoo.com/d/quotes.csv?f=sl1k2&s=SI$(this_month=).CMX+GC$(this_month=).CMX+CL$(this_month=).NYM+^TNX+^VIX+^GSPC+^IXIC+^FTSE+^GDAXI+^N225+^HSI"
while true;do
  old_day=$day
  day="$(date --date=now +%e)"
  if (( "$old_day" < "$day" )); then
    find_month
  fi
  TICKER=$(curl -s "$C_YAHOO_FINANCE_URL")
  clear
  echo "$TICKER" | while read line;
  do
    if [[ $line == *+* ]]
    then
      echo -e "\e[00;32m$line" | tr -s ',^"' ' '
    else
      echo -e "\e[00;31m$line" | tr -s ',^"' ' '
    fi
  done
  sleep 20s
done
exit 0

You're just jealous because the voices only talk to me.

Offline

#6 2013-06-18 21:37:54

Divinorum
Member
Registered: 2011-08-16
Posts: 44

Re: [Solved] Auto update of variables

@ Halos Ghost: Thanks for referring me to case. Seeing it like this helps a lot. I guess that takes care of streamlining the script.

@ drcouzelis: What I mean by automatic updates is that the month and year values change while the script is running. I.e. the MONTH and YEAR values used in the function are initialized on June 30th and then at 00:00:01 on July 1st those values are updated to produce 07 in stead of 06, thus changing the output from "M13" to "N13".

This is necessary as the ticker symbols for gold, silver and crude oil change each month and year. I.e. SIM13.CMX == COMEX silver June 2013, CLN14.NYM ==  NYMEX crude oil July 2014.

@ moetunes: Good website reference! Should come in handy as I continue to learn bash. However, I am unclear about the part where you say that the function calls on the MONTH variable three times every 20s. One of my friends/bash mentors pointed out the faults of my scripting and stated that the MONTH and YEAR values are never updated after the script has been initialized.

That I changed to using case instead doesn't fix the original problem assuming my friend is correct. Therefore I can not verify your adjustments in that regard but it does not work in terms of printing the M13 for this month in silver, gold and crude. The terminal prints "SI.CMX". So the question I should have asked: Do the variables update after initalized and if not what will make them do so?

Offline

#7 2013-06-18 21:38:38

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

Really, case?  Array's are your friend:

month_name=(Zero F G H J K M N Q U V X Z)
aurum() {
    echo ${month_name[$MONTH]}$YEAR
}

And this, of course, nullifies the need for a separate function to do this - just use ${month_name[...]} wherever you need it.

As for the original problem, why not just restart the script once a mont??

If you really want it automated, make a cron job that kills the script and restarts it on the first second of the first day of every month.

And the curl to variable then echo variable to pipe is odd.  Just "curl url... | while read line".  I see the 'clear', which makes me think you don't want to see the curl progress.  But if that is the reason, it still would flash on the screen every 20 seconds.  Instead, just redirect curl's stderr to /dev/null.  EDIT: curl does have such a flag:

curl -s <someurlbits>${month_name[$MONTH]}$YEAR<otherurlbits> | while read line

This single line will cut 30 some lines from your original.

Last edited by Trilby (2013-06-18 21:53:05)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#8 2013-06-18 23:30:57

Divinorum
Member
Registered: 2011-08-16
Posts: 44

Re: [Solved] Auto update of variables

Trilby wrote:

Really, case?  Array's are your friend:

month_name=(Zero F G H J K M N Q U V X Z)
aurum() {
    echo ${month_name[$MONTH]}$YEAR
}

And this, of course, nullifies the need for a separate function to do this - just use ${month_name[...]} wherever you need it.

Many thanks this was a great fix. Nice and clean.

Trilby wrote:

As for the original problem, why not just restart the script once a mont??

If you really want it automated, make a cron job that kills the script and restarts it on the first second of the first day of every month.

That was my first solution but my friend challanged me to consolidate all the work in this one script. Also, if there's a fix for self-updating variables in a script doesn't that increase its portability? I know many people who would be interested in having a command line market ticker but having to add crons for gold, silver, oil and other commodities diminishes the appeal. Mostly I am interested in the challange of editing the script to have self-updating variables. I don't know if this is possible but I am pursuing it.


Trilby wrote:

And the curl to variable then echo variable to pipe is odd.  Just "curl url... | while read line".  I see the 'clear', which makes me think you don't want to see the curl progress.  But if that is the reason, it still would flash on the screen every 20 seconds.  Instead, just redirect curl's stderr to /dev/null.  EDIT: curl does have such a flag:

curl -s <someurlbits>${month_name[$MONTH]}$YEAR<otherurlbits> | while read line

This single line will cut 30 some lines from your original.

The flash was a seamless blink on each update that rarely caught my eye. Your solution does remove that but now the shell is not cleared on each update and thus the old quotes stay on top of the new. Perhaps I am missing something basic.

Offline

#9 2013-06-18 23:39:25

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

Ah, I guess you'd still need the clear then.  But with the "-s" flag it shoud still be better without the progress indicator.

As for wanting to do this all in one file - you *could* do it, but it'd be horribly ugly and inefficient.  You're really just have to run "date" again, every time through the loop.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#10 2013-06-18 23:55:44

Divinorum
Member
Registered: 2011-08-16
Posts: 44

Re: [Solved] Auto update of variables

I understand. It is strange that comex and nymex don't assign permanent symbols to the commodities like NYSE or TOPIX does with equities. The best solution would be if yahoo finance gave quotes of the spot price of physical gold and silver rather than the paper ETF market. Perhaps I will explore pulling quotes from another provider of quotes like Kitco.

Many thanks again I learned a lot especially being introduced to arrays and case.

Offline

#11 2013-06-19 04:48:41

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,485
Website

Re: [Solved] Auto update of variables

Trilby wrote:

Really, case?  Array's are your friend:

month_name=(Zero F G H J K M N Q U V X Z)
aurum() {
    echo ${month_name[$MONTH]}$YEAR
}

And this, of course, nullifies the need for a separate function to do this - just use ${month_name[...]} wherever you need it.

Oooh, pretty! Trilby, you'll always be taking me to school.

All the best,

-HG


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#12 2013-06-19 08:48:26

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,012

Re: [Solved] Auto update of variables

"However, I am unclear about the part where you say that the function calls on the MONTH variable three times every 20s."

"You set the MONTH variable when the script starts.
You run the function aurum, which checks what the month is, three times every 20 seconds when C_YAHOO_FINANCE_URL is used"


You're just jealous because the voices only talk to me.

Offline

#13 2013-06-19 09:20:39

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,837

Re: [Solved] Auto update of variables

Trilby wrote:

...As for wanting to do this all in one file - you *could* do it, but it'd be horribly ugly and inefficient.  You're really just have to run "date" again, every time through the loop.

It would be possible to have a counter that counts down to midnight and update the date when it reaches zero.

1) On start-up set the counter equal to Ts/20 where Ts = Midnight - Now (in seconds)
2) After midnight set counter equal to Ts/20 where Ts = 86,400 (no of seconds in 24 hours)

Also, it's possible to pass the output of the date call into an array:

$ monyear=($(date --date=now +"%m %g"))
$ echo ${monyear[0]}
06
$ echo ${monyear[1]}
13

Both fairly ugly, but it will vastly reduce the number of date calls.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#14 2013-06-19 10:59:33

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

One problem with your first suggestion, skanky, is that "sleep" has no gaurantee of being precise.  So if you just count until an update in a script that is expected to run for months at a time, the counter value will drift off.  Just because it loops every 20 seconds doesn't mean it can't lead to a big error; if the counter drifts off by even one count, the date check could be done 20 seconds *before* midnight rather than after - then the date would be wrong for the entire day.  No matter how this is written, the drift will lead to the precision of the date variable only being as good as how often it is refreshed: if you have a counter to update DATE once a day, the DATE variable could be off by a full day.

Last edited by Trilby (2013-06-19 11:00:31)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#15 2013-06-19 12:37:10

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,092
Website

Re: [Solved] Auto update of variables

Trilby wrote:

Really, case?  Array's are your friend:

month_name=(Zero F G H J K M N Q U V X Z)
aurum() {
    echo ${month_name[$MONTH]}$YEAR
}

And this, of course, nullifies the need for a separate function to do this - just use ${month_name[...]} wherever you need it.

Note that this will fail as is once you get to August because the output from date(1) is zero padded

$  month_name=(Zero F G H J K M N Q U V X Z); MONTH=08; echo "${month_name[MONTH]}"
bash: 09: value too great for base (error token is "08")

You'd need to "cast" the var to base 10, i.e. "${month_name[10#$MONTH]}"

Offline

#16 2013-06-19 12:54:40

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,837

Re: [Solved] Auto update of variables

Trilby wrote:

One problem with your first suggestion, skanky, is that "sleep" has no gaurantee of being precise.  So if you just count until an update in a script that is expected to run for months at a time, the counter value will drift off.  Just because it loops every 20 seconds doesn't mean it can't lead to a big error; if the counter drifts off by even one count, the date check could be done 20 seconds *before* midnight rather than after - then the date would be wrong for the entire day.  No matter how this is written, the drift will lead to the precision of the date variable only being as good as how often it is refreshed: if you have a counter to update DATE once a day, the DATE variable could be off by a full day.

Ah, in that case you could re-calculate it each day (or once every hour) to correct itself (rather than hard coding the counter steps). You might still get some drift, but it should be possible to keep it inside some tolerable bound. It would still result in a check an hour and it's easier to re-start the script each day/month, of course.

Or use a different language with a built-in timer.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#17 2013-06-19 13:40:34

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

Recalculate it once a day?  How would you know when to recalculate it?  You'd need to use the date command, and once you do that, correcting the counter would be irrelevant as the only purpose of the counter is to avoid using the date command.  As for drift being within a tolerable bound, what is tolerable?  If you set the wrong date, the script wont work.

@falconindy, that depends on how you ask date for the month.  By default it prints lots of stuff that wouldn't be needed, but it can give the non-zero-padded month with `date +%-m`

Last edited by Trilby (2013-06-19 13:45:38)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#18 2013-06-19 14:24:15

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,837

Re: [Solved] Auto update of variables

Trilby wrote:

Recalculate it once a day?  How would you know when to recalculate it?  You'd need to use the date command, and once you do that, correcting the counter would be irrelevant as the only purpose of the counter is to avoid using the date command.  As for drift being within a tolerable bound, what is tolerable?  If you set the wrong date, the script wont work.

Yeah sorry, it was only a half-formed thought really (partly using two counters, the short one would exhibit less drift). I did also wonder about when you do the update, if the day is not changed (you'd need to keep track of that too obviously) to re-calculate the update time and based on the new current time. That way the only error would be a late update, which is where the tolerable bounds comes in - how late is too late. As I agreed though, fairly ugly and messy.

Anyway, it's all a bit academic and I probably can't get a good mental grip on it until/unless I tried to write it. Not really any chance of that though, so I'll concede it's probably not worth pursuing this line. wink


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#19 2013-06-19 14:26:04

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

If you want to have some nice formatting and play with colors, check this out:

#!/bin/bash

U1="http://download.finance.yahoo.com/d/quotes.csv?f=sl1k2&s=SI"
U2=".CMX+GC"
U3=".CMX+CL"
U4=".NYM+^TNX+^VIX+^GSPC+^IXIC+^FTSE+^GDAXI+^N225+^HSI"
month=(Zero F G H J K M N Q U V X Z)

while true; do
	DAT="${month[$(date +%-m)]}$(date +%g)"
	clear
	curl -s "${U1}${DAT}${U2}${DAT}${U3}${DAT}${U4}" | while read line; do
	line=${line/^/}
    if [[ $line == *+* ]]; then
		printf "\e[1;32m%-12s \e[0;33m%10s \e[36m%10s \e[37m%c> \e[32m%6s %s\n" ${line//[,\"]/ }
	else
		printf "\e[1;31m%-12s \e[0;33m%10s \e[36m%10s \e[37m%c> \e[31m%6s %s\n" ${line//[,\"]/ }
    fi
	done
	sleep 20s
done

InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#20 2013-06-20 00:47:46

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 986

Re: [Solved] Auto update of variables

I don't think skanky was completely off base -- it wouldn't be pretty but it's still quite possible to get reasonable precision, by sleeping for a fraction of the remaining time and readjusting the timer until within a particular threshold of midnight. You're already calling out to an external API via HTTP, so it's not like you can do much better than accuracy to the nearest second (could be less, depending on the speed of your connection).

With that disclaimer, it occurred to me there's another way to approach the problem:

on_update() {
    month=$(date +%-m)
}
trap on_update USR1

echo "kill -USR1 $$" | at 12:00 AM

My tests seemed to indicate this kind of approach would work, but I'm not much at bash, so corrections welcome.

Offline

#21 2013-06-20 00:52:51

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

Trent wrote:

... and readjusting the timer

readjusting how?  To adjust you need a frame of reference against which to compare.  Without running the date command, or some other source of date information, there is nothing to compare to - and once you have something to compare with, there is no need for the counter ... you have the actual current time/date.

count=1
MONTH=$(date +%-m)
while true; do
    #some stuff here
    sleep 20;
    let count=$count+1
    if [[ $count -gt 4320 ]]; then ## one "day" has passed
        MONTH=$(date +%-m)
        let counter=1
    if
done

With this *any* error in the precision of "sleep" could lead to the conditional triggering before 24 hours, which means it could trigger at 11:59 rather than midnight.  And there would be no recheck untill the next conditional passes (11:58 the next day).  So for that entire 23hr and 58min period, the month would be wrong.

How could you check if there was a loss in count?

...
   sleep 20;
   let count=$count+1
   if [[ -magicCheckOfVariable $count]]; then
      let count=$count+1
   fi
...

What could possibly go in place of that magic check that wouldn't need some real reference point for the date/time?

This is all rather trivial though for the current purposes.  The resource use of running 'date' once every 20 seconds is a drop in the bucket when we're already curling a url every 20 seconds.

Last edited by Trilby (2013-06-20 01:02:34)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#22 2013-06-20 02:00:33

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 986

Re: [Solved] Auto update of variables

You still have to use date, naturally, but not 4300 times a day. Which was all I was trying to say, but maybe I misread the discussion.

Offline

#23 2013-06-20 02:02:46

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

Yes, you would have to.  If you use it once a day, the MONTH variable could be off by a full day.  If you use it twice a day, the MONTH variable could be off by half a day, etc.  So what then is the purpose of the counter if the MONTH variable will only be as accurate as if no counter was used at all.  If you call date 4300 times a day, MONTH will be accurate.  If you call date 430 times per day, MONTH could be off for 1/10th a day.

Last edited by Trilby (2013-06-20 02:03:56)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#24 2013-06-20 02:34:45

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 986

Re: [Solved] Auto update of variables

Trilby wrote:

With this *any* error in the precision of "sleep" could lead to the conditional triggering before 24 hours, which means it could trigger at 11:59 rather than midnight.  And there would be no recheck untill the next conditional passes (11:58 the next day).  So for that entire 23hr and 58min period, the month would be wrong.

How could you check if there was a loss in count?

Since you're running date anyway, it's trivial to check the 11:59 case -- just test whether the date has changed since the last check, and if not, wait just a little longer before checking again.

For a refinement of the same solution, on each update, wait 75% of the remaining time in the day and update the month when you come "close enough" to the next day. 8-9 calls to date over the course of a day would get you within a reasonable interval.

Obviously this would only be worthwhile if date was costly, which -- for a solution that uses bash -- it probably isn't.

Offline

#25 2013-06-20 02:44:42

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,382
Website

Re: [Solved] Auto update of variables

let counter=1
while true; do
   #do stuff
   let counter=$counter+1
   if [[ $counter -gt 4250 ]] && [[ $MONTH != $(date +%-m) ]]; then
      MONTH=$(date +%-m)
      let counter=1
   fi
   sleep 20
done

Eh, I suppose that might kinda' work.  But what should the 4250 value *actually* be?  You'd have to estimate just how much drift you could expect from a sleep in a loop.  And you'd have to estimate that many times as it would vary.  Then do you pick a value that would work 75% of the time?  Or 90%, 95%, 99%?

This requires an extra variable, two conditionals, and either yet another variable or two calls to date (as in my example) every time the conditionals pass.  All this plus some decided upon tolerable level of error, just to avoid calling date ... doesn't seem like a bargain to me.

Also the time for all those other things in the loop will add more variability than the sleep command.  Sure they may take a small fraction of a second - but a small fraction of a second times 4300 loops ... assuming .1 second for all those other steps (with is a gross underestimate with curl in there) this would add up to about 8 hours!  An 8 hour drift per day .... that's just not even keeping time.  So 4300 is *way* too many loops.  But then how many would be right?  It depends on download times.  I suspect if you took the sleep 20 out of this, and just ran the loop 4300 times, it could easily take a good part of a day.

Sleep in a loop is just *not* a way to keep track of time.

Far simpler, and just as accurate as counting sleeps:

MONTH=$RANDOM

Last edited by Trilby (2013-06-20 03:00:00)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

Board footer

Powered by FluxBB