progre.sh icon indicating copy to clipboard operation
progre.sh copied to clipboard

Running with a process?

Open briancroxall opened this issue 5 years ago • 1 comments

Sorry for the question, but how would I actually call this process in the shell when running a script?

briancroxall avatar Oct 21 '20 15:10 briancroxall

Don't actually know if it is what you are searching for, but I edited the script (+ add somecoments) so it pauses the count and updates the percentage every update.

Not the best solution (in any way), but it kind of works. You need to edit the paths of the scripts and the porcentages at which it stops. The program will stop the count until the script finishes.

All outputs are hidden and it runs up to 6 scripts without modifyng nothing but constants. But trust me, is not very efficient.

#!/bin/bash

# Change this 18 values to your needs
# Should have been an array and a for loop, but I'm bored and tired
SCRIPT_1_PATH="/path/to/script1.sh"
SCRIPT_2_PATH="/path/to/script2.sh"
SCRIPT_3_PATH="/path/to/script3.sh"
SCRIPT_4_PATH="/path/to/script4.sh"
SCRIPT_5_PATH="/path/to/script5.sh"
SCRIPT_6_PATH="/path/to/script6.sh"

SCRIPT_1_PRC=10
SCRIPT_2_PRC=26
SCRIPT_3_PRC=32
SCRIPT_4_PRC=37
SCRIPT_5_PRC=52
SCRIPT_6_PRC=86

SCRIPT_1_FLAG=false
SCRIPT_2_FLAG=false
SCRIPT_3_FLAG=true  # It should skip this one
SCRIPT_4_FLAG=false
SCRIPT_5_FLAG=false
SCRIPT_6_FLAG=false

PROGRESS_DELAY=0.075

# ------------------------

# Main function
progreSh() {
    # Defining colors with escape sequences
    PROGRESS_DELAY=0.05
    LR='\033[1;31m'  # Bright Red
    LG='\033[1;32m'  # Bright Green
    LY='\033[1;33m'  # Bright Yellow
    LC='\033[1;36m'  # Bright Cyan
    LW='\033[1;37m'  # Bold White
    NC='\033[0m'     # Restore to default

    # $1 (first argument) is 0, then the time (TME) is "now"
    if [ "${1}" = "0" ]; then
        TME=$(date +"%s");
    fi

    # I have not a single idea of what is this
    SEC=`printf "%04d\n" $(($(date +"%s")-${TME}))`; SEC="$SEC sec"
    PRC=`printf "%.0f" ${1}`  # Uses the argument as the percentage. Should be calculated every time
    SHW=`printf "%3d\n" ${PRC}`
    LNE=`printf "%.0f" $((${PRC}/2))`
    LRR=`printf "%.0f" $((${PRC}/2-12))`; if [ ${LRR} -le 0 ]; then LRR=0; fi;
    LYY=`printf "%.0f" $((${PRC}/2-24))`; if [ ${LYY} -le 0 ]; then LYY=0; fi;
    LCC=`printf "%.0f" $((${PRC}/2-36))`; if [ ${LCC} -le 0 ]; then LCC=0; fi;
    LGG=`printf "%.0f" $((${PRC}/2-48))`; if [ ${LGG} -le 0 ]; then LGG=0; fi;

    LRR_=""
    LYY_=""
    LCC_=""
    LGG_=""

    # Each loop changes one of the 4 colors

    for ((i=1;i<=13;i++))
    do
        DOTS=""; for ((ii=${i};ii<13;ii++)); do DOTS="${DOTS}."; done
        if [ ${i} -le ${LNE} ]; then LRR_="${LRR_}#"; else LRR_="${LRR_}."; fi
        echo -ne "  ${LW}${SEC}  ${LR}${LRR_}${DOTS}${LY}............${LC}............${LG}............ ${SHW}%${NC}\r"
        if [ ${LNE} -ge 1 ]; then update_prc $(($i + $i + 1)); update_tme $(($i + $i + 4)); run_scripts $(($i + $i + 1)); fi
    done
    for ((i=14;i<=25;i++))
    do
        DOTS=""; for ((ii=${i};ii<25;ii++)); do DOTS="${DOTS}."; done
        if [ ${i} -le ${LNE} ]; then LYY_="${LYY_}#"; else LYY_="${LYY_}."; fi
        echo -ne "  ${LW}${SEC}  ${LR}${LRR_}${LY}${LYY_}${DOTS}${LC}............${LG}............ ${SHW}%${NC}\r"
        if [ ${LNE} -ge 14 ]; then update_prc $(($i + $i + 1)); update_tme $(($i + $i + 4)); run_scripts $(($i + $i + 2)); fi
    done
    for ((i=26;i<=37;i++))
    do
        DOTS=""; for ((ii=${i};ii<37;ii++)); do DOTS="${DOTS}."; done
        if [ ${i} -le ${LNE} ]; then LCC_="${LCC_}#"; else LCC_="${LCC_}."; fi
        echo -ne "  ${LW}${SEC}  ${LR}${LRR_}${LY}${LYY_}${LC}${LCC_}${DOTS}${LG}............ ${SHW}%${NC}\r"
        if [ ${LNE} -ge 26 ]; then update_prc $(($i + $i + 1)); update_tme $(($i + $i + 4)); run_scripts $(($i + $i + 3)); fi
    done
    for ((i=38;i<=49;i++))
    do
        DOTS=""; for ((ii=${i};ii<49;ii++)); do DOTS="${DOTS}."; done
        if [ ${i} -le ${LNE} ]; then LGG_="${LGG_}#"; else LGG_="${LGG_}."; fi
        echo -ne "  ${LW}${SEC}  ${LR}${LRR_}${LY}${LYY_}${LC}${LCC_}${LG}${LGG_}${DOTS} ${SHW}%${NC}\r"
        if [ ${LNE} -ge 38 ]; then update_prc $(($i + $i + 1)); update_tme $(($i + $i + 4)); run_scripts $(($i + $i + 4)); fi
    done
}

