如何在UNIX中将字符串转换为整数

qwa*_*ine 25 unix linux shell interactive

我有d1="11"d2="07".我想转换d1d2整数和执行d1-d2.我如何在UNIX中执行此操作?

d1 - d2目前"11-07"为我返回结果.

Wil*_*ell 44

标准解决方案:

 expr $d1 - $d2
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

echo $(( d1 - d2 ))
Run Code Online (Sandbox Code Playgroud)

但要注意这将被07视为八进制数!(所以07是相同的7,但010不同于10).

  • 您可以进行基础转换:`echo $((d1-10#$d2))` 注意:它与 `echo $((d1-10#d2))` 不同 (4认同)

Lev*_*von 16

其中任何一个都可以在shell命令行中运行.bc可能是你最直接的解决方案.

使用bc:

$ echo "$d1 - $d2" | bc
Run Code Online (Sandbox Code Playgroud)

使用awk:

$ echo $d1 $d2 | awk '{print $1 - $2}'
Run Code Online (Sandbox Code Playgroud)

使用perl:

$ perl -E "say $d1 - $d2"
Run Code Online (Sandbox Code Playgroud)

使用Python:

$ python -c "print $d1 - $d2"
Run Code Online (Sandbox Code Playgroud)

一切都归来

4
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用纯 Bash 立即完成此操作:`echo $((d1-10#$d2))` (4认同)

Bru*_*sky 10

答案不仅限于OP的情况

问题的标题将人们引向这里,所以我决定为其他人回答这个问题,因为OP描述的案例非常有限。

长话短说

我最终决定写一个函数。

  1. 如果你想要0非 int 的情况:
int(){ printf '%d' ${1:-} 2>/dev/null || :; }
Run Code Online (Sandbox Code Playgroud)
  1. 如果您希望在非 int 的情况下使用[empty_string] :
int(){ expr 0 + ${1:-} 2>/dev/null||:; }
Run Code Online (Sandbox Code Playgroud)
  1. 如果你想找到第一个 int 或[empty_string]
int(){ expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null||:; }
Run Code Online (Sandbox Code Playgroud)
  1. 如果你想找到第一个 int 或 0:
# This is a combination of numbers 1 and 2
int(){ expr ${1:-} : '[^0-9]*\([0-9]*\)' 2>/dev/null||:; }
Run Code Online (Sandbox Code Playgroud)

如果您想在非 int 上获得非零状态代码,请删除||:(又名 或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
)
Run Code Online (Sandbox Code Playgroud)

测试输出

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
Run Code Online (Sandbox Code Playgroud)

笔记

我被送入这个兔子洞,因为接受的答案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
Run Code Online (Sandbox Code Playgroud)