![ipset 명령의 출력은 변수에 저장되지 않습니다.](https://linux55.com/image/11330/ipset%20%EB%AA%85%EB%A0%B9%EC%9D%98%20%EC%B6%9C%EB%A0%A5%EC%9D%80%20%EB%B3%80%EC%88%98%EC%97%90%20%EC%A0%80%EC%9E%A5%EB%90%98%EC%A7%80%20%EC%95%8A%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
IPSET의 존재를 감지하는 스크립트를 작성 중입니다.
#!/bin/bash
str=$(/usr/sbin/ipset test IPsetName 1.1.1.1)
echo "$str" #outputs blank line
if [[ $str = *"The set with the given name does not exist"* ]]; then
echo "IPsetName not found"
fi
이 스크립트를 실행하면 다음과 같은 결과가 나타납니다.
ipset v6.29: The set with the given name does not exist
그런 다음 빈 줄이 있고 echo "$str"
if 문의 예상 출력이 표시되지 않습니다.
ipset 명령 출력을 변수에 저장하는 방법은 무엇입니까?
답변1
감사합니다 @StephenHarris
ipset 명령의 출력은 stdout이 아닌 stderr에서 생성되며 2>&1
출력은 변수에 캡처됩니다.
str=$(/usr/sbin/ipset test IPsetName 1.1.1.1 2>&1)
if [[ $str = *"The set with the given name does not exist"* ]]; then
echo "IPsetName not found"
fi
이제 if 문이 예상대로 작동합니다!