1

I got this simple script:

#!/bin/bash
SOURCE_USER=$USER
DESTINE_USER=$1

id -u $SOURCE_USER > /dev/null 2>&1

if [ "$?" == "1" ] || [ -z $SOURCE_USER ]
then
    printf "Error: Invalid source user '$SOURCE_USER'\\n"
    exit 1
fi

if [ -z $DESTINE_USER ]
then
    printf "Error: Invalid destine user '$DESTINE_USER'\\n"
    exit 1
fi

SOURCE_GROUPS=$(id -Gn ${SOURCE_USER} | sed "s/${SOURCE_USER} //g" | sed "s/ ${SOURCE_USER}//g" | sed "s/ /,/g")
SOURCE_SHELL=$(awk -F : -v name=${SOURCE_USER} '(name == $1) { print $7 }' /etc/passwd)

id -u $DESTINE_USER > /dev/null 2>&1

if [ "$?" == "1" ]
then
    printf "Creating destine user %s\\n" "$DESTINE_USER"
    useradd --groups ${SOURCE_GROUPS} --shell ${SOURCE_SHELL} --create-home ${DESTINE_USER}
    passwd ${DESTINE_USER}
    xhost '+si:localuser:$DESTINE_USER'
    sudo usermod -G "$SOURCE_USER" "$DESTINE_USER"
else
    printf "Updating destine user '%s' with groups '%s' and shell '%s'\\n" "$DESTINE_USER" "$SOURCE_GROUPS" "$SOURCE_SHELL"
    sudo usermod -a -G "$SOURCE_GROUPS" "$DESTINE_USER"
    sudo chsh -s "$SOURCE_SHELL" "$SOURCE_USER"
fi

sudo runuser sublime_vanilla -c "${@:2}"

I run it like this:

$ bash run_as_user.sh sublime_vanilla /usr/bin/subl -n "./New Empty File"

But when I run it, I got this error:

runuser: invalid option -- 'n'
Try 'runuser --help' for more information.

But if I replace sudo runuser sublime_vanilla -c "${@:2}" with sudo runuser sublime_vanilla -c "\"$2\" \"$3\" \"$4\" \"$5\" \"$6\" \"$7\" \"$8\" \"${@:9}\""

Then, Sublime Text correctly opens the file "./New Empty File" in a new window.

How to make runuser correctly understand all argument with a variable number of command line arguments, i.e., without hard coding "\"$2\" \"$3\" \"$4\" ..."?

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

2

This is slightly different from your last question because you have to make the expansion of the arguments into a single string for the -c option.

The bash printf formatter %q is your friend here:

cmd=$( printf '%q ' "${@:2}" )
sudo runuser sublime_vanilla -c "$cmd"

On the other hand, a quick perusal through the runuser man page suggests:

sudo runuser -u sublime_vanilla "${@:2}"

Another thought: sudo runuser -u sublime_vanilla -- "${@:2}" with the double hyphens to indicate the end of the runuser options.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I also tried your second suggestion `sudo runuser -u sublime_vanilla "${@:2}`, but it gives the same error as my original question: `runuser: invalid option -- 'n'`. Hence, using your `printf` trick worked `cmd=$( printf '%q ' "${@:2}" )` – Evandro Coan Mar 06 '19 at 00:05
  • Another thought: `sudo runuser -u sublime_vanilla -- "${@:2}"` with the double hyphens to indicate the end of the runuser options. – glenn jackman Mar 06 '19 at 00:54
  • This last one `sudo runuser -u sublime_vanilla -- "${@:2}"` worked too! – Evandro Coan Mar 06 '19 at 00:57
  • That's good. I think that should be your preferred option as you don't have to play tricks stringifying an array. – glenn jackman Mar 06 '19 at 00:58
  • Ah, but `stringifying an array` is much way cooler and using `--`! – Evandro Coan Mar 06 '19 at 01:00