Little test script and how I have discovered stty to be useful — cool utility.

While training for LPI 102 I have come across an exercise — to assemble a little shell script that allows sysadmin to create an user [the script must prompt for user name, user login, create user’s home directory, check if useradd ran successfully and then change user’s password].

This is how I did mine. Sure it can be improved — after all I am just practicing for a LPI exam — in real world it would be a lot more sophisticated [bullshit — it would be the very same with some improvements of course — logging would be one to be implemented].

Anyway — for academic purposes it should be fine. I suppose.

Something I have learned while putting it together — while searching for a way to hide user’s password while typing it on shell I’ve come across ‘stty’ command. According to man: “stty – change and print terminal line settings”.

Magic CLI: stty -echo will disable any echo characters — thus hiding any output to the terminal. Kind of useful.


echo "Insert new user's name"
read username

echo "Insert new user's login"
read userlogin

if [ -z "$USER_NAME" ] || [ -z "$USER_LOGIN" ] ; then
 echo "Inform username and user login."
 exit 0

useradd -m -c "$USER_NAME" "$USER_LOGIN"

if [ $? -eq 0 ] ; then
 echo "User [$USER_NAME] sucessfully created."
 echo "Error while creating user [$USER_NAME]."

echo "Change password for user [$USER_NAME]."

stty -echo

read password

echo "$password" | passwd --stdin "$USER_LOGIN"

stty echo

And its output:

[root@random Desktop]# ./
Insert new user's name
Insert new user's login
User [Test] sucessfully created.
Change password for user [Test].
Changing password for user test.
passwd: all authentication tokens updated successfully.
[root@random Desktop]#


Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )


Conectando a %s