I am trying to create a script that looks just like /bin/bash, but it is actually a shell script that executes bash internally as a different user.

Consider the following user-bash.sh file.

#!/usr/bin/env bash
set -e

USER_ID="$USER_ID"

if [ -z "$USER_ID" ]; then
    echo "Set USER_ID to 1000"
    USER_ID="1000"
fi

# Create the dummy user, if it doesn't exist.
id dummy > /dev/null 2>&1 || { 
    echo "Creating dummy user..."
    useradd -u ${USER_ID} dummy
    mkdir -p /home/dummy/.ssh
}

# Doesn't work, but I think it illustrates what I'm trying to do.
su dummy -c "/bin/bash"

I'd like for user-bash.sh to behave exactly like /bin/bash, including support for stdin/stdout, args and exit code.

How would I do this?

Some background, I'm trying to create an entry point script for a Docker image that always executes as another user. The default entry point for most images is /bin/bash. I'd like to make it /bin/user-bash.sh.

0 Answers