2012-02-27 101 views
22

我想打电话给一个bash脚本这样解析参数getopts的后

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

这是据我得到

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

但我如何得到path/to/file?循环运行后

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

回答

33

你可以这样做。

+2

可以写第一行'shift $((OPTIND - 1))' - 即丢失括号内的美元符号吗? – Armand 2014-10-02 10:02:26

+0

阿尔芒,所以它看起来如下:http://www.tldp.org/LDP/abs/html/arithexp.html – 2017-01-05 08:43:56