当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 3 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 一个比较奇怪的脚本,看不大懂,谁能帮忙指导下
帖子发表于 : 2016-08-12 14:26 
头像

注册: 2012-09-24 14:08
帖子: 32
系统: ubuntu 12.10
送出感谢: 4
接收感谢: 0 次
代码:
#!/bin/bash
SUDO_BINARY="/usr/bin/sudo"
 
if [[ $# -eq 0 ]] ; then
  echo 'usage: ambari-sudo.sh [sudo_arg1, sudo_arg2 ...] command [arg1, arg2 ...]'
  exit 1
fi
 
# if user is non-root
if [ "$EUID" -ne 0 ] ; then
  $SUDO_BINARY "$@"
else
  ENV=()
  SUDO_ARGS=()
 
  for i ; do
    if [[ "$i" == *"="* ]] ; then
      ENV+=("$i")
      shift
    elif [[ "$i" == "-"* ]] ; then
      SUDO_ARGS+=("$i")
      shift
    else
      break
    fi
  done
   
  #echo "sudo arguments: ${SUDO_ARGS[@]}"
  #echo "env: ${ENV[@]}"
  #echo "args: $@"
 
  if [ "$ENV" ] ; then
    export "${ENV[@]}"
  fi
 
  "$@"
fi


这是我在折腾ambari时,看到的一个脚本,里面的for i ; do,中的i是什么呢,能帮忙说下吗,for循环一般不是for i in xxx的么.这个要如何理解啊.


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 一个比较奇怪的脚本,看不大懂,谁能帮忙指导下
帖子发表于 : 2016-08-12 14:46 
头像

注册: 2008-09-18 13:11
帖子: 2781
送出感谢: 1
接收感谢: 453
遇到没见过的语法查查manpage嘛
代码:
       for name [ [ in [ word ... ] ] ; ] do list ; done
              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 is executed each time.  If the in word  is  omit‐
              ted,  the  for  command  executes  list once for each positional
              parameter that is set (see PARAMETERS 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 list, no
              commands are executed, and the return status is 0.

可以看作是 for i in "$@"的简写


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 一个比较奇怪的脚本,看不大懂,谁能帮忙指导下
帖子发表于 : 2016-08-15 14:45 
头像

注册: 2012-09-24 14:08
帖子: 32
系统: ubuntu 12.10
送出感谢: 4
接收感谢: 0 次
astolia 写道:
遇到没见过的语法查查manpage嘛
代码:
       for name [ [ in [ word ... ] ] ; ] do list ; done
              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 is executed each time.  If the in word  is  omit‐
              ted,  the  for  command  executes  list once for each positional
              parameter that is set (see PARAMETERS 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 list, no
              commands are executed, and the return status is 0.

可以看作是 for i in "$@"的简写



多谢了哈,才知道这个原来也可以man


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 3 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:Bing [Bot] 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译