Unix Shell脚本中的变量问题
发布时间:2020-12-15 21:27:10 所属栏目:安全 来源:网络整理
导读:我无法从循环内部传递变量. 码: # find all output.txt that has been modified last 24h ...PROCESSED=1find ${PROCESSED_DIR} -mtime -1 -name "output.txt" | while read ido # .. and compare those with TMP_TXT if diff $i ${TMP_TXT} /dev/null then
我无法从循环内部传递变量.
码: # find all output.txt that has been modified last 24h ... PROCESSED=1 find ${PROCESSED_DIR} -mtime -1 -name "output.txt" | while read i do # .. and compare those with TMP_TXT if diff $i ${TMP_TXT} > /dev/null then # If both are same EXIT search loop PROCESSED=0 exit fi done echo "PROCESSED=${PROCESSED}" 这将始终输出1.任何想法如何使PROCESSED = 0? 这是在Solaris 9计算机上完成的. 解决方法
原因是while-command在管道内运行,这意味着它在子shell中运行,因此变量赋值不会传播到调用shell.
一种解决方案是重定向输入(您可能需要先将其写入临时文件): while read f; do ... done < foo.tmp 另一种解决方案是使用while命令中的stdout传播PROCESSED的值: PROCESSED=`find | while read f; do echo "Echo other stuff on stderr" >&2 echo 1 done` (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |