51

私は持っd1="11"ていd2="07"ます。d1d2を整数に変換して実行したいd1-d2。UNIXでこれを行うにはどうすればよいですか?

d1 - d2現在、結果として返さ"11-07"れます。

4

5 に答える 5

76

標準ソリューション:

 expr $d1 - $d2

次のこともできます。

echo $(( d1 - d2 ))

07ただし、これは8進数として扱われることに注意してください。(したがって07、と同じですが、と7010異なり10ます)。

于 2012-06-29T21:46:26.713 に答える
21

これらはいずれもシェルコマンドラインから機能します。bcおそらくあなたの最も簡単な解決策です。

bcの使用:

$ echo "$d1 - $d2" | bc

使用awk

$ echo $d1 $d2 | awk '{print $1 - $2}'

使用perl

$ perl -E "say $d1 - $d2"

使用Python

$ python -c "print $d1 - $d2"

すべて戻る

4
于 2012-06-29T20:27:58.043 に答える
6

OPの場合に限定されない答え

質問のタイトルがここの人々を導くので、OPの説明されたケースが非常に限られていたので、私は他のすべての人のためにその質問に答えることに決めました。

TL; DR

私はついに関数を書くことに決めました。

  1. 0非intの場合に必要な場合:
int(){ printf '%d' ${1:-} 2>/dev/null || :; }
  1. 非intの場合に[empty_string]が必要な場合:
int(){ expr 0 + ${1:-} 2>/dev/null||:; }
  1. 最初のintまたは[empty_string]を見つけたい場合:
int(){ expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null||:; }
  1. 最初のintまたは0を見つけたい場合:
# This is a combination of numbers 1 and 2
int(){ expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null||:; }

非整数でゼロ以外のステータスコードを取得する場合は、||:(akaまたはtrue)を削除しますが、;

テスト

# Wrapped in parens to call a subprocess and not `set` options in the main bash process
# In other words, you can literally copy-paste this code block into your shell to test
( set -eu;
    tests=( 4 "5" "6foo" "bar7" "foo8.9bar" "baz" " " "" )
    test(){ echo; type int; for test in "${tests[@]}"; do echo "got '$(int $test)' from '$test'"; done; echo "got '$(int)' with no argument"; }

    int(){ printf '%d' ${1:-} 2>/dev/null||:; };
    test

    int(){ expr 0 + ${1:-} 2>/dev/null||:; }
    test

    int(){ expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null||:; }
    test

    int(){ printf '%d' $(expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null)||:; }
    test

    # unexpected inconsistent results from `bc`
    int(){ bc<<<"${1:-}" 2>/dev/null||:; }
    test
)

テスト出力

int is a function
int ()
{
    printf '%d' ${1:-} 2> /dev/null || :
}
got '4' from '4'
got '5' from '5'
got '0' from '6foo'
got '0' from 'bar7'
got '0' from 'foo8.9bar'
got '0' from 'baz'
got '0' from ' '
got '0' from ''
got '0' with no argument

int is a function
int ()
{
    expr 0 + ${1:-} 2> /dev/null || :
}
got '4' from '4'
got '5' from '5'
got '' from '6foo'
got '' from 'bar7'
got '' from 'foo8.9bar'
got '' from 'baz'
got '' from ' '
got '' from ''
got '' with no argument

int is a function
int ()
{
    expr ${1:-} : '[^0-9]*\([0-9]*\)' 2> /dev/null || :
}
got '4' from '4'
got '5' from '5'
got '6' from '6foo'
got '7' from 'bar7'
got '8' from 'foo8.9bar'
got '' from 'baz'
got '' from ' '
got '' from ''
got '' with no argument

int is a function
int ()
{
    printf '%d' $(expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null) || :
}
got '4' from '4'
got '5' from '5'
got '6' from '6foo'
got '7' from 'bar7'
got '8' from 'foo8.9bar'
got '0' from 'baz'
got '0' from ' '
got '0' from ''
got '0' with no argument

int is a function
int ()
{
    bc <<< "${1:-}" 2> /dev/null || :
}
got '4' from '4'
got '5' from '5'
got '' from '6foo'
got '0' from 'bar7'
got '' from 'foo8.9bar'
got '0' from 'baz'
got '' from ' '
got '' from ''
got '' with no argument

ノート

受け入れられた答えset -o nounsetが(別名set -u)と互換性がないので、私はこのウサギの穴に送られました

# This works
$ ( number="3"; string="foo"; echo $((number)) $((string)); )
3 0

# This doesn't
$ ( set -u; number="3"; string="foo"; echo $((number)) $((string)); )
-bash: foo: unbound variable
于 2020-01-17T04:36:22.183 に答える
0
let d=d1-d2;echo $d;

これは役立つはずです。

于 2016-12-14T22:33:03.573 に答える
-8

これを使って:

#include <stdlib.h>
#include <string.h>

int main()
{
    const char *d1 = "11";
    int d1int = atoi(d1);
    printf("d1 = %d\n", d1);
    return 0;
}

于 2012-06-29T20:19:53.007 に答える