#!/usr/bin/bash set -e # Let's set some defaults SU_USER="ubuntu" # Parse command line arguments while [ $# -gt 0 ]; do case "$1" in -u|--user) SU_USER="$2" shift 2 ;; *) break ;; esac done # Check if the user exists if ! id -u "$SU_USER" >/dev/null 2>&1; then useradd -m "$SU_USER" fi # Check if the home directory exists if [ ! -d "/home/$SU_USER" ]; then echo "A home directory for $SU_USER does not exist. Exiting." 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