From: Bill Marcum on
On 2010-02-01, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote:
> Hi all,
>
> I want to do the following simple addition operations under bash by
> using expr:
>
> 1- I've two variables, i.e., aa and bb.
>
> 2- If the addition of aa + bb is little than 10, it should give me
> the output with a leading zero like this: 01, 08, 09 or so.
>
> 3- If the addition of aa + bb is large than 10, it should give me the
> output without a leading zero.
>
> What should I do?
>
> Thanks in advance.

printf "%02d\n" $(expr $aa + $bb)
or better:
printf "%02d\n" $((aa+bb))

From: Ivan Shmakov on
>>>>> "SL" == Sidney Lambe <sidneylambe(a)nospam.invalid> writes:

[...]

SL> Bash:

SL> (( $aa + $bb = cc }}

SL> if [ "$cc" -lt 10 ]

SL> then cc=$(echo "$cc" | sed 's/.*/0&/')

SL> fi

SL> There are surely better ways to do it.

E. g., instead of using Sed, one could simply do:

....
then cc=0"$cc"
....

Or, even more concise:

cc=$(($aa + $bb))
[ "$cc" -lt 10 ] && cc=0"$cc"

But, as mentioned elsewhere in this group, it's better to use
`printf' for the formatting tasks like this.

--
FSF associate member #7257
From: Sidney Lambe on
On comp.unix.shell, Sidney Lambe <sidneylambe(a)nospam.invalid>
wrote:

> On comp.unix.shell, Mart Frauenlob <mart.frauenlob(a)chello.at>
> wrote:
>
>> On 01.02.2010 08:12, Hongyi Zhao wrote:
>>
>>> Hi all,
>>>
>>> I want to do the following simple addition operations under
>>> bash by using expr:
>>>
>>> 1- I've two variables, i.e., aa and bb.
>>>
>>> 2- If the addition of aa + bb is little than 10, it should
>>> give me the output with a leading zero like this: 01, 08, 09
>>> or so.
>>>
>>> 3- If the addition of aa + bb is large than 10, it should
>>> give me the output without a leading zero.
>>>
>>> What should I do?
>>>
>>> Thanks in advance.
>>
>>
>> eris:~# a=3 eris:~# printf "%02d\n" "$a" 03 eris:~# a=33
>> eris:~# printf "%02d\n" "$a" 33
>>
>>
>
>
>
> Okay, I think I've got it. But what if a=333?
>
> Sid
>

It still works. Baffling. Back to the man page.

I could have sworn that 2 limited the output to
two columns.

Sid


From: Bit Twister on
On 1 Feb 2010 09:16:38 +0100, Sidney Lambe wrote:

> Okay, I think I've got it. But what if a=333?

For crying out loud, you seemed to have proclaimed yourself mister cli
and run no gui desktop manager, just enter

a=333
printf "%02d\n" "$a"

at the command line prompt and see what happens.


From: Jerry Peters on
Sidney Lambe <sidneylambe(a)nospam.invalid> wrote:
> On comp.unix.shell, Sidney Lambe <sidneylambe(a)nospam.invalid>
> wrote:
>
>> On comp.unix.shell, Mart Frauenlob <mart.frauenlob(a)chello.at>
>> wrote:
>>
>>> On 01.02.2010 08:12, Hongyi Zhao wrote:
>>>
>>>> Hi all,
>>>>
>>>> I want to do the following simple addition operations under
>>>> bash by using expr:
>>>>
>>>> 1- I've two variables, i.e., aa and bb.
>>>>
>>>> 2- If the addition of aa + bb is little than 10, it should
>>>> give me the output with a leading zero like this: 01, 08, 09
>>>> or so.
>>>>
>>>> 3- If the addition of aa + bb is large than 10, it should
>>>> give me the output without a leading zero.
>>>>
>>>> What should I do?
>>>>
>>>> Thanks in advance.
>>>
>>>
>>> eris:~# a=3 eris:~# printf "%02d\n" "$a" 03 eris:~# a=33
>>> eris:~# printf "%02d\n" "$a" 33
>>>
>>>
>>
>>
>>
>> Okay, I think I've got it. But what if a=333?
>>
>> Sid
>>
>
> It still works. Baffling. Back to the man page.
>
> I could have sworn that 2 limited the output to
> two columns.
>
> Sid
>
No, it sets the *minimum* width to 2, the maximum is what is required
to print the field.

Jerry