ALT Linux Community general discussions
 help / color / mirror / Atom feed
From: Sergey Vlasov <vsu@altlinux.ru>
To: community@lists.altlinux.org
Subject: Re: [Comm] Арифметика в bash
Date: Thu, 17 Nov 2011 19:50:27 +0400
Message-ID: <20111117155027.GA6399@atlas.home> (raw)
In-Reply-To: <E1RQwkX-0003uK-00.vkarpinsky-mail-ru@f126.mail.ru>

[-- Attachment #1: Type: text/plain, Size: 1055 bytes --]

On Thu, Nov 17, 2011 at 11:51:09AM +0400, Vladimir Karpinsky wrote:
> Уже некоторое время пытаюсь найти простое решение следующей
> проблемы: нужно производить простейшие арифметические операции (как
> правило + или -1 ;-)) с "юлианскими" днями года (date +%j).
> Сложность заключается в том, что формат такой даты JJJ, т.е. первые
> 99 дней года первая цифра 0, и bash считает это число восьмеричным,
> со всеми вытекающими последствиями. Хочется заставить bash работать
> с такими числами, как десятеричными, формат JJJ для результата
> вычисления желательно также сохранить. 

Именно в bash поддерживается синтаксис вида BASE#NUMBER с явным
указаним основания:

  $ bash -c 'a=011; echo $((10#$a))'
  11

Нужно учитывать, что в POSIX sh такой синтаксис не поддерживается,
поэтому в скриптах нужно явно указывать #!/bin/bash (использование
#!/bin/sh в ALT пока не проявится в виде ошибок, но в некоторых
дистрибутивах в роли /bin/sh может оказаться, например, dash).

Выводить числа в нужном формате можно через printf '%03d' $n .

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  parent reply	other threads:[~2011-11-17 15:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-17  8:20   ` Константин Михайленко
2011-11-17  9:31     ` Vladimir Karpinsky
2011-11-17  9:35   ` Yura Kalinichenko
2011-11-17  9:50     ` Vladimir Karpinsky
2011-11-17  9:53       ` Vladimir Karpinsky
2011-11-17 15:50   ` Sergey Vlasov [this message]
2011-11-17 16:06     ` Vladimir Karpinsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111117155027.GA6399@atlas.home \
    --to=vsu@altlinux.ru \
    --cc=community@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Community general discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/community/0 community/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 community community/ http://lore.altlinux.org/community \
		mandrake-russian@linuxteam.iplabs.ru community@lists.altlinux.org community@lists.altlinux.ru community@lists.altlinux.com
	public-inbox-index community

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.community


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git