I'd like to be able to write a function su_mt_user
that (currently) looks like this:
su_mt_user() {
su someuser -c "$*"
}
The objective is to be able to use it like this:
su_mt_user mt-auth --stuff etc
Which would run the command mt-auth --stuff etc
as user someuser
. The current version works for this particular command, but it fails for commands like this:
some_filename_with_spaces="/home/user/hello there i like spaces in filenames.txt"
su_mt_user stat "$some_filename_with_spaces"
This fails with the following errors:
stat: cannot stat '/home/user/hello': No such file or directory
stat: cannot stat 'there': No such file or directory
stat: cannot stat 'i': No such file or directory
stat: cannot stat 'like': No such file or directory
stat: cannot stat 'spaces': No such file or directory
stat: cannot stat 'in': No such file or directory
stat: cannot stat 'filenames.txt': No such file or directory
I assume that this error happens because even though $some_filename_with_spaces
is properly passed as one argument to the su_mt_user
function, that function expands it to multiple arguments with "$*"
.
I've also tried this, trial-and-error:
su_mt_user() {
su someuser -c "$0 $@"
}
But that fails as well (/usr/bin/stat: cannot execute binary file
(what?))
Of course, stat "$some_filename_with_spaces"
works as expected, from both the current user and the someuser
user.
This looks like some escaping needs to be done, but does bash know how to do that? Does it need manual subtitution? If so, which characters need to be escaped?
--
to preventsu
from parsing option arguments as well:su someuser -c '"$0" "$@"' -- "$@"
; otherwise,su
attempts to interpret option arguments like-a
as arguments to itself, causing an error. – Row