一个比较奇怪的脚本,看不大懂,谁能帮忙指导下

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
zhanju7hao
帖子: 32
注册时间: 2012-09-24 14:08
系统: ubuntu 12.10

一个比较奇怪的脚本,看不大懂,谁能帮忙指导下

#1

帖子 zhanju7hao » 2016-08-12 14:26

代码: 全选

#!/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的么.这个要如何理解啊.
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

Re: 一个比较奇怪的脚本,看不大懂,谁能帮忙指导下

#2

帖子 astolia » 2016-08-12 14:46

遇到没见过的语法查查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 "$@"的简写
头像
zhanju7hao
帖子: 32
注册时间: 2012-09-24 14:08
系统: ubuntu 12.10

Re: 一个比较奇怪的脚本,看不大懂,谁能帮忙指导下

#3

帖子 zhanju7hao » 2016-08-15 14:45

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
回复