Created
January 20, 2016 19:33
-
-
Save felix-orduz/c87061cd8c6b76ebc213 to your computer and use it in GitHub Desktop.
Generate Random Files in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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; | |
| if ! type fallocate > /dev/null; then | |
| cmd="dd if=/dev/urandom bs=$size_per_file count=1 of=$name_file" | |
| else | |
| cmd="fallocate -l $size_per_file $name_file" | |
| fi | |
| while [ $i -lt $number_files ]; do | |
| exe="$cmd$i.log" | |
| echo "$exe" | |
| (eval $exe) | |
| let i=i+1 | |
| done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment