如何使用Bash getopts命令一起获取所有参数

时间:2013-09-06 18:15:41

标签: bash getopts

我需要在这里解析几个文件,称为parse.sh。我必须为它启用可选参数,其中-l表示行,-f表示字段。所以运行该程序将是./parse.sh -l 5 -f 14 foo。如果没有-l或-f参数,我希望程序默认解析所有行和所有字段。如果指定了-l,我希望它只解析foo的那一行,如果同时指定-f,我希望它只解析该字段。我看到getopts通常是这样的:

while getopts "l:f:" opts; do
    case $opts in
        l) #code to parse that line;;
        f) #code to parse that field;;
    case
done

但这不是我需要的,因为我希望-l和-f有时一起工作。我想也许我应该做getopts解析所有选项到数组,然后编写基于解析该数组的代码?有更好的选择吗?

这是我的代码:

while getopts "l:f:" opt;
do
    options=${opt}${options}
    case $opt in
            l) lineNumber=$OPTARG ;;
            f) fieldNumber=$OPTARG ;;
    esac
done

case $options in
    f) echo "Parse field $fieldNumber of each line" ;;
    l) echo "Parse all fields of line number $lineNumber" ;;
    lf | fl) echo "Parse field $fieldNumber of line $lineNumber" ;;
    *) echo "Parse all fields of all lines" ;;
esac

3 个答案:

答案 0 :(得分:2)

#!/bin/bash

parse()
{
        local lines=$1
        local fields=$2
        local file=$3

        # logic goes here
        echo "parsing line(s) ${lines} and field(s) ${fields} of file ${file}"
}

lines=all
fields=all

while getopts "l:f:" o; do
        case $o in
                l) lines=${OPTARG}  ;;
                f) fields=${OPTARG} ;;
        esac
done
shift $((OPTIND-1))

for file; do
        parse "${lines}" "${fields}" "${file}"
done

示例运行:

$ ./t.sh foo.txt bar.txt
parsing line(s) all and field(s) all of file foo.txt
parsing line(s) all and field(s) all of file bar.txt

$ ./t.sh -l 10 foo.txt bar.txt
parsing line(s) 10 and field(s) all of file foo.txt
parsing line(s) 10 and field(s) all of file bar.txt

$ ./t.sh -l 10 -f 5 foo.txt bar.txt
parsing line(s) 10 and field(s) 5 of file foo.txt
parsing line(s) 10 and field(s) 5 of file bar.txt

$ ./t.sh -f 5 foo.txt bar.txt
parsing line(s) all and field(s) 5 of file foo.txt
parsing line(s) all and field(s) 5 of file bar.txt

答案 1 :(得分:1)

#!/bin/bash

while getopts "l:f:" opts; do
    case $opts in
    l)
        lOn=1
        ;;
    f)
        fOn=1
        ;;
    esac
done

if [[ -n $lOn && -n $fOn ]]; then
    echo 'both l and f'
elif [[ -n $lOn ]]; then
    echo 'just l'
elif [[ -n $fOn ]]; then
    echo 'just f'
else
    echo 'nothing'
fi

如果您想检查其他变量或执行更复杂的操作,语句会为您提供更大的灵活性。这不会在sh中有效,除非您将[[ ]]更改为[ ]

答案 2 :(得分:1)

我制作了一个概念剧本。请试试。

#!/bin/bash

PARSE_SPECIFIC_LINE=0
PARSE_SPECIFIC_FIELD=0

shopt -s extglob

while getopts "l:f:" opts; do
    case $opts in
    l)
        if [[ $OPTARG != +([[:digit:]]) || OPTARG -lt 1 ]]; then
            echo "Invalid argument to -l: $OPTARG"
            exit 1
        fi
        PARSE_SPECIFIC_LINE=$OPTARG
        ;;
    f)
        if [[ $OPTARG != +([[:digit:]]) || OPTARG -lt 1 ]]; then
            echo "Invalid argument to -f: $OPTARG"
            exit 1
        fi
        PARSE_SPECIFIC_FIELD=$OPTARG
        ;;
    esac
done

FILES=("${@:OPTIND}")

function parse_line {
    local LINE=$1
    if [[ -n $LINE ]]; then
        if [[ PARSE_SPECIFIC_FIELD -gt 0 ]]; then
            read -ra FIELDS <<< "$LINE"
            echo "${FIELDS[PARSE_SPECIFIC_FIELD - 1]}"
        else
            echo "$LINE"
        fi
    fi
}

for F in "${FILES[@]}"; do
    if [[ -e $F ]]; then
        if [[ PARSE_SPECIFIC_LINE -gt 0 ]]; then
            parse_line "$(sed -n "${PARSE_SPECIFIC_LINE}{p;q}" "$F")"
        else
            while read -r LINE; do
                parse_line "$LINE"
            done < "$F"
        fi
    else
        echo "File does not exist: $F"
    fi
done

我用

运行它
bash script.sh -f 2 <(for i in {1..20}; do echo "$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM"; done)

我得到了

1031
1072
4350
12471
31129
32318
...

添加-l 5

11604