From: Dave on
I was looking back at the thread:
"getting time since epoch using shell commands? (Bourne Shell)"

http://groups.google.com/group/comp.unix.shell/browse_thread/thread/121344445ef2c75a/e4903a12920f4c?hl=en&ie=UTF-8&q=seconds+since+epoch+comp.unix.shell

and in particular this post

http://groups.google.com/group/comp.unix.shell/msg/fe44b83b56478e36?hl=en

where this snippet of code was posted.

if type env >/dev/null 2>&1 ; then
set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
else
set -- `date -u '+%Y %j %H %M %S'`
fi


I'm trying to understand

1) Why these three variables are sometimes set, and on others not set?

2) Why it is ever necessary to set them? I would have expected the 'date'
command to give numeric data only, and all relative to GMT, so

3) If they are set in a script, with
set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`

would one need to save their old values first, then restore them, to save
messing up someone's environment? Since the script I want will only get print
the seconds since the epoch, then exit, I doubt this is necessary, as the
settings of the 3 variables will not be needed in that script. But I do not wish
to mess up someone's environment variables.

4) What does 'set --' do? The man page says

" -- Does not change any of the flags. This option is use-
ful in setting $1 to -."


but I just do not understand this. Can someone explain it in a bit more detail.


Dave

--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
From: Bill Marcum on
On 2009-12-13, Dave <foo(a)coo.com> wrote:
>
> if type env >/dev/null 2>&1 ; then
> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
> else
> set -- `date -u '+%Y %j %H %M %S'`
> fi
>
>
> I'm trying to understand
>
> 1) Why these three variables are sometimes set, and on others not set?
>
It might not be necessary in this case, but sometimes people do things
because it's a good habit. The locale variables can change the output of
'date', so it's a good idea to always set them if your script uses the output.

> 2) Why it is ever necessary to set them? I would have expected the 'date'
> command to give numeric data only, and all relative to GMT, so
>
> 3) If they are set in a script, with
> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
>
> would one need to save their old values first, then restore them, to
> save messing up someone's environment? Since the script I want will

When variables are set as part of a command, not as a separate command,
those variables are considered local to that command.

> 4) What does 'set --' do? The man page says
>
> " -- Does not change any of the flags. This option is use-
> ful in setting $1 to -."
>
Again in this case it's just a good habit, but in case any of the arguments
to "set" begin with "-", the "--" prevents those arguments being interpreted
as shell options.

From: Dave on
Bill Marcum wrote:
> On 2009-12-13, Dave <foo(a)coo.com> wrote:
>> if type env >/dev/null 2>&1 ; then
>> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
>> else
>> set -- `date -u '+%Y %j %H %M %S'`
>> fi
>>
>>
>> I'm trying to understand
>>
>> 1) Why these three variables are sometimes set, and on others not set?
>>
> It might not be necessary in this case, but sometimes people do things
> because it's a good habit. The locale variables can change the output of
> 'date', so it's a good idea to always set them if your script uses the output.


Could they change they output of these specific numeric values, which are

1) Year including the century
2) Day of the year (1-365)
3) Month (1-12)
3) Hour (0-23)
4) Min (0-59)
5) Sec (0-59)

If not, I'd rather not set set LC_ALL, LC_TIME and LANG.

I appreciate your point that in general it is a good idea, but I want to write a
script and someone else has to 'review' it before it may be included in a
project. The reviewer is not likely to know a lot about shell scripts, so being
too pedantic will likely introduce more concerns on his/her part, than something
which looks pretty simple for him/her to understand.

I don't want to make that process more difficult to understand than need be,
which it will be, if I include unnecessary complications.

>> 2) Why it is ever necessary to set them? I would have expected the 'date'
>> command to give numeric data only, and all relative to GMT, so
>>
>> 3) If they are set in a script, with
>> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
>>
>> would one need to save their old values first, then restore them, to
>> save messing up someone's environment? Since the script I want will
>
> When variables are set as part of a command, not as a separate command,
> those variables are considered local to that command.

But there are three separate command in that script

1) The 'set'
2) Compute 'DAYS'
3) Compute 'SECONDS'


But is it safe to assume that once the script has exited, it has not changed the
environment in any way?

>> 4) What does 'set --' do? The man page says
>>
>> " -- Does not change any of the flags. This option is use-
>> ful in setting $1 to -."
>>
> Again in this case it's just a good habit, but in case any of the arguments
> to "set" begin with "-", the "--" prevents those arguments being interpreted
> as shell options.


Fair enough. But in this case, that is probably not necessary, as none of these
can become negative.


--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
From: Eric on
On 2009-12-13, Dave <foo(a)coo.com> wrote:
> I was looking back at the thread:
> "getting time since epoch using shell commands? (Bourne Shell)"
>
> http://groups.google.com/group/comp.unix.shell/browse_thread/thread/121344445ef2c75a/e4903a12920f4c?hl=en&ie=UTF-8&q=seconds+since+epoch+comp.unix.shell
>
> and in particular this post
>
> http://groups.google.com/group/comp.unix.shell/msg/fe44b83b56478e36?hl=en
>
> where this snippet of code was posted.
>
> if type env >/dev/null 2>&1 ; then
> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
> else
> set -- `date -u '+%Y %j %H %M %S'`
> fi
>
>
> I'm trying to understand
>
> 1) Why these three variables are sometimes set, and on others not set?

The code is saying "if env is available, use it; if not, there is no
point trying". See answer to 3) below.

>
> 2) Why it is ever necessary to set them? I would have expected the 'date'
> command to give numeric data only, and all relative to GMT, so

date _may_ modify its output for some values of those environment
variables, and this is not wanted, and setting them all to C is one way
to avoid the modification.

>
> 3) If they are set in a script, with
> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`
>
> would one need to save their old values first, then restore them, to save
> messing up someone's environment? Since the script I want will only get print
> the seconds since the epoch, then exit, I doubt this is necessary, as the
> settings of the 3 variables will not be needed in that script. But I do not wish
> to mess up someone's environment variables.

This is what env is for - it sets the environment variables it is given
and then runs the specified command. It does this in its own process,
so the environment it creates applies _only_ to the command it runs.

>
> 4) What does 'set --' do? The man page says
>
> " -- Does not change any of the flags. This option is use-
> ful in setting $1 to -."
>
>
> but I just do not understand this. Can someone explain it in a bit more detail.
>

The -- means "ignore me, but nothing from here on is an option even if
it starts with a -".

Eric
From: Sven Mascheck on
Eric wrote:
> On 2009-12-13, Dave <foo(a)coo.com> wrote:

>> set -- `env LC_ALL=C LC_TIME=C LANG=C date -u '+%Y %j %H %M %S'`

>> [...] to save messing up someone's environment?

> This is what env is for - it sets the environment variables it is given
> and then runs the specified command. It does this in its own process,
> so the environment it creates applies _only_ to the command it runs.

Pedantic footnote: in the above, there's no need to use
"env var=value command", because "var=value command" behaves the same
(ignoring other features of env for now).