Bash脚本接受多个参数

时间:2017-04-05 14:51:41

标签: bash

我正在尝试修改此脚本以接受多个命令行参数,但它会继续循环。它应该允许用户输入多个用户名,然后运行。

#!/bin/bash
while [ $# -gt 0 ]; do #while arguments are greator than 1, do
        UNAME=$@ #set arg to var UNAME
        if [ -z ${UNAME} ]; then #if no argument is provided; then display this
            echo "Not enough arguments provided."
            echo "USAGE: $0 <user_name>"
            exit 1
        fi
        if grep ${UNAME} /etc/passwd >/dev/null; then #if UNAME exists then display info below
            echo "-------------------------------------------"
            echo "Username:       $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $1}' /etc/passwd)"
            echo "User ID (UID):  $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $3}' /etc/passwd)"
            echo "Group ID (GID): $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $4}' /etc/passwd)"
            echo "User info:      $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $5}' /etc/passwd)"
            echo "Home directory: $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $6}' /etc/passwd)"
            echo "Command shell:  $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $7}' /etc/passwd)"
            echo "-------------------------------------------"
        else #if UNAME is nonexistant then display this error message
            echo "-------------------------------------------"
            echo "\"${UNAME}\" does not exist."
            echo "-------------------------------------------"
        fi
done
        if [ $# -eq 0 ]; then #if num of args =0; then display this
                echo "Not enough arguments provided."
                echo "USAGE: $0 <user_name> [user_name] ..."
                exit 1
        fi
~

2 个答案:

答案 0 :(得分:1)

您正在使用while循环,但未更新索引。使用for循环更容易,例如:

#!/bin/bash
for UNAME in "$@"
do
        if [ -z ${UNAME} ]; then #if no argument is provided; then display this
            echo "Not enough arguments provided."
            echo "USAGE: $0 <user_name>"
            exit 1
        fi
        if grep ${UNAME} /etc/passwd >/dev/null; then #if UNAME exists then display info below
            echo "-------------------------------------------"
            echo "Username:       $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $1}' /etc/passwd)"
            echo "User ID (UID):  $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $3}' /etc/passwd)"
            echo "Group ID (GID): $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $4}' /etc/passwd)"
            echo "User info:      $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $5}' /etc/passwd)"
            echo "Home directory: $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $6}' /etc/passwd)"
            echo "Command shell:  $(awk -F ':' -v uname=${UNAME} '$0 ~ uname {print $7}' /etc/passwd)"
            echo "-------------------------------------------"
        else #if UNAME is nonexistant then display this error message
            echo "-------------------------------------------"
            echo "\"${UNAME}\" does not exist."
            echo "-------------------------------------------"
        fi
done

if [ $# -eq 0 ]; then #if num of args =0; then display this
        echo "Not enough arguments provided."
        echo "USAGE: $0 <user_name> [user_name] ..."
        exit 1
fi

在这种情况下,变量UNAME会在循环的每次迭代中更新,并使用下一个提供的参数。

答案 1 :(得分:0)

你可以很容易地通过案例和班次来做到这一点 以下是示例代码:

#!/bin/bash
function main{

    # Main Stuff
}

while [[ $# -gt 1 ]]
do
    arg="$1"
    case $arg in
        -a|--argument-1)

            # Stuff for argument 1

            shift
        ;;
        -b|--argument-2)

            # Stuff for argument 2

            shift
        ;;
        -c|--argument-3)

            # Stuff for argument 3

            shift
        ;;
        --default)
            DEFAULT=YES
        ;;
        *)
            shift
        ;;
        esac
    shift
done
main
exit

或者如果你想要一个总是独一无二的特殊参数(例如--help / -h)你可以这样做:

#!/bin/bash
function main{

    # Main Stuff

}
if [ "${*}" = "--help" ] || [ "${*}" = "-h" ]; then

    # Help stuff

fi
while [[ $# -gt 1 ]]
do
    arg="$1"
    case $arg in
        -a|--argument-1)

            # Stuff for argument 1

            shift
        ;;
        -b|--argument-2)

            # Stuff for argument 2

            shift
        ;;
        -c|--argument-3)

            # Stuff for argument 3

            shift
        ;;
        --default)
            DEFAULT=YES
        ;;
        *)
            shift
        ;;
        esac
    shift
done
main
exit