Fixed issues with setting up colored Bash prompt supporting displaying Git repository information.
All checks were successful
Build Docker Image / build (push) Successful in 38s

This commit is contained in:
2025-12-21 13:50:07 +00:00
parent b908f3f1f0
commit e6b4fdec1a
5 changed files with 65 additions and 53 deletions

View File

@@ -2,42 +2,55 @@
set -e
# Let's set some defaults
SU_USER="ubuntu"
# Setup default values
USER_NAME="ubuntu"
# Parse command line arguments
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-u|--user)
SU_USER="$2"
--user|-u)
USER_NAME="$2"
shift 2
;;
--)
# Stop parsing arguments
shift
break
;;
*)
break
;;
esac
done
# Check if the user exists
if ! id -u "$SU_USER" >/dev/null 2>&1; then
useradd -m -s /usr/bin/bash "$SU_USER"
# Check, if we are running as root
if [ "$(id -u)" -eq 0 ]; then
# Check, if the home directory exists for the specified user
if [ ! -d "/home/${USER_NAME}" ]; then
echo "Error: Home directory for user '${USER_NAME}' does not exist." >&2
exit 1
fi
# Check, ownership of the home directory
OWNER_UID=$(stat -c '%u' "/home/${USER_NAME}")
OWNER_GID=$(stat -c '%g' "/home/${USER_NAME}")
if [ "${OWNER_UID}" -ne 0 ] || [ "${OWNER_GID}" -ne 0 ]; then
# The home directory is not owned by the specfied user, correct it
chown "${USER_NAME}:${USER_NAME}" "/home/${USER_NAME}"
fi
# Re-initialize the contents of the home directory
su - "${USER_NAME}" -c "cp -a /etc/skel/. /home/${USER_NAME}/"
# We are done as root, quit. The container will be re-run as the specified user.
exit 0
fi
# Check if the home directory exists
if [ ! -d "/home/$SU_USER" ]; then
echo "A home directory for $SU_USER does not exist. Exiting."
# Verify that we are running as the user owning the home directory
if [ "$(id -un)" != "${USER_NAME}" ]; then
echo "Error: The script must be run as user '${USER_NAME}'." >&2
exit 1
fi
# Check user ownership of the home directory
if [ "$(stat -c '%u' /home/$SU_USER)" -ne "$(id -u $SU_USER)" ]; then
# Change ownership of the home directory
chown $SU_USER:$SU_USER /home/$SU_USER
fi
# Switch to the specified user
if [ $# -gt 0 ]; then
exec su $SU_USER -c "$@"
else
exec su $SU_USER
fi
# Run shell
exec /usr/bin/bash "$@"