How to create a new user to the sh-script?

Hi all.
I have a problem.
How to create a new user to the sh-script?

I have:
Code:
#!/bin/sh
#(c) 2011 doorways :).

if [$(id -u) -eq0 ]; then
    echo "Enter your name: "
    read user_name
	
    echo "Enter your password: "
    read user_password
	
    mkdir -p /usr/home/$user_name
    chmod 777 /usr/home/$user_name
	
    epass=`perl -e 'print crypt ("'$user_password'", "sa");'`
    adduser -m -d /usr/home/$user_name -p $epass $user_name
else
    echo "You are must be root!"
fi
# end file.

But after the command:
Code:
adduser -m -d /usr/home/$user_name -p $epass $user_name

- in the console message
Code:
Username:

…
How to automate?
Thanks.
 
It's also not needed as the pw(8) tool will create the directory (with the proper permissions) if it doesn't exist. It'll also copy the skeleton files if instructed to do so.
 
Originally Posted by doorways.
"chmod 777" almost always means "I am making a mistake right now".

It's just a sample. This script is just an example. But thank you.

Originally Posted by SirDice.
It's also not needed as the pw(8) tool will create the directory (with the proper permissions) if it doesn't exist. It'll also copy the skeleton files if instructed to do so.

Thanks.

Temporarily solved the problem:
Code:
#!/bin/sh
#(c) 2011 doorways :).

if [ $(id -u) -eq0 ]; then
    echo -n "Enter your name: "
    read user
	
    echo -n "Enter your password: "
    read password
	
    mkdir -p /usr/home/${user}
    chmod 777 /usr/home/${user}
	
    echo ${user}':::::::/usr/home/'${user}':/bin/csh:'${password} >\
        /usr/home/${user}/iuser

    adduser -f /usr/home/${user}/iuser
    rm -f /usr/home/${user}/iuser
else
    echo "You are must be root!"
fi

# End file.

But this is temporary.
Now I read the book. I am impressed with the capabilities 'shell'. Windows bat-scripts more primitive.
 
Originally Posted by UNIXgod.
Look at adduser source and see how they created their script.

Thank you. I read this script now.
Code:
# whereis adduser
adduser: /usr/sbin/adduser

# cat /usr/sbin/adduser | less
- Thanks.
 
doorways said:
Thank you. I read this script now.
Code:
# whereis adduser
adduser: /usr/sbin/adduser

# cat /usr/sbin/adduser | less
- Thanks.

Make that last line
Code:
# less /usr/sbin/adduser

cat(1) is rarely needed.
 
% perl -0777ne '$|=1;foreach (split //,$_) {print; select (undef,undef,undef,0.01)}' `whereis -q adduser | cut -d" " -f1`

(Yes, it's silly, but note the use of -q with whereis(1).)
 
Originally Posted by wblock.
"chmod 777" almost always means "I am making a mistake right now".

Originally Posted by SirDice.
Even as a test or as a sample it's a bad habit to get into.

Thanks. I'm not particularly familiar with the access rights. I will correct. :)


Originally Posted by wblock.
Make that last line
Code:
# less /usr/sbin/adduser
cat(1) is rarely needed.

Originally Posted by UNIXgod.
% less `whereis adduser | cut -d: -f2`

It is work. Thanks.

Originally Posted by wblock.
% perl -0777ne '$|=1;foreach (split //,$_) {print; select (undef,undef,undef,0.01)}' `whereis -q adduser | cut -d" " -f1`

This is a good method of reading text books.
Just need to slow.
Code:
# … {print; select (undef,undef,undef,[color="Red"]0.1[/color])} …

WORKAROUND:
Code:
#!/bin/bash
# It is addusr.sh file.
if [ $# -ne 2]; then
    exit
fi

if [ $(id -u) –eq 0 ]; then
    echo ${1}':::::::/usr/home/'${1}':/bin/csh:'${2} > $HOME/.newuser
    adduser -f $HOME/.newuser
    rm -f $HOME/.newuser
fi

# chmod +rx addusr.sh
# ./addusr.sh USER_NAME PASSWORD - but it is do not work.


Code:
# bash addusr.sh USER_NAME PASSWORD


Thank you all.
 
Originally Posted by SirDice.
Doesn't exist. It's /bin/sh.

If I used /bin/sh I have:
Code:
[: -eq: unexpected operator.
but if I use /bin/bash - I have a good result.

Yes, You are right "Doesn't exist":
Code:
# ls /bin/*sh*
/bin/csh    /bin/sh   /bin/tcsh

How can I added "bash"? Should I add it?
If I dedicate myself to FreeBSD - better to use “csh”?

Code:
# whereis bash
bash: /usr/local/bin/bash
# cd /bin/
# ln /usr/local/bin/bash bash
# ls /bin/*sh*
/bin/csh    /bin/sh   /bin/tcsh

Xm. It do not work. I read now book, again.
I read man ln.
 
doorways said:
If I used /bin/sh I have:
Code:
[: -eq: unexpected operator.
but if I use /bin/bash - I have a good result.

Which line? -eq0 should be -eq 0. There should be a space after [.

How can I added "bash"? Should I add it?
If I dedicate myself to FreeBSD - better to use “csh”?

shells/bash is in ports.

csh is great for interactive use, bash is too if you're into that, but shell scripts should be written for standard sh(1).
 
Originally Posted by wblock.
Which line? -eq0 should be -eq 0. There should be a space after [.

Originally Posted by doorways.
Code:
...
if [ $(id -u) –eq 0 ]; then
...
That is what he wrote. :stud

Originally Posted by wblock.
shells/bash is in ports.

Thanks.

Originally Posted by wblock.
csh is great for interactive use, bash is too if you're into that, but shell scripts should be written for standard sh(1).

O. I understand. Thank you.
 
doorways said:
If I used /bin/sh I have:
Code:
[: -eq: unexpected operator.
but if I use /bin/bash - I have a good result.

Don't run commands and compare the output of the command in the same line. Separate it out:
Code:
uid=$( id -u )
if [ "${uid}" -eq "0" ]; then
  blah blah
fi

The original code fails due to the way the shell parses $() and [ ] and tries to run commands incorrectly.

Keep your comparisons simple: compare strings or numbers only, not commands.
 
Back
Top