免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1411 | 回复: 3

[系统管理] [bash] 怎么理解最后3行代码的用法呢? [复制链接]

论坛徽章:
0
发表于 2017-08-28 16:31 |显示全部楼层
  1. if [ $? -ne 0 ]
  2. then
  3.         echo 'Terminating...' >&2
  4.         exit 1
  5. fi

  6. # Note the quotes around "$TEMP": they are essential!
  7. eval set -- "$TEMP"
  8. unset TEMP

  9. while true
  10. do
  11.         case "$1" in
  12.                 '-a'|'--a-long')
  13.                         echo 'Option a'
  14.                         shift
  15.                         continue
  16.                 ;;
  17.                 '-b'|'--b-long')
  18.                         echo "Option b, argument '$2'"
  19.                         shift 2
  20.                         continue
  21.                 ;;
  22.                 '-c'|'--c-long')
  23.                         # c has an optional argument. As we are in quoted mode,
  24.                         # an empty parameter will be generated if its optional
  25.                         # argument is not found.
  26.                         case "$2" in
  27.                                 '')
  28.                                         echo 'Option c, no argument'
  29.                                 ;;
  30.                                 *)
  31.                                         echo "Option c, argument '$2'"
  32.                                 ;;
  33.                         esac
  34.                         shift 2
  35.                         continue
  36.                 ;;
  37.                 '--')
  38.                         shift
  39.                         break
  40.                 ;;
  41.                 *)
  42.                         echo 'Internal error!' >&2
  43.                         exit 1
  44.                 ;;
  45.         esac
  46. done

  47. echo 'Remaining arguments:'
  48. for arg; do
  49.         echo "--> '$arg'"
  50. done
复制代码


求职 : 数据库管理员
论坛徽章:
0
发表于 2017-08-28 16:43 |显示全部楼层
eval  set --  "TEMP"将TEMP设置为位置参数,这里的for循环就将位置参数依次读取到变量arg,然后将arg输出

论坛徽章:
8
CU十四周年纪念徽章
日期:2017-08-28 17:16:1915-16赛季CBA联赛之北控
日期:2018-03-29 11:39:0615-16赛季CBA联赛之上海
日期:2018-05-21 10:46:10技术图书徽章
日期:2018-09-03 13:54:18技术图书徽章
日期:2018-09-03 13:54:2815-16赛季CBA联赛之四川
日期:2020-10-15 09:21:4720周年集字徽章-20	
日期:2020-10-28 13:53:0220周年集字徽章-庆
日期:2020-10-28 14:09:12
发表于 2017-08-28 17:02 |显示全部楼层
本帖最后由 龙牙地主天 于 2017-08-28 17:15 编辑

for arg  

等同于

for arg in "$@"


  1.        for name [ [ in [ word ... ] ] ; ] do list ; done
  2.               The  list of words following in is expanded, generating a list of items.  The variable name is set to each element of this list in turn, and list
  3.               is executed each time.  If the in word is omitted, the for command executes list once for each positional parameter that is set  (see  PARAMETERS
  4.               below).   The return status is the exit status of the last command that executes.  If the expansion of the items following in results in an empty
  5.               list, no commands are executed, and the return status is 0.
复制代码

论坛徽章:
0
发表于 2017-08-28 18:03 |显示全部楼层
多谢大家!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP