다음과 같은 MySQL 문의 실행 결과를 기반으로 변수를 설정하려고합니다.
errorOut=0
mysql -uroot -pinsite3 -rN --execute "select file_system_id from ifm.PartitionTable where physical_partition='$partName'" | while read myVal
do
echo "myVal = $myVal"
if [ $myVal -eq $fsId ];then
errorOut=1
echo "Found equal: errorOut = $errorOut"
fi
done
echo "Outside loop: errOut = $errorOut"
Here is the output:
myVal = 1
myVal = 2
Found equal: errorOut = 1
Outside loop: errOut = 0
보시다시피 파이프로 인해 루프 외부의 변수 값을 가져올 수 없습니다 (파이프 내부의 변수는 기본적으로 다른 프로세스를 분기하도록 설정되어 있습니다)
루프 외부에서 실제 값을 추출 할 수있는 방법이 있습니까?
이와 같이 for...in
대신를 사용 read
하면 어떨까요? :
errorOut=0
for myVal in $(mysql -uroot -pinsite3 -rN --execute "select file_system_id from ifm.PartitionTable where physical_partition='$partName'")
do
echo "myVal = $myVal"
if [ $myVal -eq $fsId ];then
errorOut=1
echo "Found equal: errorOut = $errorOut"
fi
done
echo "Outside loop: errOut = $errorOut"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다