zsh-users
 help / color / mirror / code / Atom feed
* sudo user-command-1; also-sudoed-command-2
@ 2016-05-16 17:22 Emanuel Berg
  2016-05-17 10:43 ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2016-05-16 17:22 UTC (permalink / raw)
  To: zsh-users

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?
I.e., because of the last demand, this:

    $ sudo zsh -c "command-1; ...; command-n"

won't do if command-1 is a user function
or alias!

Instead, this, as a user which has an "ll" ls
alias, and no permissions at /

    $ zudo ll /; touch /test

should be the equivalent of

    $ sudo ll /; sudo touch /test

which should be executed.

Here is the solution so far:

# [1]
accept-line () {
    local words
    words=( $=BUFFER )
    case $words[1] in
        (zudo) BUFFER="zudo-f ${(q-)words[2,-1]}" ;;
    esac
    zle .accept-line
}
zle -N accept-line

# [2]
zudo-f () {
    local funs_str=$1

    local -a funs
    funs=("${(@s/;/)funs_str}")

    for f in $funs; do
        eval "sudo $f"
    done
}
alias sudo='sudo '

# [3]
alias ll="ls -lh"

I'm grateful for any improvements to be made :)


[1] http://user.it.uu.se/~embe8573/conf/.zsh/todo
[2] http://user.it.uu.se/~embe8573/conf/.zshrc
[3] http://user.it.uu.se/~embe8573/conf/.zsh/ls

--
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 30 Blogomatic articles -


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-16 17:22 sudo user-command-1; also-sudoed-command-2 Emanuel Berg
@ 2016-05-17 10:43 ` Bart Schaefer
  2016-05-17 15:31   ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-05-17 19:51   ` Emanuel Berg
  0 siblings, 2 replies; 9+ messages in thread
From: Bart Schaefer @ 2016-05-17 10:43 UTC (permalink / raw)
  To: Zsh Users

On Mon, May 16, 2016 at 10:22 AM, Emanuel Berg <embe8573@student.uu.se> 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.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 10:43 ` Bart Schaefer
@ 2016-05-17 15:31   ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-05-17 19:58     ` Emanuel Berg
  2016-05-17 19:51   ` Emanuel Berg
  1 sibling, 1 reply; 9+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-05-17 15:31 UTC (permalink / raw)
  To: Bart Schaefer, Zsh Users



17.05.2016, 13:44, "Bart Schaefer" <schaefer@brasslantern.com>:
> On Mon, May 16, 2016 at 10:22 AM, Emanuel Berg <embe8573@student.uu.se> 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.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 10:43 ` Bart Schaefer
  2016-05-17 15:31   ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-05-17 19:51   ` Emanuel Berg
  2016-05-17 19:54     ` Nikolay Aleksandrovich Pavlov (ZyX)
  1 sibling, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2016-05-17 19:51 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer <schaefer@brasslantern.com>
writes:

>> 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
> "...."
>
> ??

You mean:

    zzudo () { sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -c $1}

and then provide a string as input, e.g.

    $ zzudo ll /

?

I don't get that to work with 'll', the user
ls alias: "command not found: ll"

I don't have ZDOTDIR set by the way.

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 32 Blogomatic articles -                   


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 19:51   ` Emanuel Berg
@ 2016-05-17 19:54     ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-05-17 20:10       ` Emanuel Berg
  0 siblings, 1 reply; 9+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-05-17 19:54 UTC (permalink / raw)
  To: Emanuel Berg, zsh-users

17.05.2016, 22:52, "Emanuel Berg" <embe8573@student.uu.se>:
> Bart Schaefer <schaefer@brasslantern.com>
> writes:
>
>>>  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
>>  "...."
>>
>>  ??
>
> You mean:
>
>     zzudo () { sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -c $1}
>
> and then provide a string as input, e.g.
>
>     $ zzudo ll /
>
> ?
>
> I don't get that to work with 'll', the user
> ls alias: "command not found: ll"

Check my reply, @Bart Schaefer missed `-i`, without this .zshrc is not read.

>
> I don't have ZDOTDIR set by the way.
>
> --
> underground experts united .... http://user.it.uu.se/~embe8573
> Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
>                    - so far: 32 Blogomatic articles -


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 15:31   ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-05-17 19:58     ` Emanuel Berg
  2016-05-17 20:04       ` Nikolay Aleksandrovich Pavlov (ZyX)
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2016-05-17 19:58 UTC (permalink / raw)
  To: zsh-users

"Nikolay Aleksandrovich Pavlov (ZyX)"
<kp-pav@yandex.ru> writes:

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

My neither, but anyway it doesn't work, at
least not here:

    $ sudo zsh -ic 'll /'

gets:

    zsh:1: command not found: ll

> (changed `$=` to `$(z)`). This will not run
> cycles though.

OK, done.

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

None of that is a problem to me, so let's see -
like this?

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

If so, no:

    zzzudo:emulate:1: unknown argument sudo

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 32 Blogomatic articles -                   


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 19:58     ` Emanuel Berg
@ 2016-05-17 20:04       ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-05-18  1:31         ` Emanuel Berg
  0 siblings, 1 reply; 9+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-05-17 20:04 UTC (permalink / raw)
  To: Emanuel Berg, zsh-users

17.05.2016, 22:59, "Emanuel Berg" <embe8573@student.uu.se>:
> "Nikolay Aleksandrovich Pavlov (ZyX)"
> <kp-pav@yandex.ru> writes:
>
>>  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).
>
> My neither, but anyway it doesn't work, at
> least not here:
>
>     $ sudo zsh -ic 'll /'
>
> gets:
>
>     zsh:1: command not found: ll

Where is `ll` alias defined?

>
>>  (changed `$=` to `$(z)`). This will not run
>>  cycles though.
>
> OK, done.
>
>>  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.
>
> None of that is a problem to me, so let's see -
> like this?
>
>     zzzudo () {
>         emulate -L zsh sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -ic "$1"
>     }
>
> If so, no:
>
>     zzzudo:emulate:1: unknown argument sudo

That was <blockquote>```zsh{newline}zudo-f () {{newline}    emulate -L zsh{newline}    sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -ic "$1"{newline}}</blockquote>. I do not know who spoiled the message, but I receive all my messages back from mailing list and it was fine, so it is something between you and mailing list.

>
> --
> underground experts united .... http://user.it.uu.se/~embe8573
> Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
>                    - so far: 32 Blogomatic articles -


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 19:54     ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-05-17 20:10       ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2016-05-17 20:10 UTC (permalink / raw)
  To: zsh-users

"Nikolay Aleksandrovich Pavlov (ZyX)"
<kp-pav@yandex.ru> writes:

> Check my reply, @Bart Schaefer missed `-i`,
> without this .zshrc is not read.

Same thing tho - 'll' not found.

I also get a new error now, from my .zshrc
file:

    for f in ~/.zsh/*; do

It substitutes the tilde for /root

Aha, those are related, of course! 'll' is not
found, as it isn't loaded!

Be right back...

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 32 Blogomatic articles -                   


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: sudo user-command-1; also-sudoed-command-2
  2016-05-17 20:04       ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-05-18  1:31         ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2016-05-18  1:31 UTC (permalink / raw)
  To: zsh-users

"Nikolay Aleksandrovich Pavlov (ZyX)"
<kp-pav@yandex.ru> writes:

> Where is `ll` alias defined?

OK, now I see where all this is heading!

The reason you said my solution is faster is
because with this solution, the user stuff is
loaded each time!

I have as of today 44 zsh files and even tho
the extra time for loading them can be
tolerated as time considered, it opens the door
for all kinds of problems associated with the
this process, which will be repeated perhaps in
the middle of work *changing* that process
(i.e., the zsh user files).

So no, this idea I don't like.

I just want

    $ zudo a; b; c

to be syntax sugar for

    $ sudo a; sudo b; sudo c

But to answer your question:

The alias is defined at line 21 in

    http://user.it.uu.se/~embe8573/conf/.zsh/ls

which is loaded at line 140 in

    http://user.it.uu.se/~embe8573/conf/.zshrc

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 32 Blogomatic articles -                   


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-05-18  1:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-16 17:22 sudo user-command-1; also-sudoed-command-2 Emanuel Berg
2016-05-17 10:43 ` Bart Schaefer
2016-05-17 15:31   ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-05-17 19:58     ` Emanuel Berg
2016-05-17 20:04       ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-05-18  1:31         ` Emanuel Berg
2016-05-17 19:51   ` Emanuel Berg
2016-05-17 19:54     ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-05-17 20:10       ` Emanuel Berg

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).