PC Help Headquarters: Need help with a Linux Script!! - PC Help Headquarters

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need help with a Linux Script!!

#1
User is offline   Pirate07 

  • Advanced Member
  • Group: Moderators
  • Posts: 167
  • Joined: 26-August 09
  • LocationUSA
OK as I have stated before I hate programming, I am not very good at it either.(Maybe this is why I don't like it :blink: ) Anyways I have this pretty basic script that I need to write for my Linux class, and I think Uncle Fester will be able to help me out on this one. :D Ok so this is what I have to do. You have to write a script that reads a list of user names. The script should also check if the user account exists. For this check to see if the user' home directory exists. If the user account does exist, then display a message stating so, if it does not exist, then create it with the following command adduser account_name. In this command, account_name is the name of the user account and will be represented by a variable in your script. To properly test the script make sure that your list includes names that are on your system as well as names that do not exist.

Note: Since you are creating new accounts with this script, it will have to be run as root.

Ok I have everything written except for one thing. I don't really know which variable to use for the adduser account_name (account_name is to be represented by a variable)

This is how its written so far

while read first last
do
if test -d "$filename"
then
echo "$filename exists"
else
useradd -m account_name
fi
done < names

I just need to substitute the variable for account_name. does anyone have any suggestions???

This post has been edited by Pirate07: 10 October 2009 - 02:50 AM

Posted Image

Posted Image

Posted Image
0

#2
User is online   noop 

  • Administrator
  • View gallery
  • Group: Administrators
  • Posts: 1128
  • Joined: 24-August 09
  • LocationUSA

View PostPirate07, on 09 October 2009 - 09:42 PM, said:

OK as I have stated before I hate programming, I am not very good at it either.(Maybe this is why I don't like it :blink: ) Anyways I have this pretty basic script that I need to write for my Linux class, and I think Uncle Fester will be able to help me out on this one. :D Ok so this is what I have to do. You have to write a script that reads a list of user names. The script should also check if the user account exists. For this check to see if the user' home directory exists. If the user account does exist, then display a message stating so, if it does not exist, then create it with the following command adduser account_name. In this command, account_name is the name of the user account and will be represented by a variable in your script. To properly test the script make sure that your list includes names that are on your system as well as names that do not exist.

Note: Since you are creating new accounts with this script, it will have to be run as root.

Ok I have everything written except for one thing. I don't really know which variable to use for the adduser account_name (account_name is to be represented by a variable)

This is how its written so far

while read first last

do
if test -d "$filename"
then
echo "$filename exists"
else
useradd -m account_name
fi
done < names

I just need to substitute the variable for account_name. does anyone have any suggestions???


I think we are missing some code here, is this everything?

while read username
do
if -d /home/$username
then
echo "username $username exists"
else
useradd -m -p encryptedPassword $username
fi
done < names


First, here is some info about the useradd:

Quote

useradd -m -p encryptedPassword username
-m : The user’s home directory will be created if it does not exist.
useradd -p encryptedPassword : The encrypted password, as returned by crypt().


Information on adding the user can be found here: Script to add user

Also, you don't need the word test after your if statement. In addition, when declaring one variable "$username" for example, you do not need the quotes as far as I know.

You don't need account_name as you already have the $username/$filename variable.

Hope this helps, I had to get some help on this too,
-noop
Posted Image Posted Image
0

#3
User is offline   Pirate07 

  • Advanced Member
  • Group: Moderators
  • Posts: 167
  • Joined: 26-August 09
  • LocationUSA
Well we are getting closer but line 3 (if -d /home/$username) still isn't right. When I run the script i get this error > line 3: -d: command not found. I have played around a little bit trying to find something that will work there but no luck yet. Here is some of the options that it gives me following the error.
Usage: useradd [options] LOGIN

Options:
-b, --base-dir BASE_DIR base directory for the new user account
home directory
-c, --comment COMMENT set the GECOS field for the new user account
-d, --home-dir HOME_DIR home directory for the new user account
-D, --defaults print or save modified default useradd
configuration
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP for the new user account
-G, --groups GROUPS list of supplementary groups for the new
user account
-h, --help display this help message and exit
-k, --skel SKEL_DIR specify an alternative skel directory
-K, --key KEY=VALUE overrides /etc/login.defs defaults
-l, do not add the user to the lastlog and
faillog databases
-m, --create-home create home directory for the new user
account
-M, do not create user's home directory(overrides /etc/login.defs)
-N, --no-user-group do not create a group with the same name as
the user
-o, --non-unique allow create user with duplicate
(non-unique) UID
-p, --password PASSWORD use encrypted password for the new user
account
-r, --system create a system account
-s, --shell SHELL the login shell for the new user account
-u, --uid UID force use the UID for the new user account
-U, --user-group create a group with the same name as the user
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping

I really don't know what else to do at this point. I am going to take a break from it and come back to it tomorrow. :blink: Maybe a good night sleep and a fresh mind will help me out.
Posted Image

Posted Image

Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users