Solved

Linux Shell Scripting

Posted on 2003-10-23
16
466 Views
Last Modified: 2010-03-18
Hello Everyone,

I'm stuck..I need to find a way to script the creation of directories and add new users using those directories as their home dirs. Problem is I need to do this on my linux server, and most of my scripting experience stems from using vb script.

I have never used the linux shell to script anything!

Any ideas?
0
Comment
Question by:merlin152
  • 7
  • 6
  • 3
16 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605556
Hi merlin152,

do a man useradd

for most default installations
useradd username
will add a user as well as create a home directory generally /home/username
for finer control read the man page thoroughly

Cheers!
Sunny:o)
0
 
LVL 18

Expert Comment

by:liddler
ID: 9605593
usually
useradd -d /path/to/homedir -m username
(The -m makes the homedir if it isn't there)

so for lots of users

for user in bob fred sue anne jo
do
useradd -d /path/to/homedir/$user -m $user
done
0
 

Author Comment

by:merlin152
ID: 9605635
Thanks sunny and liddler.

However, I bit info on the kind of username I'm working with:-

My users have usernames like user1 to user550. Can this increment be scripted for as well?

Merlin152
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 63 total points
ID: 9605677
i=1
while [ i -lt 551 ]
do
     adduser user$i
     i=`expr i + 1`
done

add home dirrectory path explicitly if you do not wish to have default settings
0
 
LVL 18

Accepted Solution

by:
liddler earned 62 total points
ID: 9605692
yup

#!/bin/sh
number=1
while [ $number -lt 550 ]
do
useradd -d /path/to/homedir/user$naumber -m user$number
number=`expr $number + 1`
done

would do user1 - user550
0
 
LVL 18

Expert Comment

by:liddler
ID: 9605696
bah too slow ;-(
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605702
me too typo prone ;-(
>while [ i -lt 551 ]
while [ $i -lt 551 ]
           ^ missing $
0
 

Author Comment

by:merlin152
ID: 9605744
I'm getting this error 550 times!

useradd: unable to lock password file


Any ideas?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605757
sounds like /etc/passwd file has been locked by some other application and hence useradd is not able to create new users
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605761
also, you need to execute the script as root
0
 

Author Comment

by:merlin152
ID: 9605766
how do you unlock it??
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605775
1. are you logged in as root when you are executing this script
0
 

Author Comment

by:merlin152
ID: 9605782
Yup
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605810
are you running any other application which will lock the /etc/password file... do a ps -aux and check who all are logged in as root (sudo)

next locate the lock file and try getting rid of it
in /etc/ptmp/ or some similar dir

since you are able to login, atleast the file has not been corrupted
0
 

Author Comment

by:merlin152
ID: 9606009
there's a passwd.lock file, I have deleted it. But still the script churns out the same error message.

There's a passwd- file, should I delete that one as well?
0
 

Author Comment

by:merlin152
ID: 9606678
Yeah! I got to work!...

Thanks guy's! I'll split the points between both of you.

merlin152
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multicast - Linux 4 25
url shaping application 8 50
USPS intelligent mail barcode font 11 133
How to enable SSH in Ubuntu. 7 92
I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now