I want to make a short script, just for experiment purposes. For example, I run a command such as
sudo apt-get install eclipse --yes
and instead of displaying the verbose of the command while its installing it, display a loading bar like ...... (dots just popping up while it loads or something)
I tried doing something like
apt=sudo apt-get install vlc --yes
start()
{
$apt
while $apt;
do
echo -n "."
sleep 0.5
done
}
start
(what I intended to do was to run the $apt variable and then make it move on to the while loop and the while loop will determine if the command is running, so while the command is running it will replace the verbose with dots)
apt-get install vlc --yes >/tmp/apt-get.log & # Run in background, with output redirected
pid=$! # Get PID of background command
while kill -0 $pid # Signal 0 just tests whether the process exists
do
echo -n "."
sleep 0.5
done
Put the above in a script and run it via sudo
. You can't use kill
to test the sudo
process itself, because you can't send signals to a process with a different uid.
Here's a small variation on the ones above...
spinner()
{
local pid=$!
local delay=0.75
local spinstr='...'
echo "Loading "
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf "%s " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b"
done
printf " \b\b\b\b"
}
with usage:
(a_long_running_task) &
spinner
This prints out
Loading ...
Loading ....
Loading .....
Loading ......
On the same line of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With