![sed를 사용하여 빈 파일을 건너뛰는 방법은 무엇입니까?](https://linux55.com/image/62330/sed%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EB%B9%88%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EA%B1%B4%EB%84%88%EB%9B%B0%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
나는 sed
이것을 다음과 같이 사용하고 있습니다 :
sed -e 's/ *| */|/g'
${array_export_files[$loopcount]}>>$TEMPDIR/"export_file"_${testid}_${loopcount}_$$
while 루프에서는 파일이 비어 있거나 내용이 없으면 문제가 발생합니다.
sed
파일이 존재하지만 비어 있으면 실행하고 싶지 않습니다.sed
파일이 존재하지 않으면 실행하고 싶지 않습니다.
전체 코드 조각은 다음과 같습니다.
while [ $loopcount -le $loopmax ]
do
if [ "$loopcount" -eq "$loopcount" ]
then
sed -e 's/ *| */|/g' ${array_export_files[$loopcount]}>>$TEMPDIR/"export_file"_${testid}_${loopcount}_$$
tr "|" "\t" <"export_file"_${testid}_${loopcount}_$$>${array_export_files[$loopcount]}
cp ${array_export_files[$loopcount]} "export_file"_${loopcount}_${testid}
echo "Testing Starts Here"
echo ${array_export_files[$loopcount]} "export_file"_${loopcount}_${testid}
echo "Testing Ends Here"
fi
(( loopcount=`expr $loopcount+1`))
done
따라서 위의 if 문에서 AND 연산자를 바꾸거나 사용할 수 없습니다. 이 문제를 해결할 수 있는 방법이 있습니까? AND 연산자를 사용하면 아래의 전체 코드 부분을 건너뛸 수 있으며 실행되지 않습니다. 조건부로 sed 부분을 건너뛰고 싶습니다.
답변1
-s
Bash에는 존재 여부를 테스트할 수 있는 옵션이 있습니다.그리고크기가 0보다 큽니다:
-s file
True if file exists and has a size greater than zero.
그래서 당신은 할 수 있습니다
if [ -s "${array_export_files[$loopcount]}" ]; then
sed .......
fi
루프 내에서. 이는 항상 사실 이므로 if [ "$loopcount" -eq "$loopcount" ]
다음과 같이 바꿀 수 있습니다.
while [ "$loopcount" -le "$loopmax" ]
do
if [ -s "${array_export_files[$loopcount]}" ]
then
sed -e 's/ *| */|/g' "${array_export_files[$loopcount]}" >>" $TEMPDIR/export_file_${testid}_${loopcount}_$$"
tr "|" "\t" <"export_file_${testid}_${loopcount}_$$">"${array_export_files[$loopcount]}"
cp "${array_export_files[$loopcount]}" "export_file_${loopcount}_${testid}"
echo "Testing Starts Here"
echo "${array_export_files[$loopcount]}" "export_file_${loopcount}_${testid}"
echo "Testing Ends Here"
fi
(( loopcount = loopcount + 1 ))
done