2013-04-24 25 views
6

Bash'daki getopts kullanarak komut satırı argümanlarını işlemeye çalışıyorum. Gereksinimlerden biri isteğe bağlı sayıda argümanların işlenmesi içindir (alıntıların kullanımı olmadan).Getopts (bash) kullanarak çoklu seçenek argümanları (bash)

1 örnek (sadece 1 argüman kapar)

madcap:~/projects$ ./getoptz.sh -s a b c 
-s was triggered 
Argument: a 

2 örneği (Ben bu gibi davranır ama argüman"

madcap:~/projects$ ./getoptz.sh -s "a b c" 
-s was triggered 
Argument: a b c 

alıntı gerek kalmadan istediğiniz bir yolu var mı ? İşte

kod bunu yapmak Şimdi sahip:

#!/bin/bash 
while getopts ":s:" opt; do 
    case $opt in 
    s) echo "-s was triggered" >&2 
     args="$OPTARG" 
     echo "Argument: $args" 
     ;; 
    \?) echo "Invalid option: -$OPTARG" >&2 
     ;; 
    :) echo "Option -$OPTARG requires an argument." >&2 
     exit 1 
     ;; 
    esac 
done 
+2

Bu yardımcı olabilir: http://stackoverflow.com/a/7530327/1983854 – fedorqui

+0

Daha fazla bilgi gereklidir. Getoptz.sh -s a -b c' verildiğinde hangi davranışı istiyorsunuz? '-b' -s' için bir argüman mı, yoksa' -' yeni bir seçeneği mi gösteriyor? –

+0

İlgili, ama hiçbir suretle, [Her seçenek için farklı seçenekler ve farklı argümanlar ile farklı programlar çağırma] (http://stackoverflow.com/questions/15442950/). Genel olarak, standart komut arabirimi yönergelerini kullanmak en iyisidir [POSIX Yardımcı Programları] (http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html) –

cevap

3

Komut satırı bağımsız değişkenlerini kendiniz ayrıştırabilirsiniz, ancak getopts komutu, tek bir seçeneğe birden çok bağımsız değişken tanımak üzere yapılandırılamaz. fedorqui's recommendation iyi bir alternatiftir.

while [[ "$*" ]]; do 
    if [[ $1 = "-s" ]]; then 
     # -s takes three arguments 
     args="$2 $3 $4" 
     echo "-s got $args" 
     shift 4 
    fi 
done 
+0

Nitekim, "tek bir seçeneğe birden fazla argüman tanımak" mümkün değil, ancak seçenek tekrarlanabilir. Cevaplara bir örnek ekledim çünkü bence gerçekten istenen şey bu. – mivk

11

Ben ne istediğinizi tek seçenekten değerlerinin bir listesini elde etmek için olduğunu düşünüyorum:

İşte seçenekle kendini ayrıştırma yollarından biridir. Bunun için, seçeneği gerektiği kadar tekrarlayabilir ve bir diziye argüman ekleyebilirsiniz.

#!/bin/bash 

while getopts "m:" opt; do 
    case $opt in 
     m) multi+=("$OPTARG");; 
     #... 
    esac 
done 
shift $((OPTIND -1)) 

echo "The first value of the array 'multi' is '$multi'" 
echo "The whole list of values is '${multi[@]}'" 

echo "Or:" 

for val in "${multi[@]}"; do 
    echo " - $val" 
done 

çıkışı olacaktır:

$ /tmp/t 
The first value of the array 'multi' is '' 
The whole list of values is '' 
Or: 

$ /tmp/t -m "one arg with spaces" 
The first value of the array 'multi' is 'one arg with spaces' 
The whole list of values is 'one arg with spaces' 
Or: 
- one arg with spaces 

$ /tmp/t -m one -m "second argument" -m three 
The first value of the array 'multi' is 'one' 
The whole list of values is 'one second argument three' 
Or: 
- one 
- second argument 
- three