2012-02-27 22 views
22

Quiero llamar a un script bash como estoargumentos de análisis sintáctico después getopts

$ ./scriptName -o -p -t something path/to/file 

Esto es por lo que yo entiendo

#!/bin/bash 

o=false 
p=false 

while getopts ":opt:" options 
do 
    case $options in 
     o) opt1=true 
     ;; 
     p) opt2=true 
     ;; 
     t) opt3=$OPTARG 
     ;; 
    esac 
done 

pero ¿cómo puedo obtener el path/to/file?

Respuesta

33

Usted puede hacer algo como:

shift $(($OPTIND - 1)) 
first_arg=$1 
second_arg=$2 

después del bucle se ha ejecutado.

+2

¿Se podría escribir la primera línea 'shift $ ((OPTIND - 1))' - es decir, perder el signo de dólar dentro de los paréntesis? – Armand

+0

Armand, por lo que parece según: http://www.tldp.org/LDP/abs/html/arithexp.html –

Cuestiones relacionadas