Register
It is currently Mon Dec 22, 2014 2:04 am

create accounts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Sep 01, 2011 7:27 am   

Joined: Mon Sep 27, 2010 5:34 am
Posts: 1
Hi Guys
Need to start learning scripting, so here is my first go at creating a large number of accounts script:
bulk-newusers.txt

#!/bin/sh
for i in `more user-list.txt `
do
echo $i
/usr/sbin/useradd -m $i
echo $i:$i"123" | /usr/sbin/chpasswd
echo; echo "User "$i"’s password changed!"
done

That works great as long as the account does not exist,. So I tried a if statement:
#!/bin/sh
for i in `more user-list.txt `
do
echo $i
#/usr/sbin/useradd -m $i
#done

if [ -f /home/"$i" ]; then
echo "User Exsists"
exit
else
/usr/sbin/useradd -m $i

fi
done


Top
 Profile  
 PostPosted: Thu Oct 20, 2011 4:05 am   

Joined: Thu Oct 20, 2011 3:58 am
Posts: 1
Code:
#!/bin/bash
# Loop through all users
for i in `more user-list.txt `
do
  # If user exists, exit the code
  if [ `grep "${i}" /etc/passwd | wc -l` -gt 0 ]; then
    echo "User ${i} already exists!"
    exit 1
  fi
 
  # User doesn't exist, we continue adding the user
  echo "${i}"
  /usr/sbin/useradd -m "${i}"
  echo "${i}:${i}123" | /usr/sbin/chpasswd
  echo "User ${i}’s password changed!"
done
exit 0


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP