如何在Ubuntu终端上为fstrim显示进度条

1个回答

你可以自己编写一个函数,在等待前一个任务完成时绘制进度条。
# Function to show progbar while running command in the background
show_progbar() {
  # Uses PID of previous command, unless PID is given as 1st parameter
  if [[ -z $1 ]]
  then
    PID=$!
  else
    PID=$1
  fi
  ii=0
  jj=0
  bar="="       # Character to draw
  echo -n " [ "

  # While the process with PID is running, progbar is drawing
  while [[ -d "/proc/$PID" ]]
  do
    printf "%b" "\b${bar}]"
    (( ii++ ))
    sleep .1    # Speed of the progress bar
  done
  (( ii=ii+3 ))

  # Erases the progress bar after it is done
  while [[ $jj -lt $ii ]]
  do
    printf "\b"
    (( jj++ ))
  done
}

这必须在您的shell(或脚本)中定义或来源。要使用它,请运行以下命令:
sudo fstrim -v / & \
show_progbar

(在从shell运行时需要反斜杠进行换行-在脚本中可以省略。)

1看起来很酷。而且可以进行很多微调。 - Rinzwind