From: Ben Bacarisse on
Stu <beefstu350(a)hotmail.com> writes:

> On Jul 7, 1:57 pm, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:
>> [...]  You could also, in bash, use an
>> array:
>>
>>   cpu=($(tr ':' ' ' <<<$cpu_time))
>>
>> to get ${cpu[0]} etc.
>>
>> If you don't want to run even tr, use a function:
>>
>>   function split
>>   {
>>       IFS=':'
>>       set "$1"
>>       echo "$1" "$2" "$3"
>>   }
>>
>>   cpu=($(split "$cpu_time"))
<snip>
> Ben thanks but your examples dont apper to work in ksh, which is the
> shell I am using.

No, indeed. That's why I said "in bash"!

I think this is portable across shells:

function set_cpu_time
{
cpu_hh="$1"
cpu_mm="$2"
cpu_ss="$3"
}

and then call:

set_cpu_time $(split "$cpu_time")

<snip>
--
Ben.
From: Ben Bacarisse on
Janis Papanagnou <janis_papanagnou(a)hotmail.com> writes:
<snip>
> But for your task I'd suggest to resort to Icarus' last suggestion which is
> most simple and portable.

Icarus' solution assumed the time came from a data command whose format
could be altered to get the desired effect. That may fit the bill here
but it is not a solution to the question as asked.

<snip>
--
Ben.
From: Janis Papanagnou on
On 09/07/10 17:17, Ben Bacarisse wrote:
> Janis Papanagnou <janis_papanagnou(a)hotmail.com> writes:
> <snip>
>> But for your task I'd suggest to resort to Icarus' last suggestion which is
>> most simple and portable.
>
> Icarus' solution assumed the time came from a data command whose format
> could be altered to get the desired effect. That may fit the bill here
> but it is not a solution to the question as asked.

You're right. Here's one using eval without assuming a date command

eval $( printf "cpu_hh=%s cpu_mm=%s cpu_sec=%s" ${cpu_time//:/ } )

though non-POSIX because of the variable substitution it works only
with modern shells.

Janis

>
> <snip>

From: Stu on
On Jul 9, 9:20 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> On 09/07/10 17:17, Ben Bacarisse wrote:
>
> > Janis Papanagnou <janis_papanag...(a)hotmail.com> writes:
> > <snip>
> >> But for your task I'd suggest to resort to Icarus' last suggestion which is
> >> most simple and portable.
>
> > Icarus' solution assumed the time came from a data command whose format
> > could be altered to get the desired effect.  That may fit the bill here
> > but it is not a solution to the question as asked.
>
> You're right. Here's one using eval without assuming a date command
>
>   eval $( printf "cpu_hh=%s cpu_mm=%s cpu_sec=%s" ${cpu_time//:/ } )
>
> though non-POSIX because of the variable substitution it works only
> with modern shells.
>
> Janis
>
>
>
>
>
> > <snip>- Hide quoted text -
>
> - Show quoted text -

What would be considered a modern shell, bash? This, does not work
with ksh. I already found
the solution, but I am very interested in other methods to skin the
perverbial cat.


cpu_time=23:12:56
eval $(printf "cpu_hh=%s cpu_mm=%s cpu_sec=%s" ${cpu_time//:/})

${cpu_time//:/}: 0403-011 The specified substitution is not valid for
this command.
From: Janis Papanagnou on
On 12/07/10 21:41, Stu wrote:
> On Jul 9, 9:20 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> wrote:
>> On 09/07/10 17:17, Ben Bacarisse wrote:
>>
>>> Janis Papanagnou <janis_papanag...(a)hotmail.com> writes:
>>> <snip>
>>>> But for your task I'd suggest to resort to Icarus' last suggestion which is
>>>> most simple and portable.
>>
>>> Icarus' solution assumed the time came from a data command whose format
>>> could be altered to get the desired effect. That may fit the bill here
>>> but it is not a solution to the question as asked.
>>
>> You're right. Here's one using eval without assuming a date command
>>
>> eval $( printf "cpu_hh=%s cpu_mm=%s cpu_sec=%s" ${cpu_time//:/ } )
>>
>> though non-POSIX because of the variable substitution it works only
>> with modern shells.
>>
>> Janis
>>
>>
>>
>>
>>
>>> <snip>- Hide quoted text -
>>
>> - Show quoted text -
>
> What would be considered a modern shell, bash? This, does not work
> with ksh.

It doesn't with a ksh88, it does with a ksh93, and bash, and zsh.
ksh88 is indeed not a "modern" shell; it's very old (though still
existing on commercial Unixes).

For old shells like ksh88, or bourne shell, or standard shells use
the upthread posted approach with IFS=: read which will certainly
work on those non-"modern" shells.

Janis

> I already found
> the solution, but I am very interested in other methods to skin the
> perverbial cat.
>
>
> cpu_time=23:12:56
> eval $(printf "cpu_hh=%s cpu_mm=%s cpu_sec=%s" ${cpu_time//:/})
>
> ${cpu_time//:/}: 0403-011 The specified substitution is not valid for
> this command.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: rm in a regular expression
Next: Pattern matching