Usage: expr EXPRESSION
or: expr OPTION
--help この使い方を表示して終了
--version バージョン情報を表示して終了
式の値を標準出力に表示. 以下の一覧では, 式を評価する際の優先順位ごとに
空行で区分けしています. 式として使えるのは:
ARG1 | ARG2 ARG1 がヌルでも 0 でもなければ ARG1 を返し,
それ以外の場合は ARG2 を返す
ARG1 & ARG2 ARG1, ARG2 ともにヌルでも 0 でもなければ ARG1 を返し,
それ以外の場合は 0 を返す
ARG1 < ARG2 ARG1 が ARG2 より小さい
ARG1 <= ARG2 ARG1 が ARG2 より小さいか等しい
ARG1 = ARG2 ARG1 が ARG2 と等しい
ARG1 != ARG2 ARG1 が ARG2 と等しくない
ARG1 >= ARG2 ARG1 が ARG2 より大きいか等しい
ARG1 > ARG2 ARG1 が ARG2 より大きい
ARG1 + ARG2 ARG1 と ARG2 の足し算
ARG1 - ARG2 ARG1 と ARG2 の引き算
ARG1 * ARG2 ARG1 と ARG2 の掛け算
ARG1 / ARG2 ARG1 の ARG2 による割り算
ARG1 % ARG2 ARG1 の ARG2 による割り算のあまり
STRING : REGEXP STRING における正規表現 REGEXP によるパターン照合
match STRING REGEXP STRING : REGEXP と同じ
substr STRING POS LENGTH STRING の部分文字列を返す, POS は 1から始まる
index STRING CHARS STRING から CHARS が見つかった場所を返す.
見つからなければ 0
length STRING STRING の長さ
+ TOKEN TOKEN が `match' のようなキーワードや `/' の
ような演算子であって文字列として解釈.
( 式 ) 式の値
多くの演算子はシェルに渡すためにエスケープするか引用符で囲む必要があります.
比較は ARG がいずれも数値であれば大きさにより, それ以外の場合には辞書順に
より行われます. パターン照合は, \( と \) の間, もしくはヌル文字に一致した
文字列を返します. \( と \) を使わない場合は一致する文字数か 0 を返します.
Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null
or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.
Report expr bugs to bug-coreutils@gnu.org
GNU coreutils home page:
Usage: expr EXPRESSION
or: expr OPTION
--help display this help and exit
--version output version information and exit
Print the value of EXPRESSION to standard output. A blank line below
separates increasing precedence groups. EXPRESSION may be:
ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2
ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0
ARG1 < ARG2 ARG1 is less than ARG2
ARG1 <= ARG2 ARG1 is less than or equal to ARG2
ARG1 = ARG2 ARG1 is equal to ARG2
ARG1 != ARG2 ARG1 is unequal to ARG2
ARG1 >= ARG2 ARG1 is greater than or equal to ARG2
ARG1 > ARG2 ARG1 is greater than ARG2
ARG1 + ARG2 arithmetic sum of ARG1 and ARG2
ARG1 - ARG2 arithmetic difference of ARG1 and ARG2
ARG1 * ARG2 arithmetic product of ARG1 and ARG2
ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2
ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2
STRING : REGEXP anchored pattern match of REGEXP in STRING
match STRING REGEXP same as STRING : REGEXP
substr STRING POS LENGTH substring of STRING, POS counted from 1
index STRING CHARS index in STRING where any CHARS is found, or 0
length STRING length of STRING
+ TOKEN interpret TOKEN as a string, even if it is a
keyword like `match' or an operator like `/'
( EXPRESSION ) value of EXPRESSION
Beware that many operators need to be escaped or quoted for shells.
Comparisons are arithmetic if both ARGs are numbers, else lexicographical.
Pattern matches return the string matched between \( and \) or null; if
\( and \) are not used, they return the number of characters matched or 0.
Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null
or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.
Report expr bugs to bug-coreutils@gnu.org
GNU coreutils home page:
expr(1) expr(1)
名前
expr - 式を評価する
書式
expr expression...
expr [--help] [--version]
説明
expr は式を評価し、その結果を標準出力に書き出す。式のトークン (token)
はそれぞれ別々の引数で与える必要がある。
オペランド (operand) は数字または文字列である。 expr はオペランドの位置
に 現れたものを、それに対して行われる演算に応じて整数または文字列に変換
する。文字列は expr ではクォートしなくてよいが、シェルから保護するた め
の引用符が必要かもしれない (スペースなど)。
演算子は 2 項間に挿入されるシンボルとして、または前置されるキーワードと
して与えられる。括弧も通常のようにグループ化に利用できる (しかしシェ ル
による解釈を避けるためには括弧をクォートしなければならない)。
expr は以下の終了ステータスを返す:
0: 式がナルでも 0 でもない
1: 式がナルまたは 0
2: 式が正しくない
演算子 (優先度順)
関係演算
| 左項がナルでも 0 でもなければ左項を返す。ナルや 0 だった場合は右
項を返す。これは通常の ‘or’ 演算にあたる。
& 左項、右項ともナルでも 0 でもなければ左項を返す。それ以外の場 合
は 0 となる。
< <= = == != >= >
両項を比較し、関係が真なら 1、偽なら 0 を返す (== は =と同義)。
expr はまず両方の項を数値に変換し、数値的な比較を行おうとする 。
どちらかの引数が数値に変換できなかった場合は辞書的な比較を行う。
数値演算
+ - 代数的な加減算を行う。左右の項は数値に変換され、失敗した場合はエ
ラーとなる。
* / %
代 数的な乗算・除算・剰余演算を行う (‘%’ は C の場合と同じく剰余
演算に対応する)。左右の項は数値に変換され、これに失敗した場合 は
エラーとなる。
文字列演算
string : regex
パ タ ー ン マッチを実行する。左右の項は文字列に変換され、右項は
(grep(1) の使う基本的な) 正規表現として扱われる。右項には暗黙 の
う ちに先頭に ‘^’ が自動的に付加され、左項がその正規表現にマッチ
するかのテストが行われる。
マッチが成功した場合、正規表現の一部が ‘\(’ と ’\)’ で囲われてい
れば、 : 演算子は string のうち、副表現 (subexpression) にマッチ
した部分を返す。それ以外の場合はマッチした部分の文字数が返される
。
マッチに失敗した場合、 ‘\(’ と ‘\)’ が使われていた場合にはナル文
字列が、そうでない場合には 0 が返される。
‘\(’ と ‘\)’ のペアのうち、最初のものだけが返り値に影響する。 そ
れ以外のペアは、正規表現の演算子をグループ化するという意味だけを
持つ。
他の expr と異なり、‘+’ (1 つ以上)、‘?’ (0 または 1)、‘|’ (or 接
続) もマッチング演算子として認識される。
さらに、以下のキーワードも認識される:
index string charset
charset にあるどれかの文字が string に見付かれば、その最初に見付
かった位置を返す。どの文字も見付からなければ 0 を返す。
length string
string の長さを返す。
match string regex
パターンマッチを行う別のやり方。これは ‘‘string : regex’’ と同じ
。
quote string
string を (演算子またはキーワードが含まれていても) 通常の文字列
とみなす。環境変数 POSIXLY_CORRECT が設定されていると無効にな る
。
substr string position length
string の部分文字列を返す。部分文字列は string の position から
始まり、最大 length の長さを持つ。 position や length が正でなか
ったり数値でなかった場合は、ナル文字列を返す。
括 弧 は 通常のグループ化の意味に用いることができる。キーワード (match,
substr, index, length) は文字列として用いることはできない。
オプション
GNU expr が引数 1 つだけで起動された場合には、以下のオプションが認識 さ
れる:
--help 標準出力に使用方法のメッセージを出力して正常終了する。
--version
標準出力にバージョン情報を出力して正常終了する。
例
シェル変数 foo に 1 を加える:
foo=`expr $a + 1`
変数 bar に格納されているファイル名のうち、ディレクトリ以外の部分を表示
する (bar の値には ‘/’ が含まれていなくても良い):
expr $bar : ´.*/\(.*\)´ ´|´ $a
文字列 ‘abc’ に文字 ‘b’ をマッチさせる:
expr abc : ’a\(.\)c’
文字列 ‘abcdef’ に ‘c’ または ‘z’ のいずれかが存在している位置を探す:
expr index abcdef cz
キーワード (index) を文字列として用いている誤った例:
expr index index a
キーワード (index) を文字列として用いている正しい例:
expr index quote index d
注意
プログラムのバグについては bug-sh-utils@gnu.org に報告してください。 ペ
ージの更新は Ragnar Hojland Espinosa
EXPR(1) User Commands EXPR(1)
NAME
expr - evaluate expressions
SYNOPSIS
expr EXPRESSION
expr OPTION
DESCRIPTION
--help display this help and exit
--version
output version information and exit
Print the value of EXPRESSION to standard output. A blank line below
separates increasing precedence groups. EXPRESSION may be:
ARG1 | ARG2
ARG1 if it is neither null nor 0, otherwise ARG2
ARG1 & ARG2
ARG1 if neither argument is null or 0, otherwise 0
ARG1 < ARG2
ARG1 is less than ARG2
ARG1 <= ARG2
ARG1 is less than or equal to ARG2
ARG1 = ARG2
ARG1 is equal to ARG2
ARG1 != ARG2
ARG1 is unequal to ARG2
ARG1 >= ARG2
ARG1 is greater than or equal to ARG2
ARG1 > ARG2
ARG1 is greater than ARG2
ARG1 + ARG2
arithmetic sum of ARG1 and ARG2
ARG1 - ARG2
arithmetic difference of ARG1 and ARG2
ARG1 * ARG2
arithmetic product of ARG1 and ARG2
ARG1 / ARG2
arithmetic quotient of ARG1 divided by ARG2
ARG1 % ARG2
arithmetic remainder of ARG1 divided by ARG2
STRING : REGEXP
anchored pattern match of REGEXP in STRING
match STRING REGEXP
same as STRING : REGEXP
substr STRING POS LENGTH
substring of STRING, POS counted from 1
index STRING CHARS
index in STRING where any CHARS is found, or 0
length STRING
length of STRING
+ TOKEN
interpret TOKEN as a string, even if it is a
keyword like ‘match’ or an operator like ‘/’
( EXPRESSION )
value of EXPRESSION
Beware that many operators need to be escaped or quoted for shells.
Comparisons are arithmetic if both ARGs are numbers, else lexicographi-
cal. Pattern matches return the string matched between \( and \) or
null; if \( and \) are not used, they return the number of characters
matched or 0.
Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION
is null or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an
error occurred.
AUTHOR
Written by Mike Parker, James Youngman, and Paul Eggert.
REPORTING BUGS
Report expr bugs to bug-coreutils@gnu.org
GNU coreutils home page:
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa