Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would I go about converting this bash script:

mkdir /store/sftp/%USERNAME%

sudo useradd -d /incoming %USERNAME%
sudo passwd %USERNAME%
## Password needs to be typed or passed in here
sudo usermod -g sftp %USERNAME%
sudo usermod -s /bin/false %USERNAME%

sudo chmod 755 /store/sftp/%USERNAME%
sudo chown root:root /store/sftp/%USERNAME%
sudo mkdir /store/sftp/%USERNAME%/incoming
sudo chown %USERNAME%:sftp /store/sftp/%USERNAME%/incoming

To accept a username and a password?

share|improve this question
Please be aware that command line arguments can generally be seen by other users on the system. – coneslayer Apr 7 '10 at 15:45
True, but this is a closed environment where only an admin can get in, and all other users have been "jailed" away for SFTP purposes. – Urda Apr 7 '10 at 15:52
up vote 5 down vote accepted

First, you should avoid that lots of sudo calls. Instead, you should run the script with sudo. The final version would look like this:


# first check for root user
if [ ! $UID -eq 0 ]; then
    echo "This script must be run as root."
    exit 1

# check if the user provided an argument
if [ -z $1 ]; then
    echo "No username provided. Usage: $0 username"
    exit 2

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.

share|improve this answer
Works great! Anyway to stop the script from running if no user is provided? – Urda Apr 7 '10 at 20:39
if [ -z $1 ]; then echo "No username provided. Usage: $0 username"; exit 2; fi – Dennis Williamson Apr 7 '10 at 21:25
echo "blah" >&2 to get extra points for proper stderr usage. – grawity Apr 8 '10 at 12:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.