代码之家  ›  专栏  ›  技术社区  ›  Kzqai

bash脚本中的一个小问题,使函数在初始函数调用后接受所有参数作为单个字符串

  •  2
  • Kzqai  · 技术社区  · 15 年前

    tweet Testing out my command-line tweetings. #commandlinetweets

    接受所有参数,直接将它们用作字符串。

    这是我目前的基本形式:

    function tweet()
    {
    echo "Tweeting using curl."
    curl -u tchalvak "http://twitter.com/statuses/update.xml" -d status="$@"
    echo "Tweet done if spammed info back above."
    echo "Tweeted using username tchalvak with status $@"
    }
    

    $ tweet TEst again and again and again.
    Tweeting using curl.
    Enter host password for user 'tchalvak':
    <?xml version="1.0" encoding="UTF-8"?>
    <hash>
      <request>/statuses/update.xml</request>
      <error>Status is a duplicate.</error>
    </hash>
    curl: (6) Couldn't resolve host 'again'
    curl: (6) Couldn't resolve host 'and'
    curl: (6) Couldn't resolve host 'again'
    curl: (6) Couldn't resolve host 'and'
    curl: (6) Couldn't resolve host 'again.'
    Tweet done if spammed info back above.
    Tweeted using username tchalvak with status TEst again and again and again.
    

    那么,我如何从多个参数中得到“所有这些技术上的多个参数都应该算作函数中的一个字符串变量”?

    :对不起,我应该澄清一下,我想我只是硬编码的用户名,最大限度的防弹推文语法。

    #

    function tweet()
    {
    STATUS="$*"
    echo "Tweeting using curl."
    curl -u tchalvak "http://twitter.com/statuses/update.xml" -d status="$STATUS"
    echo "Tweet done if spammed info back above."
    echo "Tweeted using username tchalvak with status $STATUS"
    }
    
    4 回复  |  直到 15 年前
        1
  •  2
  •   TimeZlicer    15 年前

    $1将是用户名

    班次将跳过1美元
    使用$*来使用其余的参数

    USER=$1
    shift
    STATUS="$*"
    echo "Tweeting using curl."
    curl -u $USER "http://twitter.com/statuses/update.xml" -d status="$STATUS"
    echo "Tweet done if spammed info back above."
    echo "Tweeted using username $USER with status $STATUS"
    

    输出示例如下

    ./tweet tchalvak TEst again and again and again.
    Tweeting using curl.
    Enter host password for user 'tchalvak':
    Tweet done if spammed info back above.
    Tweeted using username tchalvak with status TEst again and again and again.
    
        2
  •  1
  •   smichak    15 年前

    为什么不引用-

    tweet "bla bla bla"

        3
  •  1
  •   msw    15 年前
    tweet "TEst again and again and again."
    

    您的调用正在传入6个独立的参数 "$@" 把它们正确地传递为6个。此命令执行有一个参数,将作为一个参数传递。

    针对评论添加 :

    bind_args() {
        $url = $1;
        shift;
        echo "url is $url with $# arguments"
        set -- "$*"
        echo "now there is only $# argument: |$1|"
    }
    

    url shift set -- 重置位置参数。

        4
  •  0
  •   William Pursell    15 年前

           *      Expands to the positional parameters, starting from one.  When the expansion
                  occurs  within  double quotes, it expands to a single word with the value of
                  each parameter separated by the first character of the IFS special variable.
                  That  is,  "$*" is equivalent to "$1c$2c...", where c is the first character
                  of the value of the IFS variable.  If IFS is unset, the parameters are sepa-
                  rated  by  spaces.  If IFS is null, the parameters are joined without inter-
                  vening separators.
           @      Expands to the positional parameters, starting from one.  When the expansion
                  occurs  within  double  quotes,  each  parameter expands to a separate word.
                  That is, "$@" is equivalent to "$1" "$2" ...  If the double-quoted expansion
                  occurs  within  a  word, the expansion of the first parameter is joined with
                  the beginning part of the original word,  and  the  expansion  of  the  last
                  parameter is joined with the last part of the original word.  When there are
                  no positional parameters, "$@" and $@ expand  to  nothing  (i.e.,  they  are
                  removed).
    
    
    推荐文章