Create user if UID not exist; else, exit the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create user if UID not exist; else, exit the script
# 1  
Old 12-12-2012
Create user if UID not exist; else, exit the script

Hi, I want to write a script to check whether an user ID is used in my server and then create that user.

If the user ID is not used, I will echo something like "OK, continue" and then continue to execute the script. Else, I will echo something like "Used, exit" and then exit the script.

As my system only have around 10 users, using grep and awk the /etc/passwd, I can check whether the UID is used. But in the shell script, when I want to
cat /etc/passwd | grep 'UID' | awk $3 and save the result to a variable, I can't do so.

Do anyone have idea on how can I write this shell script? I have no idea now since I can't save the "cat /etc/passwd" to a variable.
# 2  
Old 12-12-2012
You could use the id command:

Code:
# cat myScript
if ! id 2340 > /dev/null 2>&1; then
  echo ok to create user...
fi

# ./myScript
ok to create user...

# 3  
Old 12-12-2012
what you mean by cant save to variable ?

UID is the variable which has the user id.

Code:
 
RESULT=$(awk -F: -v U="$UID" '$3==U {print "yes";exit}' /etc/passwd)
 
[ -z $RESULT ] && echo "Already Exists..Exiting..." && exit 1
#else your code goes here...
.....
.....


Last edited by itkamaraj; 12-12-2012 at 03:57 AM..
# 4  
Old 12-12-2012
Quote:
Originally Posted by itkamaraj
what you mean by cant save to variable ?

UID is the variable which has the user id.

Code:
 
RESULT=$(awk -F: -v U="$UID" '$3==UID {print "yes";exit}')
 
[ -z $RESULT ] && echo "Already Exists..Exiting..." && exit 1
#else your code goes here...
.....
.....

I think you forgot something important for your awk Smilie
This User Gave Thanks to Scott For This Post:
# 5  
Old 12-12-2012
filenaming is mising.. thanks scott.. its corrected now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Me. The script should not exit until the user gives an input.

Hi everyone, I'm new here and just a beginner in linux scripting. Just want to ask for help on this one. I am trying to create a script that will accept user input (year-month and user/s). I wanted to have the script to continue running, until the user inputs a DATE and name/s of user/s. ... (2 Replies)
Discussion started by: Helskadi
2 Replies

2. Shell Programming and Scripting

Exit script and open program via other user

Hello all.. so i have a problem i need to solve .. #! /bin/bash $SHELL dtterm -title my_prog -e su -user -c 'export DISPLAY=:0.0 ; /path/to/my/prog' & 2> /dev/null $SHELL intr exit This script will work on solaris 10 system in right clikt menu - in a secure system so i need to... (0 Replies)
Discussion started by: defs
0 Replies

3. Shell Programming and Scripting

Find if a User exist if not create user

What I'm trying to do is write a script in Perl to find a user and if that user exist it would print "User Exist, Pls Try Again". If The user doesn't exist I'm able to create a user with a password. Any suggestions? (3 Replies)
Discussion started by: GoBoyGo
3 Replies

4. Ubuntu

Exit user in bash script

I'm writing a bunch of scripts to automatically configure Ubuntu and I want to run the code below to remove the white dots from the login screen: sudo xhost +SI:localuser:lightdm sudo su lightdm -s /bin/bash gsettings set com.canonical.unity-greeter draw-grid false The problem is that... (3 Replies)
Discussion started by: maerlyngb
3 Replies

5. Solaris

Can we create multiple users with same UID?

* Can we create multiple users with same UID? * Can we give root permissions to normal user like admin.s ? If YES give me full details (syntax of sudo/RBAC) (14 Replies)
Discussion started by: Navkreddy
14 Replies

6. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

7. Shell Programming and Scripting

Check file and if it doesnt exist , exit script

Hi, Another problem, here is my code #!/bin/sh dir='/opt/apps/script/CSV' datadir='/opt/apps/script/data' while : ; do ls -1rt $dir/*.csv > /dev/null 2>&1 if ;then cp $datadir/weekly.txt $dir/weekly.csv else exit 0 fi done (10 Replies)
Discussion started by: tententen
10 Replies

8. Shell Programming and Scripting

Exit script if the user dosent enter any data within 5 seconds

Hello friends, Kindly help me in developing a script that asks user to enter a value and will wait for 5 seconds for the feedback. If there is no answer from the user the script will perform exit or it will continue doing something else Ex: If yu have a multi OS system i believe while... (3 Replies)
Discussion started by: frozensmilz
3 Replies

9. Shell Programming and Scripting

exit script if user input not four characters

#!/usr/bin/bash ###script to input four characters. wxyz echo "input first string" read instring1 echo "input second string" read instring2 ## echo "first string is:" $instring1 echo "second string is:" $instring2 ##IF instring1 or instring2 are NOT 4 characters (xxxx) , exit 1. ##how?? ... (2 Replies)
Discussion started by: ajp7701
2 Replies

10. Shell Programming and Scripting

If file not exist create a new one

i want a script to check whether the file name exits or not if not it has to create a new one (3 Replies)
Discussion started by: din_annauniv
3 Replies
Login or Register to Ask a Question