【问题标题】:Count is not Captured in Shell Script VariableShell 脚本变量中未捕获计数
【发布时间】:2020-10-09 18:09:01
【问题描述】:

我试图通过运行命令并尝试在 shell 脚本变量中捕获结果来捕获活动进程的数量,但不幸的是,没有捕获任何内容。代码如下:

#!/bin/ksh

## Checking whether or not the Previous Build is Completed

count_build_status=`ps -ef | grep BDD_PreCheck.sh | grep -c FT_BGmgmt` | tee -a ${logFile}

    echo "The Value of Count Build Status is $count_build_status"

        if [[ "${count_build_status}" != "0" ]]

        then

            echo INFO -  The previous build has not ended yet. Please Wait for some time or contact the Administrator | tee -a $logFile

        exit 1;

        fi

exit 0;

这里,ps -ef | grep BDD_PreCheck.sh | grep -c FT_BGmgmt 如果单独执行,结果为 0,但 'count_build_status' 中存储的值为 null。

谁能帮忙?

【问题讨论】:

  • bash 不是 ksh。

标签: bash shell variables unix sh


【解决方案1】:

示例:将 cmd-line 的输出捕获到变量并使用 tee 附加日志文件:

var=`ps -ef | grep 0 | grep -c 1 | tee -a log`
echo $var

【讨论】:

    【解决方案2】:

    这里的问题是反引号,当没有在反引号内完成 tee'ing 时,它不会为变量赋值。它应该包含在 logFile 的末尾,如下面的代码所示。

    #!/bin/bash
    
    logFile=log.txt
    
    ## Checking whether or not the Previous Build is Completed
    
    count_build_status=`ps -ef | grep BDD_PreCheck.sh | grep -c FT_BGmgmt | tee -a ${logFile}`
    
        echo "The Value of Count Build Status is $count_build_status"
    
            if [[ "${count_build_status}" != "0" ]]
    
            then
    
                echo INFO -  The previous build has not ended yet. Please Wait for some time or contact the Administrator | tee -a $logFile
    
            exit 1;
    
            fi
    
    exit 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2017-07-05
      • 2022-12-18
      • 2013-10-19
      • 2013-06-15
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多