Ημερήσια αρχεία: 14 Οκτωβρίου 2016


convertAndPrintSeconds – Convert seconds to minutes, hours and days in bash 1

The following code can be used to convert some time in seconds to minutes, hours and days in bash.
It will print on screen the converted values only if they are not 0. i.e If the resulting days is 0, it will not print the text for days at all.
You can use it in any script without copy pasting everything in it by executing the following command source ./convertAndPrintSeconds.sh.
Doing so, it will load to your script the function that is defined in convertAndPrintSeconds.sh, making it available for you to use (something like including code in C, with some caveats).

[download id=”2118″]


#!/bin/bash

convertAndPrintSeconds() {

    local totalSeconds=$1;
    local seconds=$((totalSeconds%60));
    local minutes=$((totalSeconds/60%60));
    local hours=$((totalSeconds/60/60%24));
    local days=$((totalSeconds/60/60/24));
    (( $days > 0 )) && printf '%d days ' $days;
    (( $hours > 0 )) && printf '%d hours ' $hours;
    (( $minutes > 0 )) && printf '%d minutes ' $minutes;
    (( $days > 0 || $hours > 0 || $minutes > 0 )) && printf 'and ';
    printf '%d seconds\n' $seconds;
}

Usage

Function convertAndPrintSeconds accepts one parameter, the positive integer number that represents the seconds count we want to convert.

Example


$ source ./time.sh 
$ convertAndPrintSeconds 10
10 seconds
$ convertAndPrintSeconds 100
1 minutes and 40 seconds
$ convertAndPrintSeconds 1000
16 minutes and 40 seconds
$ convertAndPrintSeconds 10000
2 hours 46 minutes and 40 seconds
$ convertAndPrintSeconds 100000
1 days 3 hours 46 minutes and 40 seconds
$ convertAndPrintSeconds 1000000
11 days 13 hours 46 minutes and 40 seconds

cecho – a function to print using different colors in bash

The following script can be used to print colored text in bash.
You can use it in any script without copy pasting everything in it by executing the following command source cecho.sh.
Doing so, it will load to your script the functions that are defined in cecho.sh, making them available for you to use (something like including code in C, with some caveats).

[download id=”2113″]

#!/bin/bash

# The following function prints a text using custom color
# -c or --color define the color for the print. See the array colors for the available options.
# -n or --noline directs the system not to print a new line after the content.
# Last argument is the message to be printed.
cecho () {

    declare -A colors;
    colors=(\
        ['black']='\E[0;47m'\
        ['red']='\E[0;31m'\
        ['green']='\E[0;32m'\
        ['yellow']='\E[0;33m'\
        ['blue']='\E[0;34m'\
        ['magenta']='\E[0;35m'\
        ['cyan']='\E[0;36m'\
        ['white']='\E[0;37m'\
    );

    local defaultMSG="No message passed.";
    local defaultColor="black";
    local defaultNewLine=true;

    while [[ $# -gt 1 ]];
    do
    key="$1";

    case $key in
        -c|--color)
            color="$2";
            shift;
        ;;
        -n|--noline)
            newLine=false;
        ;;
        *)
            # unknown option
        ;;
    esac
    shift;
    done

    message=${1:-$defaultMSG};   # Defaults to default message.
    color=${color:-$defaultColor};   # Defaults to default color, if not specified.
    newLine=${newLine:-$defaultNewLine};

    echo -en "${colors[$color]}";
    echo -en "$message";
    if [ "$newLine" = true ] ; then
        echo;
    fi
    tput sgr0; #  Reset text attributes to normal without clearing screen.

    return;
}

warning () {

    cecho -c 'yellow' "$@";
}

error () {

    cecho -c 'red' "$@";
}

information () {

    cecho -c 'blue' "$@";
}

 

Usage

Function cecho accepts the options to set the color and to control if a new line should be print.
Parameter -c or --color define the color for the print. See the array colors for the available options.
Parameter -n or --noline directs the system not to print a new line after the content.
The last parameter is the string message to be printed.
Functions warning, error and information are using cecho to print in color.
These three functions always print a new line and they have hardcoded one color set for each.

Example


#Get the name of the script currently being executed

scriptName=$(basename $(test -L "$0" && readlink "$0" || echo "$0"));

#Get the directory where the script currently being executed resides

scriptDirDIR=$(cd $(dirname "$0") && pwd);

#Print in blue color with no new line

cecho -n -c 'blue' "$scriptDir";

#Print in red color with a new line following the message

cecho -c 'red' "$scriptName";

#Using the information() function to print in blue followed by a new line

information ‘End of script’;