【问题标题】:I am trying to run a script in -vx mode , but getting error我正在尝试以 -vx 模式运行脚本,但出现错误
【发布时间】:2016-04-17 09:49:37
【问题描述】:

" ././tst.ksh: line 16: ONL P BL9_RATED_EVENT_D 1,295 780 4,063,232 60 LOCA SYST AUTO: 无法打开[没有这样的文件或目录] "

我正在尝试在 -vx 模式下执行以下脚本 我不明白为什么在输出中我得到这个

!/bin/ksh -xv

    #for i in `cat /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log | grep ONL | column -t`
    while i= read -r line
    do
    echo $i
    stat=`echo $i | cut -d" " -f1`
    typ=`echo $i | cut -d" " -f2`
    tbs=`echo $i | cut -d" " -f3`
    tot=`echo $i | cut -d" " -f4`
    free=`echo $i | cut -d" " -f5`
    lrg=`echo $i | cut -d" " -f6`
    fr=`echo $i | cut -d" " -f7`
    Ext=`echo $i | cut -d" " -f8`
    All=`echo $i | cut -d" " -f9`
    spc=`echo $i | cut -d" " -f10`
    done < `cat /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log | grep ONL | column -t`
+ cat /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log | grep ONL | column -t+ cat /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log
+ column -t
+ grep ONL
././tst.ksh: line 16: ONL  P  BL9_RATED_EVENT_D  1,295  780  4,063,232  60  LOCA  SYST  AUTO: cannot open [No such file or directory]

【问题讨论】:

    标签: unix ksh


    【解决方案1】:

    我将在这里解释一些问题。
    读取命令归档一个在命令之后给出的变量。在您的代码中,line 是填充变量的名称,i = 不属于这里。第一个改进是:

    while read -r i                           
    do                                        
        echo $i                               
        stat=`echo $i | cut -d" " -f1`        
        typ=`echo $i | cut -d" " -f2`         
        tbs=`echo $i | cut -d" " -f3`         
        tot=`echo $i | cut -d" " -f4`         
        free=`echo $i | cut -d" " -f5`        
        lrg=`echo $i | cut -d" " -f6`         
        fr=`echo $i | cut -d" " -f7`          
        Ext=`echo $i | cut -d" " -f8`         
        All=`echo $i | cut -d" " -f9`         
        spc=`echo $i | cut -d" " -f10`        
    done < /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log 
    

    我还更改了最后一行。 while 循环想要从文件中读取,而不是从命令的输出中读取。
    注意:你会对 Bash 示例感到困惑。当您想将命令的输出重定向到 while 循环中时,您可以在 Bash 中使用一些特殊的语法。 在 Bash 中,您将需要它,以便在完成 while 循环后知道在 while 循环中设置的变量。
    在您的情况下,ksh,您可以通过从您的命令开始并将其重定向到 while 循环来解决它。
    在不修复逻辑的情况下修复代码会给出

    cat /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log | grep ONL | column -t | while read -r i
    do
        echo $i
        stat=`echo $i | cut -d" " -f1`
        typ=`echo $i | cut -d" " -f2`
        tbs=`echo $i | cut -d" " -f3`
        tot=`echo $i | cut -d" " -f4`
        free=`echo $i | cut -d" " -f5`
        lrg=`echo $i | cut -d" " -f6`
        fr=`echo $i | cut -d" " -f7`
        Ext=`echo $i | cut -d" " -f8`
        All=`echo $i | cut -d" " -f9`
        spc=`echo $i | cut -d" " -f10`
    done
    

    使用column -t 没有帮助。在尝试将字段拆分为空格之前,您需要先用一个空格分隔字段。您需要用空格替换制表符,并将多个空格压缩为一个空格。 您可以为此使用expand -1 | tr -s " "。 我想建议用符号$(subcommand) 替换反引号,但您可以使用读取命令分配变量。 我的建议是

    grep ONL /tefnfs/tef/tools/tooladm/Users/Jithesh/prd3cust.log | expand -1 | tr -s " " |
    while read -r stat typ tbs tot free lrg fr Ext All spc; do
            echo "Processed line starting with ${stat}"
    done
    

    现在你应该在 while 循环中做一些事情。每次 while 循环处理下一行时,变量都会更改。在 while 循环之后,变量将填充最后一行处理的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 2017-07-17
      • 2011-07-02
      • 2022-07-24
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多