Comment soutenir les deux options courtes et longues en même temps en bash? [dupliquer]

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

  •  10-10-2019
  •  | 
  •  

Question

    

Cette question a déjà une réponse ici:

         

Je veux soutenir court et options longues dans les scripts bash, donc on peut:

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

est-il possible?

Était-ce utile?

La solution

getopt propose des options longues.

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

Voici un exemple en utilisant vos arguments:

#!/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

Sortie de $ 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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top