De Wiki inetshell
Saltar a: navegación, buscar

Obtener directorio del script[editar]

export SCRIPT_HOME="$( cd "$(dirname "${0}")" ; pwd -P )"

Leer linea por linea de un archivo[editar]

https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/

#!/bin/bash
file="/home/vivek/data.txt"
while IFS= read -r line
do
        # display $line or do somthing with $line
	printf '%s\n' "$line"
done <"$file"

Leer entrada de usuario[editar]

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

echo -n "Enter your name and press [ENTER]: "
read name

Sustituir variables de entorno en archivos[editar]

https://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf

export VAR1=data
envsubst < file.template > file.conf

Sumar enteros en lineas con AWK[editar]

https://stackoverflow.com/questions/450799/shell-command-to-sum-integers-one-per-line

awk '{s+=$1} END {print s}' mydatafile

Revisar si string es numérico[editar]

https://stackoverflow.com/questions/2210349/bash-test-whether-string-is-valid-as-an-integer

if [ -z "${INPUT_STRING//[0-9]}" ]; then
  # yes, natural number
else
  # no, has non-numeral chars
fi

Eliminar parte de las variables en BASH[editar]

AAA="/tmp/tmp.6ksHVXGxzs/fifo/extraccion_20160101.dat"
echo $AAA
/tmp/tmp.6ksHVXGxzs/fifo/extraccion_20160101.dat
echo ${AAA##*/}
extraccion_20160101.dat

Activar interrupciones[editar]

https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash

#!/bin/bash

# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

function ctrl_c() {
        echo "** Trapped CTRL-C"
}

for i in `seq 1 5`; do
    sleep 1
    echo -n "."
done

Parameter expansions[editar]

http://wiki.bash-hackers.org/syntax/pe

Instalar paquetes de una lista[editar]

https://stackoverflow.com/questions/12806176/checking-for-installed-packages-and-if-not-found-install

packageList="git gcc python-devel"

for packageName in $packageList; do
  rpm --quiet --query $packageName || sudo yum install -y $packageName
done

Comparar horas[editar]

https://stackoverflow.com/questions/8903239/how-to-calculate-time-difference-in-bash-script

START=$(date +%s);
sleep 1; # Your stuff
END=$(date +%s);
echo $((END-START)) | awk '{print int($1/60)":"int($1%60)}'

Imprimir texto en colores[editar]

https://misc.flogisoft.com/bash/tip_colors_and_formatting

echo -e "Default \e[39mDefault\e[0m"
echo -e "Default \e[31mRed\e[0m"
echo -e "Default \e[32mGreen\e[0m"
echo -e "Default \e[33mYellow\e[0m"
echo -e "Default \e[34mBlue\e[0m"
echo -e "Default \e[35mMagenta\e[0m"

Comparar booleanos[editar]

https://stackoverflow.com/questions/2953646/how-to-declare-and-use-boolean-variables-in-shell-script

the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
    echo 'Be careful not to fall off!'
fi

Imprimir texto en archivos[editar]

https://stackoverflow.com/questions/23929235/multi-line-string-with-extra-space-preserved-indentation

cat << EndOfMessage
This is line 1.
This is line 2.
Line 3.
EndOfMessage

Generar llave SSH sin entrada de usuario[editar]

function gen_ssh_keys() {
    echo "Generating SSH key in ${SSH_KEY_FILE}"
    COMMENT="SSH Key generated at $(date +"%F %T")"
    ssh-keygen -q -N '' -C "${COMMENT}" -t ecdsa -f "${SSH_KEY_FILE}"
}

SSH_KEY_FILE = /root/key"
gen_ssh_keys


Crear passwords aleatorios[editar]

https://www.commandlinefu.com/commands/matching/random-password/cmFuZG9tIHBhc3N3b3Jk/sort-by-votes

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 32

Obtener IP en CentOS7[editar]

https://askubuntu.com/questions/560412/displaying-ip-address-on-eth0-interface/560415

https://stackoverflow.com/questions/27943059/how-to-get-ip-address-of-a-server-on-centos-7-in-bash

IP=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)