Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: sudo user-command-1; also-sudoed-command-2
- X-seq: zsh-users 21573
- From: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: sudo user-command-1; also-sudoed-command-2
- Date: Tue, 17 May 2016 18:31:39 +0300
- Authentication-results: mxback10m.mail.yandex.net; dkim=pass header.i=@yandex.ru
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1463499099; bh=ZCpG6fBeo0RRvmlc5BF0PXGspnJH0rlm7AbanrfBmOI=; h=X-Yandex-Sender-Uid:X-Yandex-Hint:X-Yandex-Hint:From:To: In-Reply-To:References:Subject:MIME-Version:Message-Id:X-Mailer: Date:Content-Transfer-Encoding:Content-Type; b=CXQvk80EXbBL5PfxjKozib6lD6RfgphI0NwWM75KcLhL0kS1GbBTjU3b1KXKFy88Q iUYzboPCm/yLoZIDdOGAhIkm5XIuFA1nlHDOzR7RRqNp2TeVT90Xn/liSYbACqQe6T 1d3Y3izJUHJj1yXN3cmu+FAn64ZfFFqi20t3KHlU=
- In-reply-to: <CAH+w=7apZPY41mR_=pdZ134aUUT8jaCuKnP_vScVAiER-Pc9Cg@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <86inydgbp6.fsf@student.uu.se> <CAH+w=7apZPY41mR_=pdZ134aUUT8jaCuKnP_vScVAiER-Pc9Cg@mail.gmail.com>
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