开发者

Help me write very simple BASH script?

I am wishing to automate adding SFTP users. My commands required are the following, taking for example adding the user 'jason':

useradd jason
passwd jason
mkdir /home/jason
chown root:jason /home/jason
chmod 775 /home/jason
usermod -d /home/jason jason
usermod -a -G sftp jason
mount --bind /srv/ftp/~jason /home/jason

How may I replace each name with a variable for BASH, and have it ask for password at the beginning so it may plug it in where it is required?

I will tr开发者_Go百科y to learn to do this on my own, my mental capacity is very limited right now..


#!/bin/bash

read -p "Enter user: " user
read -s -p "Enter password: " pass

useradd "$user"
passwd --stdin "$user" <<<"$pass"
mkdir /home/"$user"
chown root:"$user" /home/"$user"
chmod 775 /home/"$user"
usermod -d /home/"$user" "$user"
usermod -a -G sftp "$user"
mount --bind /srv/ftp/~"$user" /home/"$user"

Note that if your version of passwd doesn't support the --stdin option, you're going to need to use something like expect or autoexpect

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