function update_prc {
    # This just adds 2 to the current value. 50 "." should turn into 50 "#" by the end
    PRC=`printf "%.0f" ${1}`  # Uses the argument as the percentage.
    SHW=`printf "%3d\n" ${PRC}`
}

function update_tme {
    # updates the time
    SEC=`printf "%04d\n" $(($(date +"%s")-${TME}))`; SEC="$SEC sec"
}

function run_scripts {
    CURRENT_PERCENTAGE=$1

    # Runs the scripts at designated percentages
    DUMMY_CUMPUTING_DELAY=$(( ( RANDOM % 20 ) / 10 ))  # Replace by zero

    if [ $CURRENT_PERCENTAGE -ge $SCRIPT_6_PRC ] && [ $SCRIPT_6_FLAG == false ]; then
        bash $SCRIPT_6_PATH > /dev/null 2>&1
        SCRIPT_6_FLAG=true
        sleep $DUMMY_CUMPUTING_DELAY

    elif [ $CURRENT_PERCENTAGE -ge $SCRIPT_5_PRC ] && [ $SCRIPT_5_FLAG == false ]; then
        bash $SCRIPT_5_PATH > /dev/null 2>&1
        SCRIPT_5_FLAG=true
        sleep $DUMMY_CUMPUTING_DELAY

    elif [ $CURRENT_PERCENTAGE -ge $SCRIPT_4_PRC ] && [ $SCRIPT_4_FLAG == false ]; then
        bash $SCRIPT_4_PATH > /dev/null 2>&1
        SCRIPT_4_FLAG=true
        sleep $DUMMY_CUMPUTING_DELAY

    elif [ $CURRENT_PERCENTAGE -ge $SCRIPT_3_PRC ] && [ $SCRIPT_3_FLAG == false ]; then
        bash $SCRIPT_3_PATH > /dev/null 2>&1
        SCRIPT_3_FLAG=true
        sleep $DUMMY_CUMPUTING_DELAY

    elif [ $CURRENT_PERCENTAGE -ge $SCRIPT_2_PRC ]  && [ $SCRIPT_2_FLAG == false ]; then
        bash $SCRIPT_2_PATH > /dev/null 2>&1
        SCRIPT_2_FLAG=true
        sleep $DUMMY_CUMPUTING_DELAY

    elif [ $CURRENT_PERCENTAGE -ge $SCRIPT_1_PRC ] && [ $SCRIPT_1_FLAG == false ]; then
        bash $SCRIPT_1_PATH > /dev/null 2>&1
        SCRIPT_1_FLAG=true
        sleep $DUMMY_CUMPUTING_DELAY

    fi

    # Sleep for now
    sleep $PROGRESS_DELAY
}

echo
progreSh 0; progreSh 100
echo

I hope it helps you. If you want to optimize it, go ahead. I'd love to see use something outher than that scrambled inefficient mess I just made.

2022-03-12 17-40-53

The gif seems to be a bit slower than what I look in the terminal.

ljgonzalez1 avatar Mar 12 '22 20:03 ljgonzalez1