0

I try to run the following bash script to create a bunch of users, groups, home dirs for the users and correct permissions for all of these. The OS is CentOS.

When I try to run the following, which I though should work, it returns "command not found" when running via terminal. it only gets as far as creating the /homedirs directory, nothing more. I'm a total noob at bash scripting so forgive me if this looks ugly.

mkdir /homedirs; chmod 775 /homedirs;

for iYear in {1..3} do
    sYear = $iYear"ti"
    sYearDir = "/homerirs/"$sYear
    groupadd $sYear; mkdir $sYearDir; chgrp $sYear $sYearDir; chmod 750 $sYearDir

    for sClass in {a,b} do
        sClassDir = $sYearDir/$sClass
        mkdir $sClassDir
        sClassGrp = $sYear$sClass
        groupadd $sClassGrp; chgrp $sClassGrp $sClassDir; chmod 750 $sClassDir

        for iUser in {1..3} do
            sUserName = "i"$iYear$sClass"g"$iUser
            sUserDir = $sClassDir/$sUserName
            useradd -d $sUserDir -g $sClassGrp -G $sYear -m $sUserName
            chown $sUserName $sUserDir; chmod 750 $sUserDir
        done
    done
done
MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • 1
    Doesn't it have to be `for ... in ... ; do`, with semicolons separating flow control statement and `do` block? – raina77ow Oct 03 '12 at 20:49
  • @raina77ow that could be. I'm still quite unfamliar with the syntax. In fact I think it should be, too, come to think of it. Edit: that didn't solve the problem... – MarioDS Oct 03 '12 at 20:51
  • `for iYear in 1 2 3; do` and `for sClass in a b; do` would not only be shorter, but also more portable. – tripleee Oct 03 '12 at 20:53
  • You might want to fix the spelling of `homerirs` too. – tripleee Oct 03 '12 at 20:56
  • @raina77ow: +1 Yes you do need semicolons. – tripleee Oct 03 '12 at 20:58
  • Hmm. Can you do this `cat scriptname.sh | sed 's/\r//'`, when `scriptname.sh` is your script filename? – raina77ow Oct 03 '12 at 20:59
  • @tripleee I cannot believe I've overlooked that... – MarioDS Oct 03 '12 at 20:59
  • Or just run `dos2unix` on that script, and try to run it again... See, it's weird having it execute just the first line, and then stop with 'Command not found'. – raina77ow Oct 03 '12 at 21:01
  • Your variable names got translated into Dutch (?) somewhere halfway. You must have a very dangerous computer virus (-: – tripleee Oct 03 '12 at 21:04
  • @tripleee no, the original variable names were in Dutch. I changed them here to be more readable to answerers. – MarioDS Oct 03 '12 at 21:07
  • I don't think you need to `chown` the directory created by `useradd` and it should hopefully already have the right permissions, too. – tripleee Oct 03 '12 at 21:48

3 Answers3

1

You may need to set your PATH and you really should read the advanced bash scripting guide. See also this answer.

I also suggest to debug your script by starting it with #!/bin/bash -vx as its first line. And you should make it executable with chmod u+x at least.

Perhaps groupadd might not be available on your system.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

The error message is caused by the spaces around the equals signs. A token with whitespace after it is interpreted as a command name; so what you intended as variable names causes the Command not found errors.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Can you also tell me how to login with these users? As you can see, no password was specified but the login screen or terminal ask me for a password. When I leave it blank, there's an authentication failure... – MarioDS Oct 03 '12 at 21:30
  • You should probably pass in a password with an option to `useradd`. To change the password of an existing user, run `passwd $user` as root. – tripleee Oct 03 '12 at 21:37
  • ... or install a suitable public SSH key and leave them passwordless. – tripleee Oct 03 '12 at 21:44
  • I don't really get what you mean with that last. The teacher (yes, this is for school) just asked us to script this and he said don't give a password with useradd. We have only had a poor introduction to unix last year... – MarioDS Oct 03 '12 at 22:05
  • If it's just for demonstration, you can access an account with `su - $user`. Normally a (non-system) account with no way to log in is useless. You have to have either a password or some alternate means of authentication, such as an SSH keypair. Kerberos is also popular in some organizations. – tripleee Oct 04 '12 at 03:25
-1

best thing to do is add the full path before your executables:

change useradd to /usr/sbin/useradd

change groupadd to /usr/sbin/groupadd

will cure the command not found.

remember this programs will probably need to run as root to work.

DannyK
  • 1,342
  • 16
  • 23