Shell Scripting – Tutorial 08: From theory to practice – Part 02

Shell Scripting – Tutorial 08: From theory to practice – Part 02

Shell Scripting – Tutorial 08: From theory to practice – Part 02

Continuing, with this Tutorial 08 from our series on Shell scripting, today we will continue with another set of practical examples initiated in the Tutorial 07. Which will always be worth having on hand, to better understand and implement the shell scripting technique, within their Scripts developed for their operating systems based on GNU / Linux.

And keep in mind that, in the previous tutorials 06 and 05, addressed various Online Resources and some Best Practices. While much of the theoretical basis was addressed in tutorials 04 to 01.

Shell Scripting – Tutorial 07: From theory to practice – Part 01

Shell Scripting – Tutorial 07: From theory to practice – Part 01

And, before starting this post called “Shell Scripting – Tutorial 08”, we will recommend you to also explore the following related contents, at the end of reading it:

Shell Scripting – Tutorial 07: From theory to practice – Part 01
Related article:
Shell Scripting – Tutorial 07: From theory to practice – Part 01
Shell Scripting – Tutorial 06: Bash Shell Scripts – Part 3
Related article:
Shell Scripting – Tutorial 06: Bash Shell Scripts – Part 3

Shell Scripting Tutorial 08

Shell Scripting Tutorial 08

Examples of commands to start in Shell Scripting

Next, we will learn to extract values ​​and information of various types through executing command orders in a terminal. Starting with some simple ones like today, until reaching more advanced ones, in future tutorials.

Extract values ​​and parameters related to system users

NOMUSU1K=$(cat /etc/passwd | grep 1000 | cut -d: -f1) ; echo $NOMUSU1K
# Mostrar el Nombre de Usuario creado con determinado UID/GID (EJM. 1000) - MODO SIMPLE

NOMUSU1K=$(awk -F: '{if ($3==1000) print $1}' /etc/passwd) ; echo $NOMUSU1K
# Mostrar el Nombre de Usuario creado por UID/GID (EJM. 1000) - MODO COMPLEJO

NOMUSU1K=$(awk -F: '{if ($4==1000) print $1}' /etc/passwd) ; echo $NOMUSU1K
# Mostrar el Nombre de Usuario creado por UID/GID (EJM. 1000) - MODO COMPLEJO

USUACT=$(echo $HOME | sed 's/[/]/ /g' | awk '{ print $2}') ; echo $USUACT
# Mostrar el Nombre de Usuario según el Home creado

IDUSUACT=$(id -u | awk '{print $1}') ; echo $IDUSUACT
# Mostrar el IUD del Usuario actual

IDUSUACT=$(echo $UID) ; echo $IDUSUACT
# Mostrar el IUD del Usuario actual

IDUSUACT=$(getent passwd $USER | cut -d: -f3) ; echo $IDUSUACT
# Mostrar el IUD del Usuario actual

IDUSER1K=$(for i in 1000 1001 1002 1003 1004; do grep $i /etc/passwd | awk '{print "\t" $1}' | tr -d '[A-Za-z]' | sed 's/::x*//' | cut -c 1-5 | awk '{print $1}' ; done | sed -n '1p')  ; echo $IDUSER1K
# ID del Nombre del primer usuario creado en el sistema entre los ID existentes dentro del rango 1000 al 1005.

QUIENSOY=$(who -u | awk '{print $1}' | awk 'NR==1') ; echo $QUIENSOY
Nombre de quien esta logueado en el Sistema.

IDUSU1K=$(cat /etc/passwd | grep 1000 | cut -d: -f1 | awk '{print $1}') ; echo $IDUSU1K
# Mostrar el Nombre del usuario 1000

IDUSU1K=$(cat /etc/passwd | grep 1000 | cut -d: -f1 | awk '{print $1}') ; echo $IDUSU1K | espeak -v es -stdin
# Pronunciar el nombre del usuario 100 mediante espeak.

IDUSU1K=$(cat /etc/passwd | grep 1000 | cut -d: -f1 | awk '{print $1}') | espeak -v es-la+m1 -a 100 -p 50 -s 150 -b UTF-8 "Saludos, estimado usuario: `echo $IDUSU1K`"
# Generar un saludos por voz con el nombre del usuario 1000 mediante espeak.

Al run each command shown here, we will get a similar output (result), as shown in the following image:

Similar output (result) per screen

Shell Scripting – Tutorial 05: Bash Shell Scripts – Part 2
Related article:
Shell Scripting – Tutorial 05: Bash Shell Scripts – Part 2
Shell Scripting – Tutorial 04: Bash Shell Scripts - Part 1
Related article:
Shell Scripting – Tutorial 04: Bash Shell Scripts – Part 1

Abstract banner for post

In short, we hope this Tutorial 08 on «Shell Scripting» continue to offer a small but valuable contribution in terms of goal of learning and understanding the potential of Shell Scripting Technique. Getting to the point of, build your own CLI/GUI programs, If necessary. Therefore, if you wish to reach this point, we invite you to complement this information by exploring the following YouTube channel, where currently it is approached in a practical way the power of shell scripting.

If you liked the content, comment and share it. And remember, visit the beginning of our «site», in addition to the official channel of Telegram for more news, tutorials and Linux updates. West group, for more information on today's topic or other related ones.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.