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.


#!/bin/bash

echo "Insert new user's name"
read username
USER_NAME=$username

echo "Insert new user's login"
read userlogin
USER_LOGIN=$userlogin

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

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

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

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]# ./createuser.sh
Insert new user's name
Test
Insert new user's login
test
User [Test] sucessfully created.
Change password for user [Test].
Changing password for user test.
passwd: all authentication tokens updated successfully.
[root@random Desktop]#

Deixe uma resposta

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

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. 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 )

Foto do Google+

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

Conectando a %s