Linux Shell Scripting

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?
merlin152Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
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
liddlerCommented:
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
merlin152Author Commented:
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

sunnycoderCommented:
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
liddlerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
liddlerCommented:
bah too slow ;-(
0
sunnycoderCommented:
me too typo prone ;-(
>while [ i -lt 551 ]
while [ $i -lt 551 ]
           ^ missing $
0
merlin152Author Commented:
I'm getting this error 550 times!

useradd: unable to lock password file


Any ideas?
0
sunnycoderCommented:
sounds like /etc/passwd file has been locked by some other application and hence useradd is not able to create new users
0
sunnycoderCommented:
also, you need to execute the script as root
0
merlin152Author Commented:
how do you unlock it??
0
sunnycoderCommented:
1. are you logged in as root when you are executing this script
0
merlin152Author Commented:
Yup
0
sunnycoderCommented:
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
merlin152Author Commented:
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
merlin152Author Commented:
Yeah! I got to work!...

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

merlin152
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Networking

From novice to tech pro — start learning today.