入力を整数ではないものと整数に分類するbash `case`ステートメント

入力を整数ではないものと整数に分類するbash `case`ステートメント

要約:bash case別のコードのステートメントを使用して入力を分類し、入力が次のようになるようにしたいと思います。

  • 正の整数
  • 負の整数
  • 若い
  • 空の文字列
  • 整数ではない文字列

以下は、次の入力を正しく分類する実行コードです。

  • word
  • a\nmultiline\nstring
  • 2.1
  • -3

しかし、次の2つを負の整数に分類してみてください...:-(

  • 0
  • 42

詳細:

以下をファイル(たとえば/tmp/integer_case_statement.sh)に保存してchmod実行します。

#!/usr/bin/env bash

### This should be a simple `bash` `case` statement to classify inputs as
### {positive|negative|zero|non-} integers.
### Trying extglob, since my previous integer-match patterns failed.
### Gotta turn that on before *function definition* per https://stackoverflow.com/a/34913651/915044
shopt -s extglob

declare cmd=''

function identify() {
    local -r it=${1}  # no quotes in case it's multiline
#    shopt -s extglob # can't do that here
    case ${it} in
        '')
            # empty string, no need for `if [[ -z ...`
            >&2 echo 'ERROR: null arg'
            ;;
        ?(-|+)+([[:digit:]]))
            # it's an integer, so just say so and fallthrough
            >&2 echo 'DEBUG: int(it), fallthrough'
            ;&
        -+([[:digit:]]))
            # it's negative: just return it
            >&2 echo 'DEBUG: int(it) && (it < 0), returning it'
            echo "${it}"
            ;;
        0)
            # it's zero: that's OK
            >&2 echo 'DEBUG: int(it) && (it == 0), returning it'
            echo '0'
            ;;
        ++([[:digit:]]))
            # it's positive: just return it
            >&2 echo 'DEBUG: int(it) && (it > 0), returning it'
            echo "${it}"
            ;;
        *)
            # not an integer, just return it
            >&2 echo 'DEBUG: !int(it)'
            echo "${it}"
            ;;
    esac
} # end function identify

echo -e "'bash --version'==${BASH_VERSION}\n"

echo "identify '':"
identify ''
echo
# > ERROR: null arg

echo 'identify word:'
identify word
echo
# > DEBUG: !int(it)
# > word

echo 'identify a
multiline
string:'
identify 'a
multiline
string'
echo
# > DEBUG: !int(it)
# > a
# > multiline
# > string

echo 'identify 2.1:'
identify 2.1
echo
# > DEBUG: !int(it)
# > 2.1

echo 'identify -3:'
identify -3
echo
# > DEBUG: int(it), fallthrough
# > DEBUG: int(it) && (it < 0), returning it
# > -3

echo 'identify 0:'
identify 0
echo
# > DEBUG: int(it), fallthrough
# > DEBUG: int(it) && (it < 0), returning it
# > 0

echo 'identify 42:'
identify 42
echo
# > DEBUG: int(it), fallthrough
# > DEBUG: int(it) && (it < 0), returning it
# > 42

exit 0

現在の出力はファイルにインラインですが、読みやすくするために、現在の出力は次のようになります。

'bash --version'==4.3.30(1)-release

identify '':
ERROR: null arg

identify word:
DEBUG: !int(it)
word

identify a
multiline
string:
DEBUG: !int(it)
a
multiline
string

identify 2.1:
DEBUG: !int(it)
2.1

identify -3:
DEBUG: int(it), fallthrough
DEBUG: int(it) && (it < 0), returning it
-3

identify 0:
DEBUG: int(it), fallthrough
DEBUG: int(it) && (it < 0), returning it
0

identify 42:
DEBUG: int(it), fallthrough
DEBUG: int(it) && (it < 0), returning it
42

最後の2つの入力は私の質問です。ケースの説明が識別されるのはなぜですか?

  • 負の整数である0(0ではない)
  • 42 負の整数(正の整数の代わりに)

?ご協力ありがとうございます。

ベストアンサー1

要約:ありがとう

また、符号付きゼロを検出するための追加セクションといくつかのテストケースを追加しました。

詳細:

この拡張コードをファイル(たとえば/tmp/integer_case_statement.sh)に保存しchmodて実行します。

#!/usr/bin/env bash

### Simple `bash` `case` statement to classify inputs as {positive|negative|zero|non-} integers.
### Trying extglob, since my previous integer-match patterns failed.
### Gotta turn that on before *function definition* per https://stackoverflow.com/a/34913651/915044
shopt -s extglob

declare input=''

### For `case` *patterns* (NOT regexps), see
### https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
function identify() {
    local -r it=${1}  # no quotes in case it's multiline
#    shopt -s extglob # can't do that here
    case ${it} in
        '')
            # empty string, no need for `if [[ -z ...`
            >&2 echo 'ERROR: null arg'
            ;;
        [+-]0)
            >&2 echo 'ERROR: zero should not be signed'
            ;;
        ?(-|+)+([[:digit:]]))
            # it's an integer, so just say so and fallthrough
            >&2 echo 'DEBUG: int(it), fallthrough'
#            ;& # this only runs the next clause, thanks https://unix.stackexchange.com/users/30851/frostschutz
            ;;& # works
        -+([[:digit:]]))
            >&2 echo 'DEBUG: it < 0'
            ;;
        0)
            >&2 echo 'DEBUG: it == 0'
            echo '0'
            ;;
        ?(+)+([[:digit:]])) # thanks https://unix.stackexchange.com/users/332764/freddy
            >&2 echo 'DEBUG: it > 0'
            ;;
        *)
            >&2 echo 'DEBUG: !int(it)'
            ;;
    esac
} # end function identify

echo -e "'bash --version'==${BASH_VERSION}\n"

for input in \
    '' \
    '@#$%^&!' \
    'word' \
    'a
multiline
string' \
    '2.1' \
    '-3' \
    '+3' \
    '+0' \
    '0' \
    '-0' \
    '42' \
; do
    echo "identify '${input}'"
    identify "${input}"
    ret_val="${?}"
    if [[ "${ret_val}" -ne 0 ]] ; then
        >&2 echo "ERROR: retval='${ret_val}', exiting ..."
        exit 3
    fi
    echo # newline
done

exit 0

このDebianワークステーションでは、上記の内容は現在次のように出力されます。

'bash --version'==4.3.30(1)-release

identify ''
ERROR: null arg

identify '@#$%^&!'
DEBUG: !int(it)

identify 'word'
DEBUG: !int(it)

identify 'a
multiline
string'
DEBUG: !int(it)

identify '2.1'
DEBUG: !int(it)

identify '-3'
DEBUG: int(it), fallthrough
DEBUG: it < 0

identify '+3'
DEBUG: int(it), fallthrough
DEBUG: it > 0

identify '+0'
ERROR: zero should not be signed

identify '0'
DEBUG: int(it), fallthrough
DEBUG: it == 0
0

identify '-0'
ERROR: zero should not be signed

identify '42'
DEBUG: int(it), fallthrough
DEBUG: it > 0

ご協力ありがとうございます!

おすすめ記事