How to extract parameters from the Terminal applying Shell Scripting

El Shell scripting, It refers to the execution of complex orders about him GNU / Linux Terminal (Console), it is very useful to automate routine and important activities within our GNU / Linux Operating System, which allows us to optimize our Resources and Time, that is, with what is contained here we will explore how from the terminal we can execute manual command orders that then allow us schedule / automate activities about a team saving Hours / Labor of manual or face-to-face execution, implementing said orders in a Bash Shell Script or compatible and explained in a practical and simple way.

Selection_007

We will cover as in a single command command CAN extract and display values ​​/ information from the Operating System / Hardware, which we can then implement within a Bash Shell script to automate a specific task. Focusing on the Best Practices necessary to obtain an excellent design of a Script in the way more efficient and practical.

--------------------
Get the name of the first user created in the system:
--------------------

USER_1000=$(cat /etc/passwd | grep 1000 | cut -d: -f1) ;  echo $USER_1000

USER_1001=$(cat /etc/passwd | grep 1001 | cut -d: -f1) ;  echo $USER_1001

----------------------
Get the path of the / home of the first user created on the system:
----------------------

USER_1000=$(cat /etc/passwd | grep 1000 | cut -d: -f1) ; HOME_USER_1000=/home/$USER_1000 ; echo $HOME_USER_1000

USER_1001=$(cat /etc/passwd | grep 1001 | cut -d: -f1) ; HOME_USER_1001=/home/$USER_1001 ; echo $HOME_USER_1001

-------------
Check Current Equipment Date:
-------------

FECHA_ACTUAL=$(date +"%d %b %y") ; echo $FECHA_ACTUAL

------------
Check Current Equipment Time:
------------

HORA_ACTUAL=$(date +"%H:%M") ; echo $HORA_ACTUAL

----------------
Check if the Host has an Internet connection:
----------------

if ping -c 1 8.8.8.8 &> /dev/null; then CONEXION_INTERNET=Habilitado; else CONEXION_INTERNET=Deshabilitado; fi ; echo $CONEXION_INTERNET

TEST_PING=$(ping 192.168.3.249 -c 5 | grep packet | awk '{print $6}' | cut -f1 -d%) ; echo $TEST_PING % de Perdida de paquetes
TEST_LATENCIA=$(ping 8.8.8.8 -c 5 | grep packet | awk '{print $10}' | cut -f1 -d%) ; echo $TEST_LATENCIA de Latencia del Enlace
-------------
Check the type of Operating System:
-------------

SISTEMA_OPERATIVO=$(uname -o) ; echo $SISTEMA_OPERATIVO

