Emulating sudo behavior with su

I am trying to write a wrapper around suto make it more like sudo, so su_wrapper foo bar baz== su -c "foo bar baz".

However, I am not sure how to approach this problem. I came up with this:

su_wrapper ()
{
  su -c "$@"
}

However, in the above example, there can be only one argument; this fails with a few arguments (because it sutreats them as its own arguments).

There is another problem: since the argument is passed through the shell, I think I should explicitly specify the shell in order to avoid other problems. Perhaps what I want to do can be expressed in pseudo-w50> (!) Like su -c 'bash -c "$@"'.

So how can I make it accept multiple arguments?

+4
2

printf "%q", , :

su_wrapper() {
    su -s /bin/bash -c "$(printf "%q " "$@")"
}

$*, , .

+2

$*, $@:

su_wrapper() {
    local IFS=' '
    su -c "$*"
}

. Bash $* $@.

local IFS=' ' , IFS - ( , $*, , , IFS ).

+2

Source: https://habr.com/ru/post/1545467/


All Articles