条件使用

发布于 2024-12-24 18:51:37 字数 735 浏览 0 评论 0原文

我有一个包含内容的文件:

$> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星光不落少年眉 2024-12-31 18:51:37

你写的

我希望,如果 ${i}==1,

是的,但是如果 ${i} =“输入错误”或其他值,您的语句需要明确说明您的条件。另外,使用 less 将文件发送到管道不是标准情况,为什么不直接将文件名传递给 awk 进行处理,即

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;}' file1 \
  | while read i;do
 if (( "${i}" == 1 ));then
 echo -n "Would you like to continue? [yes or no]: "
 read yno
 . . .

我希望这有帮助

you wrote

I would expect, that if ${i}==1,

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.

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;}' file1 \
  | while read i;do
 if (( "${i}" == 1 ));then
 echo -n "Would you like to continue? [yes or no]: "
 read yno
 . . .

I hope this helps

蹲墙角沉默 2024-12-31 18:51:37

问题是唯一的输入来自 less,它被 awk 脚本完全消耗。控制台不可用。

这样的东西有用吗

i=$(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;}' file)
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

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

i=$(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;}' file)
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文