我有这样的shell脚本:
cat file | while read line
do
# run some commands using $line
done
Run Code Online (Sandbox Code Playgroud)
现在我需要检查该行是否包含任何非空格字符([\n\t\t]),如果不包含,则跳过它.我怎样才能做到这一点?
Ark*_*kku 62
由于read
默认情况下读取空格分隔的字段,因此仅包含空格的行应该导致将空字符串分配给变量,因此您应该能够使用以下内容跳过空行:
[ -z "$line" ] && continue
Run Code Online (Sandbox Code Playgroud)
小智 10
试试这个
while read line;
do
if [ "$line" != "" ]; then
# Do something here
fi
done < $SOURCE_FILE
Run Code Online (Sandbox Code Playgroud)
庆典:
if [[ ! $line =~ [^[:space:]] ]] ; then
continue
fi
Run Code Online (Sandbox Code Playgroud)
并使用done < file
而不是cat file | while
,除非你知道为什么你使用后者.