getopt example

| | Comments (0)

getopt 是 shell 裡抓參數的好工具
例: getopt abc:d: 容許參數 -a -b -c -d, -c and -d 後面要接參數

#!/bin/sh
set - `getopt abc:d: $*`
while true
do
case $1 in
-a)
echo option -a
shift
;;
-b)
echo option -b
shift
;;
-c)
echo option -c=$2
shift 2
;;
-d)
echo option -d=$2
shift 2
;;
--)
shift
break
;;
*)
echo "error!"
exit 1
;;
esac
done

執行結果
# ./go -a
option a
# ./go -c jack
option -c=jack
# ./go -b -c jack
option -b
option -c=jack
# ./go -b -c test -d
getopt: option requires an argument -- d
option -b
option -c=test
(使用後面要加參數的 option 會提示)
# ./go -b -c test -f
getopt: invalid option -- f
option -b
option -c=test
(使用未支援的參數會提示)

Leave a comment

March 2009

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

About this Entry

This page contains a single entry by Pank published on May 5, 2004 5:30 PM.

Mozilla 系列軟體啟動時問你要使用那個設定檔, 又說有人在使用 was the previous entry in this blog.

smb.conf lanman auth is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.