3

我正在尝试制作一个 bash 函数,它将$_为我搜索终端的最后输出。例如,当我尝试某事时,我从终端收到错误,而不是在谷歌中复制和粘贴错误,我只需键入google that它就会为我搜索错误。

它还将支持打开谷歌主页和随机谷歌搜索。

function google() {
  if [ $1 == 'that' ]
  then
    open /Applications/Google\ Chrome.app/ "http://www.google.com/search?q= $_";
  elif [ $1 == '' ]
  then
    open /Applications/Google\ Chrome.app/ "http://www.google.com"
  else
    open /Applications/Google\ Chrome.app/ "http://www.google.com/search?q= $@";
  fi
}

当我输入时google that,我得到搜索结果[。我不明白为什么它不起作用?

我在 OSX 10 上并使用 Chrome。

4

4 回答 4

5
  1. 不要使用[ $foo == 'bar' ],使用[ "$foo" = 'bar' ][[ "$foo" == 'bar' ]]
  2. 不使用[ $1 == '' ],使用[ -z "$1" ]
  3. 正如 jm666 指出的那样,但也许应该更多地强调,$_不是终端上的最后一个输出,它是一个“[s] 特殊变量设置为上一个执行命令的最终参数”,所以你的脚本永远不会真正做你想做的事.

话虽如此,对您的代码段进行稍微干净的重写可能如下所示:

google()
{
    local s="$_"
    local query=

    case "$1" in
        '')   ;;
        that) query="search?q=${s//[[:space:]]/+}" ;;
        *)    s="$*"; query="search?q=${s//[[:space:]]/+}" ;;
    esac

    echo open /Applications/Google\ Chrome.app/ "http://www.google.com/${query}"
}

示例运行:

$ echo "foo bar quux"
foo bar quux
$ google that
open /Applications/Google Chrome.app/ http://www.google.com/search?q=foo+bar+quux

$ google
open /Applications/Google Chrome.app/ http://www.google.com/

$ google foo    bar    quux
open /Applications/Google Chrome.app/ http://www.google.com/search?q=foo+bar+quux
于 2013-05-11T11:47:57.077 回答
3

首先,平均而言(bash这里$_

the last argument of the last command

所以当你在脚本中使用它时它会改变。例如简单的功能

mytest() {
        if [ "$1" == 'that' ]
        then
                echo $_
        fi
}

将打印

]

什么是last argument of the last command(如果)

其次,直接将url添加到google中,作为base minimum,您需要将空格更改为+. 你不应该在你的脚本之间有空格=google's query你的脚本可能很简单:

google() {
        gq=$(sed 's/ /+/g' <<<"$*")
        open -a /Applications/Google\ Chrome.app "http://www.google.com/search?q=$gq";
}

sed行将空格更改为+, 所以命令

google some weird search

将更改为网址

http://www.google.com/search?q=some+weird+search

在完成source上述脚本之后,或者将其放入 ~/.profile 中,您可以将其用作:

google $_    #instead of the "that" is shorter anyway :)

last argument of the last command使用, 或简单的查询打开 Chrome

google

使用“空”搜索打开 chrome,或如上所述google some weird search

第三,如果你使用open something something_otheropen尝试打开这两个东西。所以如果你想打开 Chrome,wil 参数你应该使用-a applcationswitch。如果您的default浏览器是 Chrome 而不是 Safari,您可以使用简单的:

 open "http://www.google.com/search?q=$gq";
于 2013-05-11T07:17:24.533 回答
3

因为最后一个命令的最后一个参数是[.

在执行任何操作之前,您必须按顺序存储最后一个参数:

function google() {
  local lastarg=$_
  if [ $1 == 'that' ]; then
    open /Applications/Google\ Chrome.app/ "http://www.google.com/search?q= $lastarg"
  ...
于 2013-05-11T07:11:17.987 回答
1

对于我的方法,我改为使用 html2text(可通过许多东西获得,我使用 HomeBrew)、Curl 和 Grep 的组合。然后我在我的 BashProfile 中创建了以下函数。

goo() {
    ARGS=''
    if [ "x$1" != "x" ]; then
        for arg in $*
        do
            ARGS="$ARGS $arg"
        done
    else
        read -p "what do you want to search for: " ARGS
    fi
    ARGS=$(echo $ARGS | sed -e 's/\ /+/g')
    printf "\nsearching for the following term: $ARGS \n\n"
    curl -A Chrome http://www.google.com/search?q=$ARGS | html2text | grep ' 1\. \| 2\. \| 3\. ' -A 4 -B1 --color
}

当我在终端中时,我只需要输入“goo hello world”,这将在谷歌搜索中搜索 hello+world,返回前三名谷歌点击。

对于那些不熟悉 Grep 的人,-A 表示“找到匹配后的行”,-B 表示之前。--color 将突出显示输出中的“匹配”术语。

由于我似乎在终端上花费了很多时间,我发现这个命令非常有用。

于 2014-04-16T10:49:14.557 回答