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

时间:2010-08-02 06:03:11

标签: curl bash

我正在尝试编写一个简单的bash脚本,它接受所有参数并将它们简单地解释为字符串。

换句话说: 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 个答案:

答案 0 :(得分:2)

$ 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.

答案 1 :(得分:1)

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|"
}

urlshift在哪里,以防您需要类似的内容。显然,关键是“$ *”,这与“$ @”特殊的方式不同。 set --重置位置参数。

为了个人方便,这很好,但可能会让那些期望正常shell行为的人感到困惑。

答案 2 :(得分:1)

为什么不使用引用 -

通过以下方式调用您的函数:tweet "bla bla bla"

而不是$ @使用“$ {1}”来表示整个字符串作为第一个参数。

答案 3 :(得分:0)

使用“$ *”代替“$ @”。从bash手册页:

       *      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).