条件使用
我有一个包含内容的文件:
$> cat file
1
2
4
现在,我想使用/运行另一个脚本,如果数字(减法)之间的差异大于 1,否则从主脚本退出。
我尝试按以下方式执行此操作,但不起作用:
less file \
| awk '\
function abs(x){return (((x < 0.0) ? -x : x) + 0.0)}\
BEGIN{i=1;}\
{new=old;old=$1}\
{if(abs($1-new)>1)i++;}
END{if(i>1) print 1; else print 0;}' \
| while read i;do
if (( ${i} ));then
echo -n "Would you like to continue? [yes or no]: "
read yno
case ${yno} in
y )
echo Continuing...
;;
n )
echo Exiting...
;;
* )
echo "Invalid input"
;;
esac
else echo Cont...
fi
done
我希望如果 ${i}==1,那么我可以做出决定,无论我是否要继续。
I have a file with contents:
gt; cat file
1
2
4
Now, I would like to use/run another script, if the differences between the numbers (subtraction) is larger than 1, else exit from the main script.
I tried to do this in the following way, but does not work:
less file \
| awk '\
function abs(x){return (((x < 0.0) ? -x : x) + 0.0)}\
BEGIN{i=1;}\
{new=old;old=$1}\
{if(abs($1-new)>1)i++;}
END{if(i>1) print 1; else print 0;}' \
| while read i;do
if (( ${i} ));then
echo -n "Would you like to continue? [yes or no]: "
read yno
case ${yno} in
y )
echo Continuing...
;;
n )
echo Exiting...
;;
* )
echo "Invalid input"
;;
esac
else echo Cont...
fi
done
I would expect, that if ${i}==1, then I can make a decision, whether I want to continue or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你写的
是的,但是如果 ${i} =“输入错误”或其他值,您的语句需要明确说明您的条件。另外,使用 less 将文件发送到管道不是标准情况,为什么不直接将文件名传递给 awk 进行处理,即
我希望这有帮助
you wrote
Yes, but what if ${i} = "Error on input" or some other value, your statement needs to explicitly state your condition. Also using less to send a file to a pipe is not a standard situation, why not just pass in the filename to awk for processing, i.e.
I hope this helps
问题是唯一的输入来自 less,它被 awk 脚本完全消耗。控制台不可用。
这样的东西有用吗
The problem is that the only input is from less which is fully consumed by the awk script. The console is not available.
Would something like this be useful