Bashケースロジックで改行と空白以外の文字を検出する方法は?

Bashケースロジックで改行と空白以外の文字を検出する方法は?

私は$ messageに空白以外の文字があるかどうかを検出するためにbashスクリプトを作成し、大文字と小文字のロジックを使用していますが、改行以外の文字も検出したいと思います。時々私のスクリプトの$ message変数は改行と同じで、時にはtrueを返します。

空白以外の条件と改行以外の条件を組み合わせる方法は?

このように?[!\ !\N]

これが私が今まで持っているものです:

  7 case $message in
  8     *[!\ ]*) # contains non-space
  9         messagex='``` '"$message"' ```' ;;
 10     *)       # contains nothing or only spaces
 11         messagex=
 12 esac

ベストアンサー1

case $message in
  (*[!' 
 ']*) : contains characters other space and newline
esac

または:

case $message in
  (*[!"$(printf '\n ')"]*) : contains characters other space and newline
esac

または、ksh93スタイルの$'...'特殊引用符を使用してください(にはありませんsh)。

case $message in
  (*[!$' \n']*) : contains characters other space and newline
esac

[:space:]あるいは、POSIX 文字クラスを使用して、TAB、SPC、CR、NL などのすべてのスペースを一致させることもできます。

case $message in
  (*[![:space:]]*) : contains non-whitespace characters
esac

おすすめ記事