Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: sudo user-command-1; also-sudoed-command-2




17.05.2016, 13:44, "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxx>:
> On Mon, May 16, 2016 at 10:22 AM, Emanuel Berg <embe8573@xxxxxxxxxxxxx> wrote:
>>  How can I execute a block of arbitrary commands
>>  with sudo, but only having to type sudo once,
>>  and still be able to use the user functions?
>
> Why not
>
>   sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -c "...."
>
> ?? That should cause zsh to read the current user's .zshrc etc. files
> before executing the "...", thereby obtaining the alias and function
> definitions.

Why should it read zshrc without -i? If I am not mistaking using `sudo zsh -ic "…"` should be enough (assuming sudo keeps both $HOME and $ZDOTDIR, I do not remember this).

---

I would not recommend using $=BUFFER in your case: this is going to break in many cases (i.e. if inside quotes there is any sequence of whitespace characters, except a sequence containing exactly one space this will break). The easiest way to fix this is

```zsh
accept-line () {
    emulate -L zsh
    local words
    words=( ${(z)BUFFER} )
    case $words[1] in
        (zudo) BUFFER="zudo-f ${(q-)words[2,-1]}" ;;
    esac
    zle .accept-line
}
```

(changed `$=` to `$(z)`). This will not run cycles though. You may change zudo-f to

```zsh
zudo-f () {
    emulate -L zsh
    sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -ic "$1"
}
```

, but this has different downside: if alias or function was defined in user configuration everything may be fine. But if it was defined in the interactive session, it will not be used. Also this is going to be slower then your variant.



Messages sorted by: Reverse Date, Date, Thread, Author