파워쉘의 논리 연산자

  1. PowerShell 논리 연산자
  2. PowerShell에서 -and 연산자
  3. PowerShell에서 -or 연산자
  4. PowerShell에서 -xor 연산자
  5. PowerShell에서 -not 연산자
파워쉘의 논리 연산자

논리 연산자는 다양한 조건을 하나의 조건으로 변환할 수 있습니다.

이 기사에서는 실제 사례를 논의하고 PowerShell 스크립트에서 논리 연산자를 적용합니다.

PowerShell 논리 연산자

논리 연산자는 and, or, xornot 또는 !입니다.

PowerShell에서 -and 연산자

출력은 $a$btrue일 경우 true이고, 그렇지 않으면 false입니다.

진리표:

A B 출력
0 0 0
1 0 0
0 1 0
1 1 1
$a = 0
$b = 0
$a -and $b # false (if both variables are false)

$a = 1
$b = 0
$a -and $b  # false (if any of the variables are false)

$a = 1
$b = 1
$a -and $b # true (if both variables are true)

-and 연산자는 두 가지가 모두 true일 때만 true를 반환합니다. 일반적으로 -and 연산자는 모든 조건이 확인되고 충족되기를 원하는 경우에 사용됩니다.

충족되어야 하는 두 조건의 예는 다음과 같습니다.

$attendance = 102
$paid = "Y"
if ($attendance -gt 100 -and $paid -eq "Y") {
    Write-Output "Allow for examination."
}

출력:

Allow for examination.

PowerShell에서 -or 연산자

출력은 $a$bfalse일 경우 false입니다. 이는 -and 연산자와 비교됩니다.

-or 연산자는 true를 출력하기 위해 하나의 변수가 true일 필요만 있습니다.

진리표:

A B 출력
0 0 0
1 0 1
0 1 1
1 1 1
$a = 0
$b = 0
$a -or $b # false (if both conditions are false)

$a = 1
$b = 0
$a -or $b # true (if any of the variables are true)

$a = 1
$b = 1
$a -or $b  # true (if both of the variables are true)

-or 연산자는 두 조건이 모두 false일 때만 false를 반환합니다. 일반적으로 -or 연산자는 어떤 조건이든 true로 간주할 때 사용됩니다.

$attendance = 99
$marks = 201
if ($attendance -gt 100 -or $marks -gt 200) {
    Write-Output "Give five extra marks."
}

출력:

Give five extra marks.

PowerShell에서 -xor 연산자

배타적 or 또는 -xor$a 또는 $b 중 하나만 true일 때 true가 됩니다. 두 조건이 모두 true일 경우 -xorfalse의 결과를 제공합니다.

진리표:

A B 출력
0 0 0
1 0 1
0 1 1
1 1 0
('a' -eq 'A') -xor ('a' -eq 'z') # true as one of them is true
('a' -eq 'A') -xor ('Z' -eq 'z') # false as both of them is true
('a' -eq 's') -xor ('Z' -eq 'p') # false as both of them are false

PowerShell에서 -not 연산자

-not 연산자는 표현식 출력의 반대를 반환합니다. 표현식의 출력이 true인 경우, 연산자는 이를 false로 반환하고 그 반대도 마찬가지입니다.

-not ('a' -eq 'a') # false as the output of expression is true
-not ('v' -eq 'a') # true as output expression is false
-not ('v' -eq 'V') # false as output expression is true
-not ('V' -eq 'V1') # true as output expression is false

느낌표 !-not 연산자와 동일합니다.

!('a' -eq 'a')  # false as the output of expression is true
!('v' -eq 'a') # true as output expression is false
!('v' -eq 'V') # false as output expression is true
!('V' -eq 'V1') # true as output expression is false
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn