From: Hongyi Zhao on 1 Feb 2010 02:12 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. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Sidney Lambe on 1 Feb 2010 02:36 On comp.unix.shell, 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. Bash: (( $aa + $bb = cc }} if [ "$cc" -lt 10 ] then cc=$(echo "$cc" | sed 's/.*/0&/') fi There are surely better ways to do it. Sid
From: Mart Frauenlob on 1 Feb 2010 02:43 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
From: Sidney Lambe on 1 Feb 2010 03:11 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 > > > Very slick. I'm reading man printf and sprintf in an attempt to understand what you've done there, but it isn't coming easy. Are you a C programmer by any chance? Sid
From: Sidney Lambe on 1 Feb 2010 03:16 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
|
Next
|
Last
Pages: 1 2 3 Prev: xen, bash, and random seed failure Next: Delete Chinese characters from the filenames. |