Raspberry Pi_Kor_10.5.1 “expr” 명령 – 산술연산


Published Book on Amazon


All of IOT

Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1

All of IOT

Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사

물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)

최신 라즈베리파이(Raspberry Pi)로 시작하는 사

물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


10.5   연산 처리 관련 명령

 

10.5.1  "expr" 명령 - 산술연산

 

"expr" 명령을 이용해서 산술연산을 수행할 수 있다.

 

[명령 형식]

expr [OPTION] / [EXPRESSION]

 

[명령 개요]

    지정된 연산식에 대해서 산술연산을 한다.

    user 권한    -- 일반 user.

 

[상세 설명]

    이 명령은 기본적으로 operand operator에는 공백이 필요함

    이 명령으로 계산한 값을 다른 곳에 저장하고자 할 때는 ` `(backtab)을 이용한다.

    주의할 점은 "*"을 사용할 때는 escape()을 함께 사용해야 한다.

 

[주요 Option]

--help

display this help and exit

--version

output version information and exit

 

[주요 expression]

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

if ARG1 is less than ARG2, then 1, otherwise 0

ARG1 <= ARG2

if ARG1 is less than or equal to ARG2, then 1, otherwise 0

ARG1 = ARG2

if ARG1 is equal to ARG2, then 1, otherwise 0

ARG1 != ARG2

if ARG1 is unequal to ARG2, then 1, otherwise 0

ARG1 >= ARG2

if ARG1 is greater than or equal to ARG2, then 1, otherwise 0

ARG1 > ARG2

if ARG1 is greater than ARG2, then 1, otherwise 0

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

             

 

[사용 Example]

다음은 이 명령을 사용한 몇가지의 사례이다.

 

pi@raspberrypi ~ $ echo `expr 5 + 4'

9

pi@raspberrypi ~ $ echo `expr 11 6'

5

pi@raspberrypi3:~ $ echo `expr 10 "|" 75`

10

pi@raspberrypi3:~ $ echo `expr 0 "|" 75`

75

pi@raspberrypi3:~ $ echo `expr 10 ">" 75`

0

pi@raspberrypi3:~ $ echo `expr 10 ">" 7`

1

pi@raspberrypi3:~ $ echo `expr 10 "<" 7`

0

pi@raspberrypi3:~ $ echo `expr 10 "<" 75`

1