5

sudo !!我正在尝试编写一个与 Bash等效的函数。它有效,但仅当最后一个命令没有参数时。

到目前为止,功能是:

function s --description "Run last command (or specified command) using sudo"
    if test $argv
        switch $argv[1]
            case '!!'
                command sudo (echo $history[1])
            case '*'
                command sudo $argv
        end
    else
        command sudo fish
    end
end

测试相关线路:

$ command sudo whoami
root
$ whoami
nick
$ command sudo (echo $history[1])
root

到目前为止一切顺利,现在让我们尝试一个带有一些参数的命令:

$ echo hi >> /etc/motd
An error occurred while redirecting file '/etc/motd'
open: Permission denied
$ command sudo (echo $history[1])
sudo: echo hi >> /etc/motd: command not found

嗯,奇怪。

4

2 回答 2

5

使用 eval 让它工作。

function sudo --description 'Run command using sudo (use !! for last command)'
      if test (count $argv) -gt 0
          switch $argv[1]
              case '!!'
                  if test (count $argv) -gt 1
                      set cmd "command sudo $history[1] $argv[2..-1]"
                  else
                      set cmd "command sudo $history[1]"
                  end
              case '*'
                  set cmd "command sudo $argv"
          end
      else
          set cmd "command sudo fish"
      end
      eval $cmd
  end
于 2014-03-26T03:28:45.570 回答
4

我遇到了和你一样的问题,我通过使用(它是shelloh-my-fish 的插件管理器) https://github.com/oh-my-fish/oh-my-fish修复了它。您可以使用以下命令安装它:fish

curl -L https://get.oh-my.fish | fish

然后使用以下命令安装插件bang-bang(允许!!!$):

omf install bang-bang 
于 2017-05-16T14:34:40.883 回答