Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Linux (http://www.go4expert.com/forums/linux-forum/)
-   -   Having problem with the Linux Shell scripting (http://www.go4expert.com/forums/linux-shell-scripting-t4668/)

larrenV2.003 13Jun2007 21:15

Having problem with the Linux Shell scripting
 
I have been tasked with the following:

You have been asked by your boss to set up 100 user accounts for your SLES9 Linux server. Each user account should have the following details:

Login name: userxx
Password: p@Ssxx
UID: 1900+xx
GID: 747 (name of group is IMFDelegates)
Informational name: “Secret Agent xx”
Home directory: /home/agentxx
Login shell: /bin/sh

For example, the first user will have the following details:
Login name: user00
Password: p@Ss00
UID: 1900
GID: 747
Informational name: “Secret Agent 00”
Home directory: /home/agent00
Login shell: /bin/sh

And for example, the last user will have the following details:
Login name: user99
Password: p@Ss99
UID: 1999
GID: 747
Informational name: “Secret Agent 99”
Home directory: /home/agent99
Login shell: /bin/sh

Note: You will need to create a shell script to automate this task. You can make use of the groupadd and useradd scripts inside your shell script. You can also use the mkpass command to create the encrypted passwords.

Would like to have some pointers from the experts here on creating the script, and at the same time showing me source codes of creating the scripts as mentioned above. Thank You! :D

Lastly i would lke to know thea meaning of UID and GID? Not forgettiing the examples of these 2.

munkyeetr 14Jun2007 06:42

Re: Having problem with the Linux Shell scripting
 
homework?
try these at the command prompt..
Code:

man useradd
Code:

man groupadd

larrenV2.003 14Jun2007 09:42

Re: Having problem with the Linux Shell scripting
 
Quote:

Originally Posted by munkyeetr
homework?
try these at the command prompt..
Code:

man useradd
Code:

man groupadd

Yea... :(

The problem is how do i go about creating the script and how do i add the command to create user name and password.

Especially if i should use "for" loop or the "do while" loop to make count the number of users.

Btw if i use the "man useradd" and "man groupadd" means i create the group 714 and and user 1900?

munkyeetr 14Jun2007 10:03

Re: Having problem with the Linux Shell scripting
 
You won't learn by having people just hand you working source code...

man useradd and man groupadd lays out the syntax for using those commands to do what you want.

http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html is a link to a site showing basic bash scripting.

You'll have to put in some effort and come up with an attempt at writing it yourself before people will help you (generally speaking), otherwise what's the point? Why bother "learning it"? And, I don't think it's even so much how to do this script, but how to find the information you need to create the script, that's more important than the script itself.

After you first posted this thread I decided to write one myself, and searched, and tested through trial and error, and found out how to do it. I now have a working script that does exactly what you want.

So, put some effort into it and I will help you further, post your code, and explain what you are having trouble understanding...

...or not, it's up to you.

larrenV2.003 1Jul2007 21:12

Re: Having problem with the Linux Shell scripting
 
here is my source code

need some advice ;)

Code:

#!/bin/sh
sux=”sux”
dash=”-“
passwd_1=”123pass”

gad=”groupadd”
g=”-g”
gid=747
re=”IMFDelegates”

vi=”vi”
ep=”/etc/password”

uid=1900
num=0
u=”user”
cross=”x”
iname_1=”Secret”
iname_2=”Agent”
homedir=”/home/agent”
logshell=”bin/sh”
p=”passwd”
password_2=”p@Ss”


$sux $dash        #input command line sux – to enter root
$passwd_1        #input password 123pass

$gad $g $gid $re        #create group with command line groupadd –g 747 IMFDelegates

while [ $num –lt 99]
do
echo $vi  $ep
echo $u:$cross:$uid:$gid:$iname_1 $iname_2$num:$homedir$cross:$logshell
echo $p $u$num
echo $passwd_2$num
num=$((num+1))
done

echo “All 99 user accounts created”


munkyeetr 1Jul2007 21:58

Re: Having problem with the Linux Shell scripting
 
Okay, have you tried running your code? Does it do what you need?

I have a few words of advice:

1) You do not need to declare so many variables. Variables generally hold values that change, hence the word variable. So, in our script, the only value that really changes is the User ID. For each different User ID we perform the same actions. We can virtually eliminate all the variables you have declared, except one. I'll give you a hint: keep the $uid variable.

2) System commands do not need to be declared as variables. They can just be called from the script. That is what a script is; a series of system commands. So things like groupadd and useradd can just be called. HINT: you may need to set the path to your user commands depending on the distibution you are using. For instance in Fedora 7 (which I am using) I need to be specific about calling /usr/sbin/useradd. Just calling useradd gives me an error command not found.

3) The biggest problem I see with your code is that you are trying to manipulate the /etc/passwd file directly. THIS IS NOT A GOOD IDEA.
The useradd, usermod, and userdel commands will handle all this for you, and they will edit the file correctly. Use the tools in the toolbox and don't try to reinvent the wheel. Letting these system commands do the work for you will also cut down your code substantially (The script I have written is only 8 lines long).

You are on the right track. Your use of the while loop is correct, though the contents of it are not (again, do not manipulate the /etc/passwd file directly).

Give it another try, taking into account the advice I've given here.

larrenV2.003 1Jul2007 22:10

Re: Having problem with the Linux Shell scripting
 
okie so i just type groupadd –g 747 IMFDelegates in to the script itself? Btw i using "konsole" CLI program from my SUSE LINUX ENTERPRISE SEVER 9.

The part on manipualtion i dun understand can show me another examples on how to avoid the manipuation on etc/passwd?

munkyeetr 1Jul2007 22:45

Re: Having problem with the Linux Shell scripting
 
Just so you know, I am presuming what your code will do because I do not want to run it on my system.

Quote:

okie so i just type groupadd –g 747 IMFDelegates in to the script itself?
Yes, you can put that call into your script. Do not put it in the while loop because we only need to create the group once, not each time we create a user.

Quote:

The part on manipualtion i dun understand can show me another examples on how to avoid the manipuation on etc/passwd?
Basically it looks to me like your script is trying to open the /etc/passwd file with vi editor, and write a new line with the user information:

Code:

while [ $num –lt 99]
      do
          echo $vi  $ep
          echo $u:$cross:$uid:$gid:$iname_1 $iname_2$num:$homedir$cross:$logshell
          echo $p $u$num
          echo $passwd_2$num
          num=$((num+1))
      done

Is that what you are trying to do here?

If you pass your new user information to the useradd command instead (in the form of command-line arguments), useradd will make the entry in /etc/passwd for you. It will also create the home directory for the new user.

Type useradd --help or man useradd for information on using that command.
HINT: It will look something like this (I'm pretty much giving it to you):
Code:

useradd -c common-name -d /home/username -g gid -u uid -s shell -p password
login-name

munkyeetr 1Jul2007 22:47

Re: Having problem with the Linux Shell scripting
 
EDIT: for above (I am still unable to edit my posts :mad:)

The last line login-name should be the last part of the useradd command in the code tags!

munkyeetr 2Jul2007 08:05

Re: Having problem with the Linux Shell scripting
 
Hey larrenV2.003,

How ya coming along?


All times are GMT +5.5. The time now is 16:39.