First, you should avoid that lots of sudo
calls. Instead, you should run the script with sudo
. The final version would look like this:
#!/bin/bash # first check for root user if [ ! $UID -eq 0 ]; then echo "This script must be run as root." exit 1 fi # check if the user provided an argument if [ -z $1 ]; then echo "No username provided. Usage: $0 username" exit 2 fi username=$1 # set the first argument as the username mkdir "/store/sftp/$username" useradd -d /incoming "$username" passwd "$username" ## Password needs to be typed or passed in here usermod -g sftp "$username" usermod -s /bin/false "$username" chmod 755 "/store/sftp/$username" mkdir "/store/sftp/$username/incoming" chown "$username:sftp" "/store/sftp/$username/incoming"
Quoting is necessary because the user name may contain spaces (but sure, it usually doesn't).
Edited to work.