LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 11-23-2009, 01:42 PM   #1
Karas
Member
 
Registered: Oct 2009
Distribution: Slackware 13.0
Posts: 49

Rep: Reputation: 15
While script issue, "cannot create directory..."


Hi, me again!

The script below, is probably wrong. However I am learning at the moment. What I am hoping is to make new directories for potential new users, and I would like to copy the /etc/skel folder contents to their home directories, I thought the script below would, however it does not.

I cannot use the adduser command, as this is for a university project, and am only allowed to use the newusers command, and am meant to copy the contents from the /etc/skel folder, however as you can see I am having difficulties.

Any help, would be very much appreciated.

Quote:
#!/bin/bash
PATH=$PATH
names=`cat newusers.txt | cut -f1 -d":"`

while read line
do
mkdir $names
cp -r /etc/skel $name/
done
The error message I recieve after running this script is:

Quote:
mkdir: cannot create directory `gary': File exists
mkdir: cannot create directory `test1': File exists
mkdir: cannot create directory `heat': File exists
mkdir: cannot create directory `omg': File exists
Four folders are created that correspond to each username, but, inside of one folder 3 of the same usernames from the parent directory are created, and inside only one of those is the contents of the /etc/skel directory.
 
Old 11-23-2009, 01:46 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
I'd do it this way (i didn't test this script however):

