Date: Tue, 2 Feb 2016 20:53:24 -0700 From: Sergey Manucharian <sm@ara-ler.com> To: Ernie Luzar <luzar722@gmail.com> Cc: Freebsd Questions <FreeBSD-questions@freebsd.org> Subject: Re: sh script help with numeric values Message-ID: <20160203035324.GH1828@dendrobates.araler.com> In-Reply-To: <56B1765D.2060800@gmail.com> References: <56B1765D.2060800@gmail.com>
index | next in thread | previous in thread | raw e-mail
Hello Ernie,
Excerpts from Ernie Luzar's message from Wed 03-Feb-16 11:39:
> Hello list
>
> I am having trouble getting leading zeros to show up.
> The following is an example of what i mean.
>
> dirname="dir"
> dup_count=000
> dup_times=5
>
> while [ "${dup_count}" -ne "${dup_times}" ]; do
> dup_count=$(( $dup_count + 001 ))
> dname="${dirname}-${dup_count}"
> echo "${dname}"
> done
>
> Outputs this
> dir-1
> dir-2
> dir-3
> dir-4
> dir-5
>
> What I really want to see is this
> dir-001
> dir-002
> dir-003
> dir-004
> dir-005
> dir-013
> dir-111
> ect
>
> How do I get the leading zeros to show?
This is an arithmetic expression:
dup_count=$(( $dup_count + 001 ))
which gives calculate a number, you don't need to use leading zeros.
Use "printf" instead of "echo" to output the result.
Example:
$ A=3
$ printf "%03d\n" $A
$ 003
--
Sergey
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20160203035324.GH1828>