—————————————————————-
Check the name, version and subversion of the Operating System:
—————————————————————-
NOMBRE_SISTEMA=$(cat /etc/os-release | grep NAME | grep -v "VERSION" | sed -n '2p' | cut -f2 -d\") ; echo $NOMBRE_SISTEMA
VERSION_SISTEMA=$(cat /etc/os-release | grep VERSION= | sed -n '1p' | sed 's/VERSION=//' | sed 's/"//g') ; echo $VERSION_SISTEMA

SUBVERSION_SISTEMA=$(lsb_release -d | awk '{print $4}') ; echo $SUBVERSION_SISTEMA

----------------
Check the architecture of the Operating System:
----------------

ARQUITECTURA=$(uname -m) ; echo $ARQUITECTURA

ARQUITECTURA=$(uname -m) ; if [[ "$ARQUITECTURA" = "x86" ]]; then ARQ_SISTEMA=32; else ARQ_SISTEMA=64; fi ; echo $ARQ_SISTEMA

------------------
Check the kernel version of the Operating System:
------------------

VERSION_KERNEL=$(uname -r) ; echo $VERSION_KERNEL

----------
Check Host name:
----------

NOMBRE_HOST=$(cat /etc/hostname) ; echo $NOMBRE_HOST
—————————————————————-
Check Internal and External IP (Main Internet Output):
—————————————————————-

IP_INTERNA=$(hostname -I) ; echo $IP_INTERNA

IP_EXTERNA=$(curl -s ipecho.net/plain;echo) ; echo $IP_EXTERNA

------------------
Check Values ​​(IP / MAC) of Network Interfaces:
------------------

IP_ETH0=$(ifconfig eth0 | grep inet | grep -v inet6 | cut -d ":" -f 2 | cut -d " " -f 1) ; echo $IP_ETH0

MAC_ETH0=$(ifconfig eth0 | sed -n '1p' | awk '{print $5}') ; echo $MAC_ETH0

IP_WLAN0=$(ifconfig wlan0 | grep inet | grep -v inet6 | cut -d ":" -f 2 | cut -d " " -f 1) ; echo $IP_WLAN0

MAC_WLAN0=$(ifconfig wlan0 | sed -n '1p' | awk '{print $5}') ; echo $MAC_WLAN0
-----------------
Check the Proxy / Gateway of the Operating System:
-----------------

PROXY_GATEWAY=$(route -n | sed -n '3p' | awk '{print $2}') ; echo $PROXY_GATEWAY

----------------
Check the Host Network Domain Name:
----------------

DOMINIO=$(cat /etc/resolv.conf | sed '2 d' | grep search | sed -n '1p' | awk '{print $2}') ; echo $DOMINIO

—————————————————————
Check the Network Address (IP) of the Host DNS Server:
—————————————————————

IP_SERVIDOR_DNS=$(cat /etc/resolv.conf | sed '1 d' | awk '{print $2}') ; echo $IP_SERVIDOR_DNS

-------------
Check users connected to the Host:
-------------

who>/tmp/who ; echo -e "Usuarios conectados al Host :"  && echo "" && echo "Usuarios Puertos      Fecha      Hora  Pantalla" ; echo "*************************************************************************" && cat /tmp/who

USUARIOS_CONECTADOS=$(who | awk '{print $1}') ; echo $USUARIOS_CONECTADOS

USER_ONLINE1=$(who | awk '{print $1}') ; echo $USER_ONLINE1 | wc -w

USER_ONLINE2=$(top -n 1 -b | grep "load average:" | awk '{print $6}') ; echo $USER_ONLINE2
---------------------------------
Check User Folder with more data (# of Files / Size in Bytes) in the Operating System:
----------------------------------

CARPETA_USUARIO1=$(ls -l /home | sed '1 d' | sort -k2 | sed q | awk '{print $9}') ; echo $CARPETA_USUARIO1

DATA_USUARIO1=$(du -sh /home/* | sort -r | sed q | awk '{print $1}') ; echo $DATA_USUARIO1

——————————————————————————————
Check Size in Bytes of Superuser Folder or other Folder of the Operating System:
——————————————————————————————

DATA_ROOT=$(du -sh /root | awk '{print $1}') ; echo $DATA_ROOT

DATA_CARPETA1=$(du -sh /var | awk '{print $1}') ; echo $DATA_CARPETA1

--------------------
Check Created User Folders (Names and Numbers):
--------------------

NOMBRES_CARPETAS=$(ls -l /home | sed '1 d' | awk '{print $9}') ; echo $NOMBRES_CARPETAS

NUMERO_CARPETAS=$(ls -l /home | sed '1 d' | awk '{print $9}') ; echo $NUMERO_CARPETAS | wc -w

—————————————————————
Check Users with UID 0 AND GID 0 (SUPERUSERS) created:
—————————————————————

SUPERUSUARIOS_UID=$(awk -F: '{if ($3==0) print $1}' /etc/passwd) ; echo $SUPERUSUARIOS_UID

SUPERUSUARIOS_GID=$(awk -F: '{if ($3==0) print $1}' /etc/passwd) ; echo $SUPERUSUARIOS_GID

--------------
Check status of RAM and Swap memory:
--------------

MEM_TOTAL=$(free -h | sed '1 d' | grep Mem: | awk '{print $2}') ; echo $MEM_TOTAL

MEM_USADA=$(free -h | sed '1 d' | grep Mem: | awk '{print $3}') ; echo $MEM_USADA

MEM_LIBRE=$(free -h | sed '1 d' | grep Mem: | awk '{print $4}') ; echo $MEM_LIBRE

MEM_COMPARTIDA=$(free -h | sed '1 d' | grep Mem: | awk '{print $5}') ; echo $MEM_COMPARTIDA

MEM_ALMACENADA=$(free -h | sed '1 d' | grep Mem: | awk '{print $6}') ; echo $MEM_ALMACENADA

MEM_CACHEADA=$(free -h | sed '1 d' | grep Mem: | awk '{print $7}') ; echo $MEM_CACHEADA

SWAP_TOTAL=$(free -h | sed '1 d' | grep Swap: | awk '{print $2}') ; echo $SWAP_TOTAL

SWAP_USADA=$(free -h | sed '1 d' | grep Swap: | awk '{print $3}') ; echo $SWAP_USADA


SWAP_LIBRE=$(free -h | sed '1 d' | grep Swap: | awk '{print $4}') ; echo $SWAP_LIBRE

————————————————————————
Check Status of partitions / mount point of a SATA Disk:
————————————————————————

PART1_TOTAL=$(df -h | sed '1 d' | grep /dev/sda5 | awk '{print $2}') ; echo $PART1_TOTAL

PART1_USADO=$(df -h | sed '1 d' | grep /dev/sda5 | awk '{print $3}') ; echo $PART1_USADO

PART1_DISPONIBLE=$(df -h | sed '1 d' | grep /dev/sda5 | awk '{print $4}') ; echo $PART1_DISPONIBLE

PART1_PORCENTAJE=$(df -h | sed '1 d' | grep /dev/sda5 | awk '{print $5}') ; echo $PART1_PORCENTAJE

PART1_PUNTOMONTAJE=$(df -h | sed '1 d' | grep /dev/sda5 | awk '{print $6}') ; echo $PART1_PUNTOMONTAJE

—————————————————————
Check Average System Load (Queued Processes):
—————————————————————

CARGA_1MIN=$(top -n 1 -b | grep "load average:" | awk '{print $10}' | sed 's/,//2') ; echo $CARGA_1MIN

CARGA_5MIN=$(top -n 1 -b | grep "load average:" | awk '{print $11}' | sed 's/,//2') ; echo $CARGA_5MIN

CARGA_15MIN=$(top -n 1 -b | grep "load average:" | awk '{print $12}' | sed 's/,//2') ; echo $CARGA_15MIN

CARGA_1MIN=$(uptime | awk '{print $8}' | sed 's/,//2') ; echo $CARGA_1MIN

CARGA_5MIN=$(uptime | awk '{print $9}' | sed 's/,//2') ; echo $CARGA_5MIN

CARGA_15MIN=$(uptime | awk '{print $10}' | sed 's/,//2') ; echo $CARGA_15MIN

-----------------
Check Zombies processes in the Operating System:
-----------------

PROC_ZOMBIE=$(top -n 1 -b | grep "zombie" | awk '{print $10}') ; echo $PROC_ZOMBIE

—————————————————————
Check Total Work Time (Start / On):
—————————————————————

TIEMPO_ENCENDIDO=$(uptime | awk '{print $3,$4}' | cut -f1 -d,) ; echo $TIEMPO_ENCENDIDO

---------------
Check Video card parameters:
---------------

============

Manufacturer:

FAB_TVIDEO=$(lspci -v | grep "VGA" | cut -d " " -f05) ; echo $FAB_TVIDEO

============

RAM:

MEM_TVIDEO=$(lspci -v -s `lspci | awk '/VGA/{print $1}'` | sed -n '/Memory.*, prefetchable/s/.*\[size=\([^]]\+\)M\]/\1/p') ; echo $MEM_TVIDEO

================

Module (Driver):

DRV_TVIDEO=$(lspci -nnk | grep -i vga -A3 | grep 'in use' | cut -d " " -f05) ; echo $DRV_TVIDEO

===============

3D acceleration:

A3D_TVIDEO=$(glxinfo | grep "direct rendering: Yes" | awk '{print $3}') ; echo $A3D_TVIDEO

--------------
Check Processor (CPU) parameters:
--------------

===========

Manufacturer:

FABRICANTE_CPU=$(grep "vendor_id" /proc/cpuinfo | sed q | awk '{print $3}') ; echo $FABRICANTE_CPU

=======

Model:

MODELO_CPU=$(grep "model name" /proc/cpuinfo | sed q | cut -d ":" -f 2 | awk '{print $0}') ; echo $MODELO_CPU

=========

Quantity:

NUM_CPU=$(grep "processor" /proc/cpuinfo | sort -r | sed q | awk '{print $3}') ; TOTAL_CPU=$((`expr $NUM_CPU + 1`)) ; echo $TOTAL_CPU

================

Cores per CPU:

NUCLEO_CPU=$(grep "cpu cores" /proc/cpuinfo | sed q | awk '{print $4}') ; echo $NUCLEO_CPU

==========================

Total Cores per CPUs:

NUM_CPU=$(grep "processor" /proc/cpuinfo | sort -r | sed q | awk '{print $3}') ; TOTAL_CPU=$((`expr $NUM_CPU + 1`)) ; NUCLEO_CPU=$(grep "cpu cores" /proc/cpuinfo | sed q | awk '{print $4}') ; TOTAL_NUCLEO_CPU=$((`expr $TOTAL_CPU \* $NUCLEO_CPU`)) ; echo $TOTAL_NUCLEO_CPU

======================

CPU cache memory:

CACHE_CPU=$(grep "cache size" /proc/cpuinfo | sed q | cut -d ":" -f 2 | awk '{print $0}') ; echo $CACHE_CPU

I hope these little ones "Tips" make it easy for them to perform basic but useful optimizations, which are generally only reserved for experts in Technology, Computing, Free Software and GNU / Linux.

SAMPLE SCREENS

jalbert: bash - Konsole_008

jalbert: bash - Konsole_009

REMINDER: If any command line fails to execute or displays the correct value, be sure to manually test each section of the command prompt to try and adjust the values ​​or syntax of some variables.


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.

  1.   Eduardo said

    Thank you very much.

    Very useful.

  2.   HO2Gi said

    Very good, thank you.

  3.   Jose Albert said

    A pleasure to collaborate! Soon I will mount many more on the use of Shell Scripting in very practical cases.

  4.   cr0t0 said

    Very good engineer! Hopefully there are more Shell Scripting articles.

  5.   Francis Tovar said

    The commands shown there are excellent and very useful.

  6.   userarch said

    Thank you for the valuable information; but in my case some commands do not give the expected result, for example the command "who" and "w" do not show anything; This has happened to me since I made some system update (I use archlinux with screen manager "lxdm" and graphical environment "xfce 4.12"). Any idea what happens (the result is the same even if I use user root).
    Thank you.

  7.   Jose Albert said

    They would like an expert System made with Shell Scripting that upon request will generate the extraction of all the parameters of the system in the form of a Report.

    An example of what can be done with Shell Scripting:

    LPI-SB8 Test ScreenCast (LINUX POST INSTALL - SCRIPT BICENTENARIO 8.0.0)
    (lpi_sb8_adaptation-audiovisual_2016.sh / 43Kb)

    See Screencast: https://www.youtube.com/watch?v=cWpVQcbgCyY