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..










share|improve this question







New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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















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..










share|improve this question







New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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













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..










share|improve this question







New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Anthony J

1




1




New contributor




Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Anthony J is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




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














  • 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










2 Answers
2






active

oldest

votes

















up vote
0
down vote













You'll want to:




  1. create your desired username

  2. test if it exists:

  3. if it exists, increment the counter and append it to the user name: goto 2






share|improve this answer






























    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 greps (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.






    share|improve this answer























    • Hoppla... a residue from testing in a file, not /etc/passwd. Removed from answer, thanks.
      – RudiC
      yesterday


















    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:




    1. create your desired username

    2. test if it exists:

    3. if it exists, increment the counter and append it to the user name: goto 2






    share|improve this answer



























      up vote
      0
      down vote













      You'll want to:




      1. create your desired username

      2. test if it exists:

      3. if it exists, increment the counter and append it to the user name: goto 2






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You'll want to:




        1. create your desired username

        2. test if it exists:

        3. if it exists, increment the counter and append it to the user name: goto 2






        share|improve this answer














        You'll want to:




        1. create your desired username

        2. test if it exists:

        3. if it exists, increment the counter and append it to the user name: goto 2







        share|improve this answer














        share|improve this answer



        share|improve this answer








        answered 2 days ago


























        community wiki





        glenn jackman

























            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 greps (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.






            share|improve this answer























            • Hoppla... a residue from testing in a file, not /etc/passwd. Removed from answer, thanks.
              – RudiC
              yesterday















            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 greps (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.






            share|improve this answer























            • Hoppla... a residue from testing in a file, not /etc/passwd. Removed from answer, thanks.
              – RudiC
              yesterday













            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 greps (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.






            share|improve this answer














            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 greps (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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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


















            • 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



            Popular posts from this blog

            サソリ

            広島県道265号伴広島線

            Setup Asymptote in Texstudio