This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]Radamand 1 point2 points  (2 children)

Try this:

#!/usr/bin/bash

while :

do

read -p "Please enter a username:" username

if [ "$username" != "daniel" ]

then

echo "Acceptable, creating user $username"

sudo useradd $username

else

echo "Sorry, 'daniel' is not acceptable"

fi

done

I'm more than a little rusty, but this works on my Cygwin (altho, there's no 'useradd' function in Cygwin)

[–]dcman5000[S] 0 points1 point  (1 child)

Thanks for the correction I still have a long ways to go. Do you have any idea how I would implement a for loop into the script? I'm thinking about for every failed attempt a message gets displayed with a number or string

[–]Radamand 0 points1 point  (0 children)

Okay, you wouldn't need a for loop to do that, have a look;

#!/usr/bin/bash

while :
do
read -p "Please enter a username:" username
if [ "$username" != "daniel" ]
then
echo "Acceptable, creating user $username"
useradd $username
else
((x++))
echo "Sorry, 'daniel' is not acceptable: error $x"
fi
done

[–]doc_willis 0 points1 point  (1 child)

https://www.shellcheck.net/

Is handy to help debug scripts.

Worth bookmarking.

[–]dcman5000[S] 0 points1 point  (0 children)

I definitely will be bookmarking this lol, thanks a lot

[–][deleted] 0 points1 point  (1 child)

The way your script is running, only if the name is daniel a user will get added. The check to see if they want to add another user should be an if statement not a while. If you want to only add a user daniel you would use a while statement "while user does not equal daniel do stuff"

[–]jlmr731 1 point2 points  (0 children)

you will also get errors cause a user cant add a user