Skip to content

Instantly share code, notes, and snippets.

@felix-orduz
Created September 25, 2014 19:10
Show Gist options
  • Select an option

  • Save felix-orduz/9eac4bf24cd084048f02 to your computer and use it in GitHub Desktop.

Select an option

Save felix-orduz/9eac4bf24cd084048f02 to your computer and use it in GitHub Desktop.
#!/bin/bash
number_files=0;
size_per_file='';
level_log=3;
name_file='log';
###########
# Options #
###########
while getopts "n:c:s:" opts; do
case $opts in
n) name_file=$OPTARG;;
c) number_files=$OPTARG;;
s) size_per_file=$OPTARG;;
?) exit 1;;
esac
done
shift $(($OPTIND-1))
#############
# Functions #
#############
log() {
local level=${1?}
shift
now=$(date +"%m/%d/%Y %H:%M:%S")
local code= line="$*"
if [ ! -z $log_path ]; then
echo "[$now] $level: $line" >> $log_path
fi;
if [ -t 2 ]
then
case "$level" in
INFO) code=36 ;;
DEBUG) code=30 ;;
WARN) code=33 ;;
ERROR) code=31 ;;
*) code=37 ;;
esac
#nivel error
if [ $level_log -eq 3 ]; then
echo -e "\e[${code}m[$now] $level: \e[0m${line}"
fi
if [[ ($level_log -eq 2 ) && ( $code -eq 33 || $code -eq 36 ) ]]; then
echo -e "\e[${code}m[$now] $level: \e[0m${line}"
fi
if [[ ($level_log -eq 1 ) && ( $code -eq 36 ) ]]; then
echo -e "\e[${code}m[$now] $level: \e[0m${line}"
fi
else
echo "[$now] $level: $line"
fi >&2
}
#Function for stderr
logger(){
while read line; do
log "ERROR" $line
done
}
###############
# Validations #
###############
if [ $number_files -eq 0 ]; then
log "ERROR" "Debe Especificar la cantidad de archivos a crear: con la opcion -c"
exit 0;
fi;
if [ -z $size_per_file ]; then
log "ERROR" "Debe Especificar el tamaño del archivo a crear: con la opcion -s"
exit 0;
fi;
if [ -z $name_file ]; then
log "ERROR" "Debe Especificar un nombre para los archivos: con la opcion -n"
exit 0;
fi;
i=0;
while [ $i -lt $number_files ]; do
dd if=/dev/urandom of=$name_file$i.log bs=$size_per_file count=1
echo $i;
i=$(( $i + 1 ));
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment