在Shell脚本中分配参数

时间:2015-02-13 09:42:39

标签: bash shell terminal

所以我刚接触shell脚本,目前我有一个sheLL我打电话来运行我的其他人看起来像这样

  # RC Script Call 
if [ "$1" = "runrc" ]
then
    echo "RC folder Setup is running"
    echo ""
    sh ./RCFolderSetup.sh
fi

if [ "$2" = "runrc" ]
then
    echo "RC folder Setup is running"
    echo ""
    sh ./RCFolderSetup.sh
fi

if [ "$3" = "runrc" ]
then
    echo "RC folder Setup is running"
    echo ""
    sh ./RCFolderSetup.sh
fi

# Trunk Script Call 
if [ "$1" = "runtrunk" ]
then
    echo "Trunk folder Setup is running"
    echo ""
    sh ./TrunkFolderSetup.sh
fi

if [ "$2" = "runtrunk" ]
then
    echo "Trunk folder Setup is running"
    echo ""
    sh ./TrunkFolderSetup.sh
fi

if [ "$3" = "runtrunk" ]
then
    echo "Trunk folder Setup is running"
    echo ""
    sh ./TrunkFolderSetup.sh
fi

# Testing Script Call 
if [ "$1" = "runtest" ]
then
    echo "Test folder Setup/Push the files is running"
    echo ""
    sh ./TestingScript.sh
fi

if [ "$2" = "runtest" ]
then
    echo "Test folder Setup/Push the files is running"
    echo ""
    sh ./TestingScript.sh
fi

if [ "$3" = "runtest" ]
then
    echo "Test folder Setup/Push the files is running"
    echo ""
    sh ./TestingScript.sh
fi

正如你所看到的,我必须分配一个以上的值,我的问题是,他们是一个更简单的方法来编写这个脚本,但仍然有它做同样的事情

2 个答案:

答案 0 :(得分:5)

我认为理智的替代品将是

#!/bin/sh

# while the number of arguments is not zero    
while [ "$#" -gt 0 ]; do

    # do the stuff for the first argument
    case "$1" in
        runrc)
            echo "RC folder Setup is running"
            echo ""
            sh ./RCFolderSetup.sh
            ;;
        runtrunk)
            echo "Trunk folder Setup is running"
            echo ""
            sh ./TrunkFolderSetup.sh
            ;;
        runtest)
            echo "Test folder Setup/Push the files is running"
            echo ""
            sh ./TestingScript.sh
            ;;
    esac

    # and shift the arguments back one so that $2 becomes $1, $3 becomes $2,
    # and so forth
    shift
done

如果执行顺序很重要(即使参数混乱也应该修复),这种方法可以修改为

#!/bin/sh

while [ "$#" -gt 0 ]; do
    case "$1" in
        runrc)
            do_runrc=yes
            ;;
        runtrunk)
            do_runtrunk=yes
            ;;
        runtest)
            do_runtest=yes
            ;;
    esac

    shift
done

if [ "$do_runrc" = yes ]; then
    echo "RC folder Setup is running"
    echo ""
    sh ./RCFolderSetup.sh
fi

if [ "$do_runtrunk" = yes ]; then
    echo "Trunk folder Setup is running"
    echo ""
    sh ./TrunkFolderSetup.sh

fi

if [ "$do_runtest" = yes ]; then
    echo "Test folder Setup/Push the files is running"
    echo ""
    sh ./TestingScript.sh
fi

答案 1 :(得分:2)

为这些if块创建一个函数,如下所示:

checkArg() {
    if [ "$1" = "$2" ]; then
      echo "$3"
      echo 
      sh "$4"
   fi
}

然后将其称为:

checkArg "$1" "runtrunk" "Trunk folder Setup is running" "./TrunkFolderSetup.sh"

checkArg "$2" "runtrunk" "Trunk folder Setup is running" "./TrunkFolderSetup.sh"

checkArg "$3" "runrc" "RC folder Setup is running" "./RCFolderSetup.sh"

## ...
## ...