19 – Creación de scripts BASH

CREACIÓN DE ARCHIVO

#!/bin/bash

CREACIÓN DE CÓDIGO FUENTE

PERMISOS

chmod +x FILENAME

EJECUCIÓN

./script.sh

PASO DE PARÁMETROS


echo $1 $2 $4
echo $#
echo $*

VARIABLES

greeting="Welcome"
user=$(whoami)
day=$(date +%A)

ENTRADA / SALIDA

echo : sacar por salida estandar

read : leer de la entrada estandar

LECTURA DE VARIABLES

OPERADORES DE COMPARACIÓN

Description Numeric Comparison String Comparison
Shell comparison example: [ 100 -eq 50 ]; echo $? [ «GNU» = «UNIX» ]; echo $?
less than -lt <
greater than -gt >
equal -eq =
not equal -ne !=
less or equal -le N/A
greater or equal -ge N/A

 

CONDICIONALES

if [ $num_a -lt $num_b ]; then
    echo "$num_a is less than $num_b!"
fi

BUCLES

for i in 1 2 3; do
    echo $i
done
while [ $counter -lt 3 ]; do
    let counter+=1
    echo $counter
done
until [ $counter -lt 3 ]; do
    let counter-=1
    echo $counter
done

FUNCIONES

function total_files {
        find $1 -type f | wc -l
}

function total_directories {
        find $1 -type d | wc -l
}

function total_archived_directories {
        tar -tzf $1 | grep  /$ | wc -l
}

function total_archived_files {
        tar -tzf $1 | grep -v /$ | wc -l
}

ENLACES

https://linuxconfig.org/bash-scripting-tutorial-for-beginners

https://bioinf.comav.upv.es/courses/unix/scripts_bash.html