Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1281191
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类: LINUX

2013-09-25 10:57:34


  1. #!/bin/bash
  2. # Author: jiyin@redhat.com

  3. TEMP=`getopt -o vt: --long target -n 'example.bash' -- "$@"`
  4. if [ $? != 0 ] ; then echo "getopt fail, terminating..." >&2 ; exit 1 ; fi

  5. eval set -- "$TEMP"
  6. IDNT=" "

  7. Usage() {
  8.     echo "usage: $0 -t [ { | ... struct:E>} ... ]"
  9.     echo " eg1: $0 -t search_book string:linux int:2009"
  10.     echo " eg2: $0 -t search_dock string:dock \"struct:B mem1:int:8 mem2:int:32 struct:E\""
  11.     exit 1
  12. }
  13. simpleArg() {
  14.     local arg="$1"
  15.     local type=${arg%%:*} val=${arg#*:}
  16.     local indent=$indent

  17.     echo -e "${indent}<$type>$val"
  18. }
  19. structMember() {
  20.     local arg="$1"
  21.     local name=${arg%%:*} val=${arg#*:}
  22.     local type=${val%%:*} val=${val#*:}
  23.     local indent=$indent

  24.     echo -e "${indent}"
  25.     echo -e "${indent}${IDNT}$name"
  26.     echo -e "${indent}${IDNT}<$type>$val"
  27.     echo -e "${indent}"
  28. }
  29. structArg() {
  30.     local args="$1"
  31.     local deep=0

  32.     local cindent="${indent}"
  33.     local nindent="${cindent}${IDNT}"
  34.     echo -e "${cindent}\n${cindent}${IDNT}"
  35.     eval set -- $args
  36.     for _arg; do
  37.         shift
  38.         case $_arg in
  39.         struct:B*)
  40.             ((deep > 0)) && echo -e "${nindent}"
  41.             local name=${_arg#struct:B}
  42.             [ -n "$name" ] && echo -e "${nindent}${IDNT}${name#:}"
  43.             echo -e "${nindent}${IDNT}"
  44.             indent="${nindent}${IDNT}${IDNT}"
  45.             nindent="${nindent}${IDNT}${IDNT}"
  46.             ((deep++))
  47.             ;;
  48.         struct:E)
  49.             indent=${indent%$IDNT$IDNT}
  50.             nindent=${nindent%$IDNT$IDNT}
  51.             echo -e "${nindent}${IDNT}"
  52.             ((--deep > 0)) && echo -e "${nindent}"
  53.             ;;
  54.         *)
  55.             structMember "$_arg"
  56.             ;;
  57.         esac
  58.     done
  59.     echo -e "${cindent}${IDNT}\n${cindent}"
  60. }

  61. while true ; do
  62.     case "$1" in
  63.     -t|--target) servUrl=$2; shift 2;;
  64.     -v) verbose=--verbose; shift;;
  65.     --) shift; break;;
  66.     *) echo "Internal error!"; exit 1;;
  67.     esac
  68. done
  69. [ -z "$servUrl" ] && Usage
  70. [ $# -lt 1 ] && Usage

  71. #<method> <arg1> <arg2> ... <argx>
  72. generateRequestXml() {
  73.     method=$1; shift
  74.     echo ''
  75.     echo ""
  76.     echo " $method"
  77.     echo " "

  78.     indent="${IDNT}${IDNT}"
  79.     for arg; do
  80.         case $arg in
  81.         struct:B*) structArg "$arg";;
  82.         *) simpleArg "$arg";;
  83.         esac
  84.     done

  85.     echo " "
  86.     echo ""
  87. }

  88. rpcxml=rpc$$.xml
  89. generateRequestXml "$@" > $rpcxml
  90. [ "$verbose" = "--verbose" ] && cat "$rpcxml"
  91. curl $verbose --data "@$rpcxml" "$servUrl" 2>/dev/null | xmllint --format -
  92. \rm -f $rpcxml

  93. # format
  94. true <<COMM
  95.         <param>
  96.             <value>
  97.                 <struct>
  98.                     <member>
  99.                         <name>Channel</name>
  100.                         <value><string>1ee246fc-e60d-2fd3-6242- 5d19850f</string></value>
  101.                     </member>
  102.                     <member>
  103.                         <name>IntValue</name>
  104.                         <value><int>42</int></value>
  105.                     </member>
  106.                     <member>
  107.                         <name>StringValue</name>
  108.                         <value><string>Hello, world!></value>
  109.                     </member>
  110.                 </struct>
  111.             </value>
  112.         </param>
  113. COMM


阅读(1459) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~