【问题标题】:working fine in command prompt but not in shell script在命令提示符下工作正常,但在 shell 脚本中不行
【发布时间】:2014-04-17 16:10:36
【问题描述】:

我必须从 zip 文件中的特定行获取字段计数。 当我在 Linux 中的命令提示符下查询时,它会给出输出。

gunzip -c file | grep 'good' | awk -F' ' '{prinf NF}'

在命令行上执行此查询时,它会给出正确的输出 10。

当我将它分配给 shell 脚本中的变量并执行 .sh 时,它给了我错误

cat > find.sh
cnt=`gunzip -c file | grep 'good' | awk -F' ' '{print NF}'`
echo $cnt
./ sh find.sh 
find.sh: 2: find sh: 10: not found

请帮忙解决这个问题..!!

【问题讨论】:

    标签: linux shell ubuntu awk command-prompt


    【解决方案1】:

    试试这个:

    cat find.sh
    #!/bin/bash
    cnt=$(gunzip -c file | awk '/good/ {prinf NF}')
    echo $cnt
    
    ./find.sh
    10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 2017-07-19
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多