Linux shell, displays something while the team is running in the background

I want to make a short script, just for experimentation. For example, I run a command, for example

sudo apt-get install eclipse --yes 

and instead of displaying a detailed command when installing it, display the download panel, for example ...... (dots only appear when they load or something like that)

I tried to do 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 run the $ apt variable and then make it go to the while loop, and the while loop will determine if the command is running, so it replaces the verbose points during the execution of the command)

+4
source share
3 answers

Here's a slight variation on the 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" } 

using:

 (a_long_running_task) & spinner 

Will print

Loading...

Loading....

Loading.....

Loading...

On the same line, of course.

+3
source
 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 value in a script and run it through sudo . You cannot use kill to test the sudo process itself, because you cannot send signals to the process with a different uid.

+6
source

Whiptail is a tool for this. It's pretty simple to make it display a progress bar or other information for you while your task is complete.

In fact, it is a tool used by Debian and many other distributions, in the same context that you use.

Here's a simplified version of the code we use to simplify the installation of aptitude:

 pkg=0 setterm -msg off # Disable kernel messages to this terminal setterm -blank 0 # Disable screen blanking aptitude -y install <list of packages> | \ tr '[:upper:]' '[:lower:]' | \ while read x; do case $x in *upgraded*newly*) u=${x%% *} n=${x%% newly installed*} n=${n##*upgraded, } r=${x%% to remove*} r=${r##*installed, } pkgs=$((u*2+n*2+r)) pkg=0 ;; unpacking*|setting\ up*|removing*\ ...) if [ $pkgs -gt 0 ]; then pkg=$((pkg+1)) x=${x%% (*} x=${x%% ...} x=$(echo ${x:0:1} | tr '[:lower:]' '[:upper:]')${x:1} printf "XXX\n$((pkg*100/pkgs))\n${x} ...\nXXX\n$((pkg*100/pkgs))\n" fi ;; esac done | whiptail --title "Installing Packages" \ --gauge "Preparing installation..." 7 70 0 setterm -msg on # Re-enable kernel messages invoke-rc.d kbd restart # Restore screen blaking to default setting 
+1
source

Source: https://habr.com/ru/post/1489602/


All Articles