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
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.