Find the answer to your Linux question:
Results 1 to 5 of 5
I have a useradd script that can be found here: [Bash] #!/bin/bash #useradd script while true; do echo Please Enter First Name - Pastebin.com I need to add users that ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    87

    [SOLVED] how do I add users with same name to system?

    I have a useradd script that can be found here: [Bash] #!/bin/bash #useradd script while true; do echo Please Enter First Name - Pastebin.com

    I need to add users that have the same name, while keeping with the current scheme, I'd just like to have numbers at the end of the user, for example joeuser, joeuser1, joeuser2... Basically, keeping with the current naming scheme, I would like to modify the script to automatically add a number to the end of the username when I have users that have the same name.

    I just can't seem to find the command and argument that would allow me to do it. All help is greatly appreciated.

  2. #2
    Just Joined!
    Join Date
    Mar 2005
    Location
    Corona, CA
    Posts
    29
    The script is interactive, why don't you just have it find the last username that would conflict, have it echo that on the screen and ask you for the username? Efficient. FWIW, having usernames where only the last character is different will always make your future scripts more difficult since they are so much alike.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    87
    would I just do
    Code:
    uniq -d /etc/passwd
    echo You have to add a number to user
    I think I'm starting to over think this.

  4. #4
    Just Joined!
    Join Date
    Jan 2011
    Posts
    87
    I tried working on it, I added a few new lines, namely at line 25-29. I seriously thought that this would solve my problem. The code worked fine by itself, but when I add Joe User (userj) and and John User (userj) it does not append a number to the last user I added. I have the code in a pastebin here - [Bash] DECISION=yes while [ "$DECISION" = 'yes' ]; do echo Please, enter the u - Pastebin.com

  5. #5
    Just Joined!
    Join Date
    Jan 2011
    Posts
    87
    I solved it! here's the script, maybe it can help some other people

    Code:
    #!/bin/bash
    
    decision=yes
      while [[ $decision = 'yes' ]]; do
      
      read -p "please, enter the users first name." fname
      read -p "please, enter the users last name." lname
      read -p "please, enter the users initial group." igroup
      read -p "please, enter any additional groups the user belongs to." agroup
    
      igroupl=$(echo "$igroup" | tr '[:upper:]' '[:lower:]')
      agroupl=$(echo "$agroup" | tr '[:upper:]' '[:lower:]')
      lnameu=$(echo "$lname" | tr '[:lower:]' '[:upper:]')
      fnameu=$(echo "$fname" | tr '[:lower:]' '[:upper:]')
      usrnm1=$(echo "$lname" | tr '[:upper:]' '[:lower:]')
      usrnm2=$(echo "$fname" | cut -c 1 | tr '[:upper:]' '[:lower:]')  
      usrnm=$usrnm1$usrnm2
      
      useradd -c "$fnameu $lnameu" -g "$igroupl" -g "$agroupl" "$usrnm"
      echo 123456 | passwd "$usrnm" --stdin
    
      if getent passwd "$username" >/dev/null; then 
        for i in {1..50}; do 
          getent passwd "$username$i" >/dev/null || { username=$username$i; break; }; 
        done; 
      fi
      
      read -p "would you like to add another user?(yes/no)" yesno
      decision=$(echo "$yesno" | tr '[:upper:]' '[:lower:]')
        
      doneloop=1
      while ((doneloop == 1)); do
        if [[ $decision == 'yes' ]]; then
          decision=yes
          doneloop=2
        elif [[ $decision == "no" ]]; then
          echo "thank you for using this program"
          doneloop=2
        else
          echo "please use a correct option.(yes/no)"
          read yesno
          decision=$(echo "$yesno" | tr '[[:upper:]]' '[:lower:]')
        fi
      done
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...