Code:
for i in `cat newusers.txt | cut -f1 -d":"`; do
  mkdir /home/$i
  cp -R /etc/skel/* /home/$i
done
 
Old 11-23-2009, 01:56 PM   #3
Karas
Member
 
Registered: Oct 2009
Distribution: Slackware 13.0
Posts: 49

Original Poster
Rep: Reputation: 15
That script gives me "permission denied" for each folder it tries to make...
 
Old 11-23-2009, 02:00 PM   #4
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Post

If you also need to create the users that are on the userlist.txt file i'll make the script like that.

Code:
#!/bin/bash
# PATH=$PATH  -- this is useless as you don't change it.

newusers newusers.txt

# there you should test if the job is done by testing $?
# i let you do it.

for i in $(cut -f1 -d":" newusers.txt) ; do

   # cp with -a i prefer.
   cp -a /etc/skel /home/$i

   # i force the owner of the file to the user ($i)
   chown -R $i /home/$i
done
But the real problem for you is that u can't run this script without being root.

So maybe all that is useless because newusers will create the userdir and copy the skel by himself if you have the right to execute it.

I suppose that you are using sudo for that.

Last edited by Chirel; 11-23-2009 at 02:03 PM.
 
Old 11-23-2009, 02:05 PM   #5
Karas
Member
 
Registered: Oct 2009
Distribution: Slackware 13.0
Posts: 49

Original Poster
Rep: Reputation: 15
Im using a cronjob for root to run all the scripts, I want it to be all automated.
 
Old 11-23-2009, 02:22 PM   #6
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Quote:
Originally Posted by Karas View Post
Im using a cronjob for root to run all the scripts, I want it to be all automated.
So i guess you don't want to run it twice with the same newusers.txt

And if you plan to make a cron job, you should log the process of the script somewhere.

Maybe only with >>/var/log/mylogfile.log at the end of the cronjob call.

But anyways, then you should add some check on the scripts - something like this.

Code:
#!/bin/bash
# PATH=$PATH  -- this is useless as you don't change it.
NEWUSERFILE=/somewhere/over/there/newusers.txt
TMPFILE=/tmp/newusers.txt.tmp

if [ -w $NEWUSERFILE ]
  then
    # we move the file to a temporary working file
    mv $NEWUSERFILE $TMPFILE
  else
    echo "Nothing to do."
    exit 0
fi

newusers $TMPFILE

# there you should test if the job is done by testing $?
# i let you do it.

for i in $(cut -f1 -d":" $TMPFILE) ; do

   # cp with -a i prefer.
   cp -a /etc/skel /home/$i

   # i force the owner of the file to the user ($i)
   chown -R $i /home/$i
done

Now the script will move the working file so a second run will do nothing.

You can't test this script without being root.

To use this script change the NEWUSERFILE to the full path of the newusers.txt file.

Beware of the PATH when using cron job, it's a good idea to add PATH=(a copy of root PATH) at the begining of the script.
 
Old 11-23-2009, 02:25 PM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Code:
#!/bin/bash
PATH=$PATH
That line stores the contents of $PATH in $PATH, resulting in, well, $PATH containing what path $PATH contains. In other words, it does nothing.

Quote:
names=`cat newusers.txt | cut -f1 -d":"`

while read line
The problem here is that you store the names into a variable, but you are not telling "read" from where to actually read them. You could echo the variable and pipe it into the while loop, but that's not the easiest in this case because cut would not provide carriage returns as far as I know. So, I'd use the for sentence provided above by someone else.

You could as well turn $names into an array,

Code:
names=(`cat foo.txt | cut -f1 -d":"`)
for i in $(seq 0 $((${#names}-1))); do
  echo names[$i]=${names[$i]}
done
Quote:
Four folders are created that correspond to each username, but, inside of one folder 3 of the same usernames from the parent directory are created, and inside only one of those is the contents of the /etc/skel directory.
You need to process the directories separately, something that your current loop fails to do. You also need to check if the directory already exists, like in

Code:
if [ -d "/home/${names[i]} ]; then
  echo "directory already exists"
else
  echo "directory doesn't exist,"
  echo "* creating it..."
  echo "* copying skel..."
fi


This is your script:

Code:
#!/bin/bash
PATH=$PATH
names=`cat newusers.txt | cut -f1 -d":"`

while read line
do
mkdir $names
cp -r /etc/skel $name/
done
This stores the names into a var, names will contain something like

Code:
name="foo bar moo cow"
Being each of the tokens the name of one user. The, your while statement reads a string and puts it into a variable called "$line", which by the way you never use in your script, so it serves absolutely no purpose (vars that are never referenced are useless, like a book that's never read).

In any case, after reading a variable from your keyboard, it runs this command

Code:
mkdir $names
With my example above, this would expand to

Code:
mkdir foo bar moo cow
This will create 4 dirs, assuming they do not exist. Where? Well... you didn't even specify, so it will create those 4 dirs wherever you are at the moment, you are creating all the dirs in a row, not a good thing because you don't know the list beforehand by the way, and it might be too long. Never make assumptions like "the list is always gonna fit in a single argument line".

The next command gets even worse:

Code:
cp -R /etc/skel $names/
which would expand to

Code:
cp -R /etc/skel foo bar moo cow/
cp uses the last argument as destination, so this command means textually: "copy recursively all of /etc/skel foo bar AND moo INTO cow/".

Then the while loop starts again, asking you for another name that won't be used for anything, and so ad infinitum.
 
Old 11-24-2009, 01:30 PM   #8
Karas
Member
 
Registered: Oct 2009
Distribution: Slackware 13.0
Posts: 49

Original Poster
Rep: Reputation: 15
I've sort of worked out how I am going to be doing the for loops, however I am just wondering, how do I get two variables out of the file newusers, which I can use in the same for loop?
 
Old 11-24-2009, 10:19 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993
What does the newusers file look like ie the content?
 
Old 11-25-2009, 01:35 PM   #10
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Quote:
Originally Posted by Karas View Post
I've sort of worked out how I am going to be doing the for loops, however I am just wondering, how do I get two variables out of the file newusers, which I can use in the same for loop?
like that ?
Code:
cat /etc/passwd | { IFS=':'; while read m_name m_pass m_uid m_gid m_rest; do echo "[$m_name $m_uid]"; done }
 
Old 11-25-2009, 02:51 PM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
I find it simpler to just read lines, then do all the parsing inside the loop to tokenize the $line.
 
  


Reply

Tags
crontab


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cgi-bin: "enable to create directory" xpucto Linux - Server 3 05-16-2007 09:09 AM
Installed kernel-devel and source for i686; didn't create directory "build" itsonlyme Linux - Kernel 3 04-05-2007 09:08 AM
totem/xine/rythmbox etc. "can't create mcop directory" a_vri Linux - Newbie 3 03-07-2007 02:17 PM
"Cannot create directory: No space left on device" but I have 7.1G available !!!! fr_laz Linux - General 3 12-09-2005 04:44 PM
"mkdir: cannot create directory `foo': Read-only file system" on FAT32 maddes Linux - Hardware 1 11-26-2003 06:19 PM


All times are GMT -5. The time now is 02:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration