![Fish의 if 문에서 여러 조건을 그룹화하는 방법](https://linux55.com/image/128498/Fish%EC%9D%98%20if%20%EB%AC%B8%EC%97%90%EC%84%9C%20%EC%97%AC%EB%9F%AC%20%EC%A1%B0%EA%B1%B4%EC%9D%84%20%EA%B7%B8%EB%A3%B9%ED%99%94%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
실제로 다음 코드는 괄호를 이런 방식으로 사용할 수 없기 때문에 유효하지 않습니다. 이를 제거하면 정상적으로 실행되고 다음과 같이 출력됩니다.
true
true
암호:
#!/usr/bin/fish
if ( false ; and true ) ; or true
echo "true"
else
echo "false"
end
if false ; and ( true ; or true )
echo "true"
else
echo "false"
end
괄호 안에 표시된 기능을 어떻게 얻을 수 있나요?
원하는 출력:
true
false
답변1
당신은 그것을 사용할 수 있습니다begin
그리고end
조건문도 마찬가지입니다.
~에서물고기 튜토리얼:
더 복잡한 조건의 경우 start 및 end를 사용하여 해당 부분을 그룹화하세요.
더 간단한 예를 보려면 다음을 참조하세요.이 답변스택 오버플로에서.
코드의 경우 및를 (
으로 바꾸면 됩니다 .begin ;
)
; end
#!/usr/bin/fish
if begin ; false ; and true ; end ; or true
echo "true"
else
echo "false"
end
if false; and begin ; true ; or true ; end
echo "true"
else
echo "false"
end
답변2
대체 솔루션:조건부 체인의 일부를 함수로 아웃소싱
이와 같이:
#!/usr/bin/fish
function _my_and_checker
return $argv[1]; and argv[2]
end
function _my_or_checker
return $argv[1]; or argv[2]
end
if _my_and_checker false true ; or true
echo "true"
else
echo "false"
end
if false; and _my_or_checker true true
echo "true"
else
echo "false"
end
이는 조건 자체가 복잡한 명령인 경우 가장 적합합니다.