在IF期间,BASH脚本以未知原因结束

时间:2015-04-30 12:59:35

标签: bash shell command-line jmeter

我有一个运行一堆Jmeter脚本的bash脚本。 我刚刚修改了脚本,所以它只会在环境变量设置正确的情况下运行(在这种情况下为2之一)。

如果使用第一个环境,则脚本结束。 如果使用第二个环境选项,脚本将运行并正确完成。

全文:

<?php

似乎导致我问题的部分是

#!/bin/bash
if [ "$#" -ne 4 ]; then
    echo "Parameters required: version environment plc_username plc_password"
else
    cd $HOME/workspace/OptimumStart/resources/jmeterrun/

    if [[ "$2" =~ ^hibagon ]]; then
        echo 'doing hibagon'
        echo 'using version '$1 
        source ./hibagon_env.sh $1 
        echo 'done hibagon env'
    elif [[ "$2" =~ ^kameosa ]]; then
        echo 'doing kameosa'
        echo 'using version '$1 
        source ./kameosa_env.sh $1 
        echo 'done kameosa env'
        if [[ "$2" =~ ^hibagon ]] || [[ "$2" =~ ^kameosa ]]; then
            echo "Continuing to tests"
            source ./user_env.sh $3 $4
            echo 'done user env'
            export jmtestFeature=DEV15298
            env | grep jm
            $HOME/apache-jmeter-2.11/bin/jmeter -n -t $HOME/workspace/OptimumStart/resources/jmeterscripts/DEV15298_Feedback/QA3 1985_AllowCoolingZwaveFeedback.jmx
            echo 'done Allow Cooling Tests'
            $HOME/apache-jmeter-2.11/bin/jmeter -n -t $HOME/workspace/OptimumStart/resources/jmeterscripts/DEV15298_Feedback/QA3 1986_WarmingZwaveFeedback.jmx
            echo 'done Warming Tests'
            $HOME/apache-jmeter-2.11/bin/jmeter -n -t $HOME/workspace/OptimumStart/resources/jmeterscripts/DEV15298_Feedback/QA3 1987_MaintainingZwaveFeedback.jmx
            echo 'done Maintaining Tests'
            $HOME/apache-jmeter-2.11/bin/jmeter -n -t $HOME/workspace/OptimumStart/resources/jmeterscripts/DEV15298_Feedback/QA31993_PreHeatingZwaveFeedback.jmx
            echo 'done Pre Heating Tests'
            $HOME/apache-jmeter-2.11/bin/jmeter -n -t $HOME/workspace/OptimumStart/resources/jmeterscripts/DEV15298_Feedback/QA-32651_PreZwaveOccChange.jmx
            echo 'Done Occupancy change test'
            $HOME/apache-jmeter-2.11/bin/jmeter -n -t $HOME/workspace/OptimumStart/resources/jmeterscripts/Createtree.jmx
            echo 'Created tree of results'  
        else
            echo "Unkown server, stopping tests"
        fi
    fi
fi

如果使用3种不同的环境从命令行运行,则会发生以下情况。

if [[ "$2" =~ ^hibagon ]]; then
  echo 'doing hibagon'
  echo 'using version '$1 
  source ./hibagon_env.sh $1 
  echo 'done hibagon env'
elif [[ "$2" =~ ^kameosa ]]; then
  echo 'doing kameosa'
  echo 'using version '$1 
  source ./kameosa_env.sh $1 
  echo 'done kameosa env'

继续运行脚本。

./jmeterrun2.sh testversion kameosa  shaun shaun
doing kameosa
using version testversion
done kameosa env
Continuing to tests
done user env
jmtestFeature=DEV15298
OLDPWD=/home/shaunb/workspace/OptimumStart/resources/jmeterrun
jmuser=shaun
jmpass=shaun
jmPort=443
PWD=/home/shaunb/workspace/OptimumStart/resources/jmeterrun
jmtestVersion=testversion
jmHost=kameosa.passivonline.com

这只是停在那里,什么都不做

./jmeterrun2.sh testversion hibagon  shaun shaun
doing hibagon
using version testversion
done hibagon env

这没有任何作为

如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

我已将您的脚本分解为函数库,我称之为jmFunctionLib。你“源”或“点(。)”库。 e.g:

ittoolbox.$ . ./jmFunctionLib
ONLY executable statement
USAGE: myJmrun version environment plc_username plc_password

我希望你能解决这个问题。这种风格的目的是开发一种实践,帮助您避免“这个脚本有什么问题”类型的问题。怎么样?

  1. 为您提供可测试的中间代码块(函数)

  2. 通过折叠很长的代码段来使嵌套难以理解,

  3. 通过将重复使用命令收集到更加语义的形式, 例如“myJmaster”功能很可能成为“主要”。

  4. 维护变得更加容易。

  5. 享受!函数库:

    function comment
    { 
        echo $* 1>&2
    }
    function quietly
    { 
        "$@" 2> /dev/null
    }
    function myJminit
    {
        comment USAGE: myJmrun version environment plc_username plc_password
        quietly comment 1. put all announcements on stderr as comments;
        quietly comment 2. collapse into functions;
        quietly comment 3. recognize parallel logic
    }
    function myJmeter
    { 
        $HOME/apache-jmeter-2.11/bin/jmeter -n -t \
        $HOME/workspace/OptimumStart/resources/jmeterscripts/"$1";
        comment done $(echo "$1"|sed 's/.*_//; s/.jmx$//') Tests
    }
    function myJmfeed
    { 
        myJmeter DEV15298_Feedback/"$1"
    }
    function myJmRun
    { 
        [[ $# -lt 4 ]] && { 
            myJminit
            return
        };
        cd $HOME/workspace/OptimumStart/resources/jmeterrun/;
        case $2 in 
            *hibagon* | *kameosa*)
                myJmaster "$@"
            ;;
            *)
                comment Unkown server, stopping tests
            ;;
        esac
    }
    function myJmaster
    { 
        comment "doing $2";
        comment "using version $1";
        source ${2}_env.sh;
        comment done $2 env;
        comment contintuing to tests;
        source ./user_env.sh $3 $4;
        comment done user env;
        export jmtestFeature=DEV15298;
        comment $(env | grep jm );
        myJmfeed "QA3 1985_AllowCoolingZwaveFeedback.jmx";
        myJmfeed "QA3 1986_WarmingZwaveFeedback.jmx";
        myJmfeed "QA3 1987_MaintainingZwaveFeedback.jmx";
        myJmfeed "QA31993_PreHeatingZwaveFeedback.jmx";
        myJmfeed "QA-32651_PreZwaveOccChange.jmx";
        myJmeter Createtree.jmx
    }
    comment ONLY executable statement; myJminit