Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 条件使用_Bash_Shell - Fatal编程技术网

Bash 条件使用

Bash 条件使用,bash,shell,Bash,Shell,我有一个包含以下内容的文件: $> 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++;}

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

$> 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
less文件\
|awk'\
函数abs(x){return((x<0.0)?-x:x)+0.0}\
开始{i=1;}\
{新=旧;旧=1}\
{if(abs($1-new)>1)i++;}
结束{if(i>1)打印1;else打印0;}'\
|当我读书时;做
如果(${i}));然后
echo-n“是否继续?[是或否]:”
读yno
案例${yno}in
y)
回音继续。。。
;;
n)
回音退出。。。
;;
* )
回显“无效输入”
;;
以撒
否则回声继续。。。
fi
完成
我希望,如果${I}==1,那么我可以决定是否继续

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

是的,但是如果${i}=“Error on input”或其他值,您的语句需要显式声明您的条件。同样,使用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
 . . .
awk'\
函数abs(x){return((x<0.0)?-x:x)+0.0}\
开始{i=1;}\
{新=旧;旧=1}\
{if(abs($1-new)>1)i++;}
结束{if(i>1)打印1;else打印0;}'文件1\
|当我读书时;做
如果(“${i}”==1));然后
echo-n“是否继续?[是或否]:”
读yno
. . .
我希望这对你写的文章有所帮助

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

是的,但是如果${i}=“Error on input”或其他值,您的语句需要显式声明您的条件。同样,使用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
 . . .
awk'\
函数abs(x){return((x<0.0)?-x:x)+0.0}\
开始{i=1;}\
{新=旧;旧=1}\
{if(abs($1-new)>1)i++;}
结束{if(i>1)打印1;else打印0;}'文件1\
|当我读书时;做
如果(“${i}”==1));然后
echo-n“是否继续?[是或否]:”
读yno
. . .

我希望这会有所帮助。问题是,唯一的输入是来自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
i=$(awk'函数abs(x){return((x<0.0)?-x:x)+0.0}开始{i=1;}{new=old;old=$1}{if(abs($1-new)>1)i++;}结束{if(i>1)打印1;else打印0;}文件)
如果(${i}));然后
echo-n“是否继续?[是或否]:”
读yno
案例${yno}in
y)
回音继续。。。
;;
n)
回音退出。。。
;;
* )
回显“无效输入”
;;
以撒
否则回声继续。。。
fi

问题在于,唯一的输入来自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
i=$(awk'函数abs(x){return((x<0.0)?-x:x)+0.0}开始{i=1;}{new=old;old=$1}{if(abs($1-new)>1)i++;}结束{if(i>1)打印1;else打印0;}文件)
如果(${i}));然后
echo-n“是否继续?[是或否]:”
读yno
案例${yno}in
y)
回音继续。。。
;;
n)
回音退出。。。
;;
* )
回显“无效输入”
;;
以撒
否则回声继续。。。
fi

如果这些解决方案不起作用,如果您可以通过提示“您愿意…”来澄清您的意图,我们很乐意帮助您解决问题。为什么不能执行减法并允许退出代码确定awk之外发生的情况。如果您说“但不起作用:”,请编辑您的问题以显示您收到了哪些错误消息,或者指出您为什么说它不起作用。看起来应该能用。祝你好运。如果这些解决方案不起作用,如果你能通过提示“你愿意…”来澄清你的意图,我很乐意帮助你解决问题。为什么不能执行减法并允许退出代码确定awk之外发生的情况。如果您说“但不起作用:”,请编辑您的问题以显示您收到了哪些错误消息,或者指出您为什么说它不起作用。看起来应该能用。祝你好运