Bash: Numbering the processing command

Submitted by admin on Thu, 04/18/2019 - 17:29

Define a function echo_count and count the function name to be called in the script. You can refer to a variable ${TOTAL} .For example,
#!/bin/bash

export TOTAL=$(( $(grep '^echo_count' $0 | wc -l) ))
export COUNT=1

function echo_count () {

echo -n "($(( COUNT++ ))/${TOTAL}) $1"
}

echo
echo_count 'Processing one - Stopping Apache2... '
sudo service apache2 stop
echo 'Done'

echo_count 'Processing two - '
echo 'Apache2 is not working.'

echo_count 'Processing three - Starting Apache2... '
sudo service apache2 start
echo 'Done'

echo_count 'Processing four - '
echo 'Apache2 is running.'

echo
echo 'Done.'
echo

Output result:

(1/4) Processing one - Stopping Apache2... Done
(2/4) Processing two - Apache2 is not working.
(3/4) Processing three - Starting Apache2... Done
(4/4) Processing four - Apache2 is running.

Done.

Tags