BASH:如何在使用optargs时首先设置变量?

时间:2017-08-25 15:10:23

标签: bash scripting

我有一个使用optargs的BASH脚本。我的几个标志用于设置变量。有没有办法强制我的脚本在运行其他函数之前设置这些变量?

例如,我的标志-b -r -t需要参数来设置变量。我的另一个标志-a取决于所设置的变量。当我调用我的脚本./myscript.sh -a -b foo -r bar -t foobar时,由-a触发的函数无法访问已设置的变量。当我执行./myscript.sh -b foo -r bar -t foobar -a时,触发的函数可以访问变量。

作为参考,到目前为止我的WIP代码是:

#!/bin/bash
# -a        = run all functions
# -c        = checkout only
# -p        = pull only
# -x        = checkout, pull, mvn clean install
# -m        = mvn clean install only
# -t        = top level directory of your projects
# -r        = path to coreRepos.txt
# -b        = branch name to check out
#

BRANCH=""
BASEDIR=""
COREREPOS=""
allFunctions() { 
    echo "In all"
    checkPull
    maven

    echo "$BRANCH $BASEDIR $COREREPOS"
    return
  }
checkout() { 
    echo "In checkout" 
    return
}
pull() { 
    echo "In pull" 
    return
}
checkPull() { 
    echo "In checkPull" 
    checkout
    pull
    return
}
maven() { 
    echo "In maven" 
    return
}



while getopts ":acpxmht:r:b:" opt; do
  case $opt in

    b)
        echo "-branch set as: $OPTARG"
        BRANCH=$OPTARG
        export BRANCH
        echo "Echoing $BRANCH"
        ;;
    t)
        echo "-t set as: $OPTARG"
        BASEDIR=$OPTARG
        export BASEDIR
        ;;
    r)
        echo "-r set as: $OPTARG"
        COREREPOS=$OPTARG
        export COREREPOS
        ;;  
    a)
        echo "-all triggered"
        allFunctions
        exit
        ;;
    c)
        echo "-check triggered"
        ;;
    p)
        echo "-pull triggered"
        ;;
    x)
        echo "-check-pull triggered"
        ;;
    m)
        echo "-maven-only triggered"
        ;;
    \?)
        echo "Invalid option: -$OPTARG" >&2
        exit 1
        ;;
    h)
        echo "Help invoked..."
        exit 0
        ;;
  esac
done


exit 0

2 个答案:

答案 0 :(得分:2)

解析命令行选项时,尽量少做一些工作。只记录传递的选项以及任何参数。完成后,您可以采取参数请求的任何操作。这可以让您在事件发生时完全控制。它不仅可以确保在调用需要它的函数之前设置BRANCH,而且如果命令行看起来像check,它可以避免在调用allActions之前不必要地调用myscript.sh -c -a }。

#!/bin/bash
# -a        = run all functions
# -c        = checkout only
# -p        = pull only
# -x        = checkout, pull, mvn clean install
# -m        = mvn clean install only
# -t        = top level directory of your projects
# -r        = path to coreRepos.txt
# -b        = branch name to check out
#

BRANCH=""
BASEDIR=""
COREREPOS=""
allFunctions() { 
    echo "In all"
    checkPull
    maven

    echo "$BRANCH $BASEDIR $COREREPOS"
    return
}
checkout() { 
    echo "In checkout" 
    return
}
pull() { 
    echo "In pull" 
    return
}
checkPull() { 
    echo "In checkPull" 
    checkout
    pull
    return
}
maven() { 
    echo "In maven" 
    return
}



while getopts ":acpxmht:r:b:" opt; do
  case $opt in

    b)
        echo "-branch set as: $OPTARG"
        BRANCH=$OPTARG
        export BRANCH
        echo "Echoing $BRANCH"
        ;;
    t)
        echo "-t set as: $OPTARG"
        BASEDIR=$OPTARG
        export BASEDIR
        ;;
    r)
        echo "-r set as: $OPTARG"
        COREREPOS=$OPTARG
        export COREREPOS
        ;;  
    a)
        echo "-all triggered"
        doAll=1
        ;;
    c)
        echo "-check triggered"
        doCheck=1
        ;;
    p)
        echo "-pull triggered"
        doPull=1
        ;;
    x)
        echo "-check-pull triggered"
        doCheckPull=1
        ;;
    m)
        echo "-maven-only triggered"
        doMaven=1
        ;;
    \?)
        echo "Invalid option: -$OPTARG" >&2
        exit 1
        ;;
    h)
        echo "Help invoked..."
        exit 0
        ;;
  esac
done

if [[ $doAll = 1 ]]; then
    allFunctions
    exit 0
fi

if [[ $doCheck = 1 ]]; then
    checkout
fi

if [[ $doPull = 1 ]]; then
    pull
fi

if [[ $doCheckPull = 1 ]]; then
    checkPull
fi

# etc


exit 0

答案 1 :(得分:0)

#!/bin/bash
root_hash() {
    echo -e $ROOTARG
}
user_hash() {
    echo -e $USERARG
}

while getopts "u:h:r:" Blah; do
  case "$Blah" in
    u)  USERARG=$OPTARG
        export USERARG
        user_hash
        ;;
    r)  ROOTARG=$OPTARG
        export ROOTARG
        root_hash
        ;;
    h)  help
        ;;
    ?)  help
    
  esac
done
shift "$(($OPTIND -1))"