Simple script with progress bar

Discussion in 'Programming/Scripts' started by Poliman, Feb 21, 2018.

  1. Poliman

    Poliman Member

    I have script for doing some thing which I put in dobackup() function:
    Code:
    #!/bin/bash
    
    function progress(){
      echo -ne "Please wait\n"
      while true
      do
        echo -n "#"
        sleep 3
      done
    }
    
    function dobackup()
    {
        tar -cvf dupatest.tar dupatest >/dev/null 2>&1
    }
    
    # Start it in the background
    progress &
    
    # Save progress() PID
    # You need to use the PID to kill the function
    MYSELF=$!
    
    # Start backup
    # Transfer control to dobackup()
    dobackup
    
    # Kill progress
    kill $MYSELF > /dev/null 2>&1
    
    echo -n "done"
    
    Problem is that in line --> kill $MYSELF > /dev/null 2>&1. Not working part with redirection to /dev/null, because I got in console, for example:
    Code:
    /plik: line 32: 110509 Terminated              progress
    
    There shouldn't be anything.
     
  2. Poliman

    Poliman Member

    Code:
    #!/bin/bash
    
    #Script for removing trashes after deinstalled old kernels by apt-get autoremove
    
    function progressBar()
    {
        echo -ne "Please wait\n"
        while true
        do
            echo -n "#"
            sleep 2
        done
    }
    
    
    function clearOldKernel()
    {
        x=$(dpkg --list | grep -i linux-image | grep ^rc | awk '{print $2}')
    
        #if [ ${#x[@]} -gt 0 ]
        if [ -z "$x" ]
        then
            echo "Your system has not any old kernels to deletion."
        else
            echo "Below is the list of old kernels:"
            echo " "
            echo "$x"
            echo " "
            for plik in $x
            do
                echo "Do you want remove $plik? (answer <yes> or hit enter if not sure)"
                read answer
                if [ "$answer" = "yes" ]
                then
                    echo "Deleting file $plik":
                    apt-get --purge remove $plik
                    echo -e "$plik deleted \n"
                else
                    echo "You wasn't sure you want remove old kernels."
                    exit 1
                fi
            done
        fi
        echo
        echo "-------------------------------------------------------------- "
        echo "I have done my job, cya later."
    }
    
    #Start in background
    progressBar &
    
    
    #Save PID progressBar to variable
    MYSELF=$!
    
    #call clearing function
    clearOldKernel
    
    #kill progressBar function
    kill $MYSELF > /dev/null 2>&1
    It clears like it should but I have a problem with progress bar. I would like to show progress bar only when file is deleting. Currently when script asks about some action, user sees started showing signs '#'. Moreover I think that progressBar function is killed, because use "jobs" command does not show any background task.
     
    Last edited: Mar 16, 2018

Share This Page