Wie beide kurze und lange Optionen zur gleichen Zeit in bash unterstützen? [Duplikat]

StackOverflow https://stackoverflow.com/questions/4180880

  •  10-10-2019
  •  | 
  •  

Frage

Diese Frage bereits eine Antwort hier:

Ich mag sowohl kurze und lange Optionen in bash Skripte unterstützen, so kann man:

$ foo -ax --long-key val -b -y SOME FILE NAMES

ist es möglich?

War es hilfreich?

Lösung

getopt unterstützt lange Optionen.

http://man7.org/linux/man-pages /man1/getopt.1.html

Hier ist ein Beispiel Ihre Argumente mit:

#!/bin/bash

OPTS=`getopt -o axby -l long-key: -- "$@"`
if [ $? != 0 ]
then
    exit 1
fi

eval set -- "$OPTS"

while true ; do
    case "$1" in
        -a) echo "Got a"; shift;;
        -b) echo "Got b"; shift;;
        -x) echo "Got x"; shift;;
        -y) echo "Got y"; shift;;
        --long-key) echo "Got long-key, arg: $2"; shift 2;;
        --) shift; break;;
    esac
done
echo "Args:"
for arg
do
    echo $arg
done

Ausgabe von $ foo -ax --long-key val -b -y SOME FILE NAMES:

Got a
Got x
Got long-key, arg: val
Got b
Got y
Args:
SOME
FILE
NAMES
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top