shell script -- getopt 問題 - Linux

Joe avatar
By Joe
at 2008-08-13T06:31

Table of Contents

總共有三個地方要做修正
1. 把參數傳給 getopt 要加上正確的引號,
請參考 man bash 有關 "$*" "$@"
2. 原版 getopt 並不處理空白或其他特殊字元,
必須啟動 getopt 的加強版功能,
請參考 man getopt 有關 COMPATIBILITY 一節
3. 參數傳回 bash 不能直接丟過去,
要告訴 bash 重新 parse 參數串裡頭的引號空白,
請參考 man bash 有關 eval

※ 引述《PHP5 (Powered by Zend Engine2)》之銘言:
: getopt要如何正確處理含有空白的arg?
: 以下是小弟的script
: test.sh
: #!/bin/bash
: args=`getopt a:b: $*`

以上這一行改成:
args=$(getopt -o a:b: "$@")
或者是
args=$(getopt -s bash a:b: "$@")

: if [ $? -ne 0 ]; then
: echo error
: exit 1
: fi
: set -- $args

以上這一行改成:
eval set -- "$args"

: while true
: 以下省略

--
A quotation from the movie "The Se7en":
Ernest Hemingway once wrote that,
"The world is a fine place and worth fighting for."
I agree with the second part.

--
Tags: Linux

All Comments

Enid avatar
By Enid
at 2008-08-13T14:32
感謝!

請問 cross-compiler

Queena avatar
By Queena
at 2008-08-13T02:29
請問板上的各位高手們, cross-compiler 有辦法將自定義的 library 作 compile 嗎? 現在在 linux 上可以 include 可以 run。 但我想要用 nios2-linux-gcc ( cross compiler )來產生 flt格式的執行檔..... 現在我有兩 ...

ubuntu 8.04如何離開x-window?

Hamiltion avatar
By Hamiltion
at 2008-08-13T02:00
※ 引述《mgdesigner (魔法設計師)》之銘言: : ※ 引述《amd3dnow (qq)》之銘言: : : 之前用centos只要執行init 3 就能離開x-window : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : Ubuntu改成sudo init 1就可以進入單人、 ...

無線網路卡驅動程式

Charlie avatar
By Charlie
at 2008-08-13T01:40
請問板上的高手們, 哪裡可以下載無線網路卡(CG-WLUSB2GNR)的驅動程式 我用的是COREGA的產品 可是他們沒有支援LINUX作業系統 想問哪裡可以下載這支無線網路卡的支援軟體 - ...

請問現在Ubuntu Source.list 套件庫

Ethan avatar
By Ethan
at 2008-08-12T19:52
請問 ftp.twaren.net 是掛掉了嗎 他一直傳給我 403 囧 讓我一直不能灌套件 囧 有其它人能回報一下嗎 謝謝 - ...

Re: PCManX GTK+ 0.3.8 釋出

Dorothy avatar
By Dorothy
at 2008-08-12T19:50
※ 引述《FourDollars.bbsatbbs.sayya.org (四元)》之銘言: : ※ 引述《ReverbHorn.bbsatptt.cc (001880)》之銘言: : andgt; 剛剛下載安裝 : andgt; 但是只要超過一頁的文章就無法下載 : andgt; 只會在下載的視窗中顯示 an ...