亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2754 | 回復: 8
打印 上一主題 下一主題

[shell] getopts的問題 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-01-04 20:45 |只看該作者 |倒序瀏覽
本帖最后由 xmbbx 于 2011-01-05 09:55 編輯

看代碼:

  1. #!/bin/bash

  2. function LOG
  3. {
  4.     while getopts 't:' OPT
  5.     do
  6.         case $OPT in
  7.             t)
  8.                 LABEL="$OPTARG";;
  9.             ?)
  10.                 echo "Usage: LOG [options] Message";;
  11.         esac
  12.     done
  13.     shift $(($OPTIND-1))
  14.     MSG=${1:-""}
  15.     echo "$(date +"%b %d %X") ${LABEL}: ${MSG}"
  16. }

  17. LOG -t DEBUG "test test1"
  18. LOG -t ERROR "test test2"
  19. LOG -t ERROR "test test3"
  20. LOG -t INFO "test test4"
復制代碼
運行的結果達不到預期:
[root@localhost root]# bash tt.sh
Jan 04 20:38:43 DEBUG: test test1
Jan 04 20:38:43 DEBUG: test test2
Jan 04 20:38:43 DEBUG: test test3
Jan 04 20:38:43 DEBUG: test test4

預期應該是這樣的:
[root@localhost root]# bash tt.sh
Jan 04 20:38:43 DEBUG: test test1
Jan 04 20:38:43 ERROR : test test2
Jan 04 20:38:43 ERROR: test test3
Jan 04 20:38:43 INFO: test test4

什么問題呢?

論壇徽章:
0
2 [報告]
發(fā)表于 2011-01-05 09:54 |只看該作者
這個很奇怪啊

論壇徽章:
145
技術圖書徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11獅子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龍
日期:2014-01-08 15:26:12技術圖書徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
3 [報告]
發(fā)表于 2011-01-05 10:30 |只看該作者
看代碼:運行的結果達不到預期:
[root@localhost root]# bash tt.sh
Jan 04 20:38:43 DEBUG: test test ...
xmbbx 發(fā)表于 2011-01-04 20:45


試試....
  1. #!/bin/bash

  2. function LOG
  3. {
  4.     local LABEL=$1
  5.     local MSG=$2
  6.     echo "$(date +"%b %d %X") ${LABEL}: ${MSG}"
  7. }

  8. LOG  DEBUG "test test1"
  9. LOG  ERROR "test test2"
  10. LOG  ERROR "test test3"
  11. LOG  INFO "test test4"
復制代碼

論壇徽章:
0
4 [報告]
發(fā)表于 2011-01-05 11:19 |只看該作者
回復 3# jason680


    呵呵,這個是一種辦法。
我就是想知道為啥用getopts不行。

論壇徽章:
0
5 [報告]
發(fā)表于 2011-01-05 11:58 |只看該作者
[root@localhost shell]# sh -x  test
+ LOG -t DEBUG 'test test1'
+ getopts t: OPT
+ case $OPT in
+ LABEL=DEBUG
+ getopts t: OPT
+ shift 2

+ MSG='test test1'
++ date '+%b %d %X'
+ echo ' 1月 05 11時55分41秒 DEBUG: test test1'
1月 05 11時55分41秒 DEBUG: test test1
+ LOG -t ERROR 'test test2'
+ getopts t: OPT
+ shift 2

+ MSG='test test2'
++ date '+%b %d %X'
+ echo ' 1月 05 11時55分41秒 DEBUG: test test2'
1月 05 11時55分41秒 DEBUG: test test2

論壇徽章:
0
6 [報告]
發(fā)表于 2011-01-05 15:21 |只看該作者
回復 5# jiaminqiang


    不是很明白,第二次調用時沒有認到-t? 要怎么改。

論壇徽章:
145
技術圖書徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11獅子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龍
日期:2014-01-08 15:26:12技術圖書徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
7 [報告]
發(fā)表于 2011-01-05 15:48 |只看該作者
回復  jason680


    呵呵,這個是一種辦法。
我就是想知道為啥用getopts不行。
xmbbx 發(fā)表于 2011-01-05 11:19



就我所知getopts用在command line
你把它用在function上,沒有這樣用的經驗.....
(你這樣用有問題,更證明不應該這樣用...)

個人認為:
command Line會有各式各樣的問題,而且下command的人不一定是你
   所以無法得知使用者要怎么調用你的腳本程式...
  但function是你自己是調用的...為什么要那么累.....
  code變多,也沒有比較好懂,多此一舉....

論壇徽章:
145
技術圖書徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11獅子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龍
日期:2014-01-08 15:26:12技術圖書徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
8 [報告]
發(fā)表于 2011-01-05 16:00 |只看該作者
本帖最后由 jason680 于 2011-01-05 16:05 編輯
回復  jiaminqiang


    不是很明白,第二次調用時沒有認到-t? 要怎么改。
xmbbx 發(fā)表于 2011-01-05 15:21


找到了....
getopts的變數有 OPTIND與OPTARG
你要把OPTIND設回1才可以

function LOG
{
    OPTIND=1
    while getopts 't:' OPT
    do
       ...

# bash LOG.sh
Jan 05 03:55:03 PM DEBUG: test test1
Jan 05 03:55:03 PM ERROR: test test2
Jan 05 03:55:03 PM ERROR: test test3
Jan 05 03:55:03 PM INFO: test test4

論壇徽章:
0
9 [報告]
發(fā)表于 2011-01-05 16:13 |只看該作者
回復 8# jason680


    非常感謝,解決了我郁悶1天的疑惑。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯網協(xié)會會員  聯系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP