回文章列表
作者BurningFlame.bbs@ptt.cc (焰) 看板: linux
標題Re: [問題] shell script -- getopt 問題
時間批踢踢實業 (2008/08/13 Wed 06:32:04)
總共有三個地方要做修正
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.

--
※ 發信站: 批踢踢實業坊(ptt.cc) 
◆ From: 210.64.152.36
文章選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