Moodkiller Posted June 4, 2014 Report Share Posted June 4, 2014 I am stuck on a little personal project of mine where I need to create a script that will allow you to enter users (according to a specific syntax) and subsequently those users are added to the system. Unfortunately there are specific conditions to be met where by the users must be entered as group name:base user name:number of users. E.g I want to add 5 new users to a group called "NOSL" with the base user name "NOSL-" The following should be entered into a input box as follows NOSL:NOSL-:5 I am currently using ubuntu 13.1. I would greatly appreciate any and all help. There are a few more finer details that need addressing, but will add those later on. Link to comment Share on other sites More sharing options...
JohnFlower Posted June 4, 2014 Report Share Posted June 4, 2014 (edited) Add this to your .bashrc:adduserisforchumps() { if [ "$1" ] && [ "$2" ] && [ "$3" ]; then count=1 while [ $count -le $3 ]; do read -p "User name: " user echo $1 $2$user count=$(( $count + 1 )) done else echo "Wrong" fi}Edit: Spot the difference. Edited June 4, 2014 by JohnFlower Link to comment Share on other sites More sharing options...
Moodkiller Posted June 4, 2014 Author Report Share Posted June 4, 2014 Add this to your .bashrc: Thank you, and I cant even seem to find my .bashrc file... (unless you were referring to pasting it into the bash terminal its self? bare with me...) I assume its in the home directory? The script has to be a stand alone file as well, If I understand what you want me to do. E.g of the format And the mistake was fi} corrected to fi and } and a new line? (Going off the email notification that I sent which does not carry proper formatting) Link to comment Share on other sites More sharing options...
JohnFlower Posted June 4, 2014 Report Share Posted June 4, 2014 (edited) The .bashrc file is a file in your home directory. You can use it to add/change commands in your bash shell. Just create the file if you don't have one already. Add what I pasted and then test it out. You will have to start a new session before it kicks in. (To test, type 'adduserisforchumps herp derp- 5') Edit: Oh you mean it's an assignment..? Edited June 4, 2014 by JohnFlower Link to comment Share on other sites More sharing options...
Moodkiller Posted June 4, 2014 Author Report Share Posted June 4, 2014 Right so I added your code to the bashrc file restarted, entered adduserisforchumps herp derp -5, (no errors were given), went and checked the passwd file and there were no new users there? Link to comment Share on other sites More sharing options...
JohnFlower Posted June 4, 2014 Report Share Posted June 4, 2014 Of course. Adding users this way is kinda dumb, so I left out the command that actually adds the user >_> I'm sure you can figure it out. P.S '-5' would break this, probably. You should add decent error handling if you plan to use this often. Link to comment Share on other sites More sharing options...
Moodkiller Posted June 4, 2014 Author Report Share Posted June 4, 2014 Of course. Adding users this way is kinda dumb, so I left out the command that actually adds the user >_>LOL but I need this xD. The way that is required of me to do is by adding the users via an input box with the syntax described in the OP. but I assume the method is the same. I'm sure you can figure it out.P.S '-5' would break this, probably. You should add decent error handling if you plan to use this often.Thanks, unfortunately I do not have the programming knowledge needed for this, but thank you. And yes, that another requirement (if user exists, then an error, if group exists, then an error etc etc) Link to comment Share on other sites More sharing options...
Moodkiller Posted June 6, 2014 Author Report Share Posted June 6, 2014 (edited) This is what I have so far: #!/bin/bash #variables group="" base_name="" amount="" #open fd # 0 = stIN # 1 = stOUT # 2 = stDERR exec 3>&1 #Store data in $VALUES varibale values=$(dialog --ok-label "Submit" \ --backtitle "Add user" \ --title "Add users" \ --form "Create a new user" \ 15 50 0 \ "base name:" 1 1 "$base_name" 1 11 10 0 \ "group:" 2 1 "$group" 2 11 5 0 \ "amount:" 3 1 "$amount" 3 11 8 0 \ 2>&1 1>&3) #close fd exec 3>&- ######################################### if [ $(id -u) -eq 0 ]; then read -p "$base_name" # read -s -p "Enter password : " password egrep "^$base_name" /etc/passwd >/dev/null if [ $? -eq 0 ]; then echo "$base_name exists!" exit 1 else # pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) sudo useradd -m -p $base_name [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" fi else echo "Only root may add a user to the system" exit 2 fi #save $values to appropriate locations #sudo groupadd $group #sudo useradd -G&$base_name #Delete temp files [ -f $values ] && rm $valuesUnfortunately I cant get the layout quite right and I didn't completely understand your first code that you sent, so was trying out this version. Edited June 6, 2014 by Moodkiller Link to comment Share on other sites More sharing options...
JohnFlower Posted June 6, 2014 Report Share Posted June 6, 2014 There's a layout? You will need a loop of some kind to do the operation x amount of times. Link to comment Share on other sites More sharing options...
Moodkiller Posted June 7, 2014 Author Report Share Posted June 7, 2014 Sadly, yeah there is, something like this:but it must be all on one line and each 'field' as such should be separated by a colon. And yes I though as much with the loop, and the loop should be equal to the "amount" of users, then I also have to include a function that will automatically name the other users Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now