Josep Garcia

function

Texto aleatorio en bash

Con este script podemos crear un texto aleatorio en bash, genera un texto tan largo como el número que le pasemos como parámetro, si no le pasamos nada el texto resultante será de 2 caracteres.

#!/bin/bash
function textoAleatorio () {
	LETRAS=( a b c d e f g h i j k l m n o p q r s t u v w x y z 
		 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )
	TOTAL_LETRAS=${#LETRAS[*]} 
	y=0
	if [ $1 ]; then
		max=$1
	else
		max=2
	fi
	while [ $y -lt $max ]; do
	    x=$(($RANDOM%500)) 
    	    index=$(($RANDOM%$TOTAL_LETRAS)) 
	    echo -n ${LETRAS[$index]}
	    ((y++)) 
	done
}
textoAleatorio $1

Lo guardamos como aleatorio.sh y lo ejecutamos:
$ sh aleatorio.sh 5
lfEJe
$ sh aleatorio.sh 15
VJYFDkGcSDLvDei
$ sh aleatorio.sh 55
zMCnlrPDvTKxHOeiRTOxsXfSqIiUsoCuyPCrwtyRGXAhLmWbAhJgGgm
$ sh aleatorio.sh
xA

Categorías

LinuxAndroidBash ScriptingDrupal

Twitter

Twitter Josep Carlet