Home Linux & Systems Cybersecurity Cloud & DevOps Networks & Infrastructure SIEM & Monitoring DFIR & Threat Intel Development & Other All categories Projects About Tools

Menu Shell Script

Leer en espanol
Menu Shell Script

Table of contents

Today we are going to have something basic but quite useful. Let's see some examples of options menus in shell script. bash#!/bin/bash #rokitoh bashusage() { cat << EOF USE: $0 -n ===

Good night,

Today we are going to have something basic but quite useful. Let's see some examples of options menus in shell script.

Bash

Bash
#!/bin/bash

#rokitoh
Bash
usage()

{

cat << EOF

USO: $0 -n <OPCION UNO>  -p <OPCION DOS> -h <OPCION TRES>
text
Ejemplo menú de opciones
text
OPCIONES:

-n      OPCION UNO

-m      OPCION DOS

-p      OPCION TRES
Bash
EOF

}

#Declaramos todas las variables que se van a usar para guardar los parametros

uno=

dos=

tres=

#Usamos el getopts para guardar los parametros en variables

while getopts “n:m:p:” OPTION
Bash
do

case $OPTION in
text
n)

uno=$OPTARG

;;

m)

dos=$OPTARG

;;
text
p)

tres=$OPTARG

;;
Bash
esac

done
text
#Comprobamos que se han introducido los parametros obligatorios
Bash
if [ -z $uno]  || [ -z $dos ] ||  [ -z $tres ];

then

usage

exit 1
Bash
fi

Another example:

Bash
#!/bin/bash
text
#rokitoh
text
clear
Bash
while :
text
do
php
echo " 				Escoja una opcion "
php
echo "1. 				Quien hay conectado?"
php
echo "2. 				ver FS"
php
echo "3. 				ver sistema"
php
echo "4. 				Salir"
Bash
echo -n "Seleccione 				una opcion [1 - 4]"
text
read opcion
Bash
case $opcion in
text
1) w;;
text
2) df 				-h;;
text
3) uname -r;;
text
4) exit 1;;
php
*) echo "$opc 				ERROR. opción invalida";
php
echo "Presiona 				una tecla para continuar...";
text
read foo;;
text
esac
Bash
done

Greetings, rokitoh!

Comments