回文章列表
作者PHP5.bbs@ptt.cc (Powered by Zend Engine2) 看板: linux
標題[問題] shell script -- getopt 問題
時間批踢踢實業 (2008/08/09 Sat 20:09:54)
getopt要如何正確處理含有空白的arg?
以下是小弟的script

test.sh
#!/bin/bash

args=`getopt a:b: $*`
if [ $? -ne 0 ]; then
  echo error
  exit 1
fi

set -- $args

while true
do
  case "$1" in
    -a)
      a="$2"
      shift 2
      ;;
    -b)
      b="$2"
      shift 2
      ;;
    --)
      shift
      break
      ;;
    *)
      echo error
      exit 2
      ;;
  esac
done


echo $a
echo $b


執行結果
# ./test2.sh -a AAA -b BBB
AAA
BBB
正常


但是
# ./test2.sh -a "AAA AAA" -b "BBB BBB"
並不會輸出我想要的結果
AAA AAA
BBB BBB


不知要怎麼寫才能正常處理空白



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