2012-01-04 38 views
1

我有內容的文件:條件使用

$> 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,那麼我就可以做出決定,我是否要繼續與否。

+0

快樂幫您解決問題,如果這些解決方案都沒有幫助,如果你能澄清通過提示「你......」你的意圖。爲什麼你不能做減法並且允許退出代碼來確定在awk之外發生了什麼。你說'但不起作用:',請編輯你的問題以顯示你得到了什麼錯誤信息,或者說明你爲什麼說它不起作用。它看起來應該起作用。祝你好運。 – shellter 2012-01-06 04:02:58

回答

1

你寫

我所期望的,如果$ {I} == 1,

是的,但如果$ {I} = 「錯誤輸入」 或其他一些價值,你的陳述需要明確說明你的狀況。同時使用更少的將文件發送到管道是不是一個標準的情況下,爲什麼不只是通過在文件名的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 
. . . 

我希望這有助於

0

的問題是唯一輸入來自awk腳本完全使用的less。控制檯不可用。

將這樣的事情是有用的

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