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



## valsorym (Aug 3, 2011)

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

I have:

```
#!/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:

```
adduser -m -d /usr/home/$user_name -p $epass $user_name
```

- in the console message

```
Username:

â€¦
```
How to automate?
Thanks.


----------



## SirDice (Aug 3, 2011)

Unlike Linux on FreeBSD adduser(8) is an interactive script. Use pw(8).


----------



## wblock@ (Aug 3, 2011)

doorways said:
			
		

> ```
> mkdir -p /usr/home/$user_name
> chmod 777 /usr/home/$user_name
> ```



"chmod 777" almost always means "I am making a mistake right now".


----------



## SirDice (Aug 3, 2011)

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.


----------



## valsorym (Aug 3, 2011)

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

```
#!/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.


----------



## UNIXgod (Aug 3, 2011)

You could script adduser() with pw()

Look at adduser() source and see how they created their script.


----------



## valsorym (Aug 3, 2011)

Thank you.
Although I am quite difficult to read large English text. :\


----------



## valsorym (Aug 3, 2011)

> Originally Posted by *UNIXgod*.
> Look at adduser source and see how they created their script.



Thank you. I read this script now.

```
# whereis adduser
adduser: /usr/sbin/adduser

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


----------



## wblock@ (Aug 3, 2011)

doorways said:
			
		

> Thank you. I read this script now.
> 
> ```
> # whereis adduser
> ...



Make that last line

```
# less /usr/sbin/adduser
```

cat(1) is rarely needed.


----------



## UNIXgod (Aug 3, 2011)

`% less `whereis adduser | cut -d: -f2``


----------



## wblock@ (Aug 3, 2011)

`% 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).)


----------



## SirDice (Aug 4, 2011)

doorways said:
			
		

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


Even as a test or as a sample it's a bad habit to get into.


----------



## valsorym (Aug 4, 2011)

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





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

```
# â€¦ {print; select (undef,undef,undef,[color="Red"]0.1[/color])} â€¦
```

WORKAROUND:

```
#!/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.


```
# bash addusr.sh USER_NAME PASSWORD
```
 

Thank you all.


----------



## SirDice (Aug 4, 2011)

doorways said:
			
		

> WORKAROUND:
> 
> ```
> #!/bin/bash
> ```


Doesn't exist. It's /bin/sh.


----------



## valsorym (Aug 4, 2011)

> Originally Posted by *SirDice*.
> Doesn't exist. It's /bin/sh.



If I used /bin/sh I have:

```
[: -eq: unexpected operator.
```
but if I use /bin/bash - I have a good result.

Yes, You are right  "Doesn't exist":

```
# 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â€?


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


----------



## wblock@ (Aug 4, 2011)

doorways said:
			
		

> If I used /bin/sh I have:
> 
> ```
> [: -eq: unexpected operator.
> ...



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


----------



## valsorym (Aug 4, 2011)

> Originally Posted by *wblock*.
> Which line? -eq0 should be -eq 0. There should be a space after [.





> Originally Posted by *doorways*.
> 
> ```
> ...
> ...


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.


----------



## UNIXgod (Aug 4, 2011)

doorways here is posix documentation which is sh:

http://rubyprogrammer.net/~stu/posix/


----------



## valsorym (Aug 4, 2011)

Thanks.


----------



## phoenix (Aug 8, 2011)

doorways said:
			
		

> If I used /bin/sh I have:
> 
> ```
> [: -eq: unexpected operator.
> ...



Don't run commands and compare the output of the command in the same line.  Separate it out:

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


----------



## valsorym (Aug 9, 2011)

Thanks.
Although I have figured out by UNIXgod



> Originally Posted by *UNIXgod*
> doorways here is posix documentation which is sh:
> http://rubyprogrammer.net/~stu/posix/



I looked at the examples and realized his mistake. After that I read more books.

But all the same, Thank you very much that responded.


----------

