BASH Creating unique username [on hold]
up vote
-1
down vote
favorite
I want to create a unique username based on the following
bash script. The variable "user" should change based on the following criteria below. How would you achieve this?
tput cup 18 18
echo "Creating a new user:"
tput cup 19 18
echo "===================="
# Setting cursor up and validating user.
tput cup 20 20;
echo -n "Please enter your first name: "
read first
echo $first
tput cup 21 20;
echo -n "Please enter your last name: "
read last
echo $last
user=$first$last
user=${first:0:1}${last:0:4}
echo $user
Example:
first=John
last=Doe
user becomes JDoe
but if a user with same firstname
first=Jane
last=Doe
It should be
user becomes JDoe1
and so on..
bash shell string
New contributor
put on hold as unclear what you're asking by G-Man, Rui F Ribeiro, RalfFriedl, jimmij, GAD3R yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I want to create a unique username based on the following
bash script. The variable "user" should change based on the following criteria below. How would you achieve this?
tput cup 18 18
echo "Creating a new user:"
tput cup 19 18
echo "===================="
# Setting cursor up and validating user.
tput cup 20 20;
echo -n "Please enter your first name: "
read first
echo $first
tput cup 21 20;
echo -n "Please enter your last name: "
read last
echo $last
user=$first$last
user=${first:0:1}${last:0:4}
echo $user
Example:
first=John
last=Doe
user becomes JDoe
but if a user with same firstname
first=Jane
last=Doe
It should be
user becomes JDoe1
and so on..
bash shell string
New contributor
put on hold as unclear what you're asking by G-Man, Rui F Ribeiro, RalfFriedl, jimmij, GAD3R yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
Is this homework? Is there a particular reason to do it in bash?
– mattdm
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I want to create a unique username based on the following
bash script. The variable "user" should change based on the following criteria below. How would you achieve this?
tput cup 18 18
echo "Creating a new user:"
tput cup 19 18
echo "===================="
# Setting cursor up and validating user.
tput cup 20 20;
echo -n "Please enter your first name: "
read first
echo $first
tput cup 21 20;
echo -n "Please enter your last name: "
read last
echo $last
user=$first$last
user=${first:0:1}${last:0:4}
echo $user
Example:
first=John
last=Doe
user becomes JDoe
but if a user with same firstname
first=Jane
last=Doe
It should be
user becomes JDoe1
and so on..
bash shell string
New contributor
I want to create a unique username based on the following
bash script. The variable "user" should change based on the following criteria below. How would you achieve this?
tput cup 18 18
echo "Creating a new user:"
tput cup 19 18
echo "===================="
# Setting cursor up and validating user.
tput cup 20 20;
echo -n "Please enter your first name: "
read first
echo $first
tput cup 21 20;
echo -n "Please enter your last name: "
read last
echo $last
user=$first$last
user=${first:0:1}${last:0:4}
echo $user
Example:
first=John
last=Doe
user becomes JDoe
but if a user with same firstname
first=Jane
last=Doe
It should be
user becomes JDoe1
and so on..
bash shell string
bash shell string
New contributor
New contributor
New contributor
asked 2 days ago
Anthony J
1
1
New contributor
New contributor
put on hold as unclear what you're asking by G-Man, Rui F Ribeiro, RalfFriedl, jimmij, GAD3R yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by G-Man, Rui F Ribeiro, RalfFriedl, jimmij, GAD3R yesterday
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
Is this homework? Is there a particular reason to do it in bash?
– mattdm
2 days ago
add a comment |
3
Is this homework? Is there a particular reason to do it in bash?
– mattdm
2 days ago
3
3
Is this homework? Is there a particular reason to do it in bash?
– mattdm
2 days ago
Is this homework? Is there a particular reason to do it in bash?
– mattdm
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You'll want to:
- create your desired username
- test if it exists:
- if it exists, increment the counter and append it to the user name: goto 2
add a comment |
up vote
0
down vote
Try like
$ TMP=${first:0:1}${last:0:4}
$ user="$TMP$(grep -Ec "^$TMP[0-9]*:" /etc/passwd | sed 's/^0$//')"
It creates a temp var with your original approach, and then grep
s (with thew -c
count option) the password file for existing users with that temp name and maybe following digits, anchored at begin-of-line, delimited by the :
. This eliminates false positives (e.g.JDoefoe
). The piping through sed
is necessary alas to eliminate the zero count for absolutly new user names.
Hoppla... a residue from testing in a file, not/etc/passwd
. Removed from answer, thanks.
– RudiC
yesterday
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You'll want to:
- create your desired username
- test if it exists:
- if it exists, increment the counter and append it to the user name: goto 2
add a comment |
up vote
0
down vote
You'll want to:
- create your desired username
- test if it exists:
- if it exists, increment the counter and append it to the user name: goto 2
add a comment |
up vote
0
down vote
up vote
0
down vote
You'll want to:
- create your desired username
- test if it exists:
- if it exists, increment the counter and append it to the user name: goto 2
You'll want to:
- create your desired username
- test if it exists:
- if it exists, increment the counter and append it to the user name: goto 2
answered 2 days ago
community wiki
glenn jackman
add a comment |
add a comment |
up vote
0
down vote
Try like
$ TMP=${first:0:1}${last:0:4}
$ user="$TMP$(grep -Ec "^$TMP[0-9]*:" /etc/passwd | sed 's/^0$//')"
It creates a temp var with your original approach, and then grep
s (with thew -c
count option) the password file for existing users with that temp name and maybe following digits, anchored at begin-of-line, delimited by the :
. This eliminates false positives (e.g.JDoefoe
). The piping through sed
is necessary alas to eliminate the zero count for absolutly new user names.
Hoppla... a residue from testing in a file, not/etc/passwd
. Removed from answer, thanks.
– RudiC
yesterday
add a comment |
up vote
0
down vote
Try like
$ TMP=${first:0:1}${last:0:4}
$ user="$TMP$(grep -Ec "^$TMP[0-9]*:" /etc/passwd | sed 's/^0$//')"
It creates a temp var with your original approach, and then grep
s (with thew -c
count option) the password file for existing users with that temp name and maybe following digits, anchored at begin-of-line, delimited by the :
. This eliminates false positives (e.g.JDoefoe
). The piping through sed
is necessary alas to eliminate the zero count for absolutly new user names.
Hoppla... a residue from testing in a file, not/etc/passwd
. Removed from answer, thanks.
– RudiC
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Try like
$ TMP=${first:0:1}${last:0:4}
$ user="$TMP$(grep -Ec "^$TMP[0-9]*:" /etc/passwd | sed 's/^0$//')"
It creates a temp var with your original approach, and then grep
s (with thew -c
count option) the password file for existing users with that temp name and maybe following digits, anchored at begin-of-line, delimited by the :
. This eliminates false positives (e.g.JDoefoe
). The piping through sed
is necessary alas to eliminate the zero count for absolutly new user names.
Try like
$ TMP=${first:0:1}${last:0:4}
$ user="$TMP$(grep -Ec "^$TMP[0-9]*:" /etc/passwd | sed 's/^0$//')"
It creates a temp var with your original approach, and then grep
s (with thew -c
count option) the password file for existing users with that temp name and maybe following digits, anchored at begin-of-line, delimited by the :
. This eliminates false positives (e.g.JDoefoe
). The piping through sed
is necessary alas to eliminate the zero count for absolutly new user names.
edited yesterday
answered 2 days ago
RudiC
3,8891312
3,8891312
Hoppla... a residue from testing in a file, not/etc/passwd
. Removed from answer, thanks.
– RudiC
yesterday
add a comment |
Hoppla... a residue from testing in a file, not/etc/passwd
. Removed from answer, thanks.
– RudiC
yesterday
Hoppla... a residue from testing in a file, not
/etc/passwd
. Removed from answer, thanks.– RudiC
yesterday
Hoppla... a residue from testing in a file, not
/etc/passwd
. Removed from answer, thanks.– RudiC
yesterday
add a comment |
3
Is this homework? Is there a particular reason to do it in bash?
– mattdm
2 days ago