Jump to content

Linux scripting assistance


Recommended Posts

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

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 by JohnFlower
Link to comment
Share on other sites

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

2c308d4af7.png

 

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

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 by JohnFlower
Link to comment
Share on other sites

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

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 $values

Unfortunately 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 by Moodkiller
Link to comment
Share on other sites

Sadly, yeah there is, something like this:


0d4541ce09.png


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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...
Please Sign In or Sign Up