如何解决在POSIX shell/ bin / sh中处理变量作用域在while循环中使用适当的管道?
我偶然发现了这个问题:
管道的右侧在子外壳内部运行。子外壳内部设置的任何变量都不会在父外壳中设置。要解决此问题,请使用重定向而不是管道:
因此,这是一个示例脚本:
NUMCOUNT=0
testlines="Lorem ipsum dolor sit amet,consectetur adipiscing elit.
Duis sem sapien,congue sed lacus nec,scelerisque bibendum arcu.
Pellentesque placerat dapibus erat eu iaculis.
Maecenas vitae blandit quam.
Suspendisse eu aliquet nunc."
## this does not work - because NUMCOUNT is not preserved in a subshell - so the final line echoes $NUMCOUNT as 0,which it should not be
#echo "$testlines" | while read line; do
# if echo "$line" | grep -m 1 -q 'que'; then
# NUMCOUNT=$((NUMCOUNT+1))
# echo "found match nr. $NUMCOUNT for substring 'que' in line: $line"
# fi
#done
##this works in bash - but for dash (/bin/sh): "Syntax error: redirection unexpected"
#while read line; do
# if echo "$line" | grep -m 1 -q 'que'; then
# NUMCOUNT=$((NUMCOUNT+1))
# echo "found match nr. $NUMCOUNT for substring 'que' in line: $line"
# fi
#done <<< "$testlines"
# this works in bash - but for dash (/bin/sh): "Syntax error: redirection unexpected"
while read line; do
if echo "$line" | grep -m 1 -q 'que'; then
NUMCOUNT=$((NUMCOUNT+1))
echo "found match nr. $NUMCOUNT for substring 'que' in line: $line"
fi
done < <(echo "$testlines")
echo "Script done - found $NUMCOUNT matches"
如果我在bash
中运行,一切正常:
$ bash test.sh
found match nr. 1 for substring 'que' in line: Duis sem sapien,scelerisque bibendum arcu.
found match nr. 2 for substring 'que' in line: Pellentesque placerat dapibus erat eu iaculis.
found match nr. 3 for substring 'que' in line: Suspendisse eu aliquet nunc.
Script done - found 3 matches
如果我在dash
中运行它,它将失败:
$ /bin/sh test.sh
test.sh: 31: test.sh: Syntax error: redirection unexpected
如何在POSIX /bin/sh
shell中正确实现此循环,以便在脚本的最后一行保留$ NUMCOUNT?
解决方法
如何在POSIX / bin / sh shell中正确实现此循环,以便在脚本的最后一行保留$ NUMCOUNT?
在posix中,将fifo与用于传输数据的后台进程一起使用。但实际上,就您而言,只需将数据保存到文件中即可。
tmp=$(mktemp)
printf "%s\n" "$testlines" > "$tmp"
while IFS= read -r line; do
if printf "%s\n" "$line" | grep -m 1 -q 'que'; then
...
fi
done < "$tmp"
rm "$tmp"
或在后台进程中使用fifo: f
ifo=$(mktemp -u)
mkfifo "$fifo"
printf "%s\n" "$testlines" > "$fifo" &
while IFS= read -r line; do
if echo "$line" | grep -m 1 -q 'que'; then
NUMCOUNT=$((NUMCOUNT+1))
echo "found match nr. $NUMCOUNT for substring 'que' in line: $line"
fi
done < "$fifo"
rm "$fifo"
echo "Script done - found $NUMCOUNT matches"
您也可以将其保存在变量中,进行预处理以提取NUMCOUNT
并打印其余内容,而不是在当前shell中运行循环。
tmp=$(
printf "%s\n" "$testlines" |
awk -v q="'" '
/que/{
++NUMCOUNT
print "found match nr. " NUMCOUNT " for substring "q"que"q" in line: "$0
}
END{
# extra last line to output NUMCOUNT
print NUMCOUNT
}'
)
# extract NUMCOUNT - last line
NUMCOUNT=$(printf "%s\n" "$tmp" | tail -n 1)
# extract everything except NUMCOUNT - ie. remove last line
tmp=$(printf "%s\n" "$tmp" | head -n +1)
# output the actual output
printf "%s\n" "$tmp"
# output your trailing line
printf "Script done - found %d matches\n" "$NUMCOUNT"
或者您可以继续在子Shell中执行。
printf "%s\n" "$testlines" | {
while IFS= read -r line; do
...
done
echo "Script done - found $NUMCOUNT matches"
# go on
}
注意:bashfaq How can I read a file (data stream,variable) line-by-line (and/or field-by-field)和unix.stackexchange Why is printf better than echo?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。