I tried to write a script which creates a user.
First it needs to check if:
- root is running
- if user maybe already exists
I Also tried to set the password equal to the username which was typed in.
Overall this works fine if i copy it step by step and execute it step by step. But the whole script won't work.
#!/bin/bash
if [ "$(id -u)" = "0" ]; then
read -p "User: " username
pass=$username
if [ getent username>/dev/null 2>&1 ]; then
echo "$username already exists"
sleep 10
exit 1
else
useradd -m $username
echo "$pass" | passwd $username --stdin
[ $? -eq 0 ] && echo "User was created" || echo "Error while creating"
unset username
fi
else
echo "No root"
sleep 3
exit 2
fi
-p
. – Chris Down Nov 28 '13 at 14:59if getent passwd "$username" ...
with no[]
. – Joseph R. Nov 28 '13 at 15:00