【问题标题】:shell command with progress bar带进度条的 shell 命令
【发布时间】:2020-03-09 19:58:56
【问题描述】:

我想问你一个小问题, 如果我有来自https://github.com/fearside/ProgressBar/的这个进度条@

    #!/bin/bash
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function ProgressBar {
# Process data
    let _progress=(${1}*100/${2}*100)/100
    let _done=(${_progress}*4)/10
    let _left=40-$_done
# Build progressbar string lengths
    _fill=$(printf "%${_done}s")
    _empty=$(printf "%${_left}s")

# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"

}

# Variables
_start=1

# This accounts as the "totalState" variable for the ProgressBar function
_end=100

# Proof of concept
for number in $(seq ${_start} ${_end})
do
    sleep 0.1
    ProgressBar ${number} ${_end}
done
printf '\nFinished!\n'

在这段代码中我可以在我的循环中组合到哪里?

for i in `cat server`
do ssh -o "BatchMode=yes" -o StrictHostKeyChecking=no $i " (df -h /var | tr '\n' ',') & (hostname)| tr '\n' ',' " ;echo 2>&1 | tee >> /tmp/check_var2.csv

done

【问题讨论】:

  • 你想展示什么样的进步?从文件server 中读取了多少行?
  • 该命令只是一个示例.. 我希望它在我运行它后显示循环进度(某些命令需要时间,例如清理 inode 或裸露其他东西..),它是大约 2 行,2 个服务器(服务器列表)

标签: bash shell unix


【解决方案1】:

一切都已经在这里了。

Create an array from a text file:

mapfile -t server < server

Take the size of the array:

end=${#server[@]}

Loop over an array:

for (( i=1; i <= end; i++ )); do
  ProgressBar "$i" "$end"
  echo do something
done

【讨论】:

    【解决方案2】:

    您可以像这样组合 for 循环:

    X=0
    SERVERS=($(cat server))
    for i in "${SERVERS[@]}"; do
        ssh -o "BatchMode=yes" -o StrictHostKeyChecking=no $i " (df -h /var | tr '\n' ',') & (hostname)| tr '\n' ',' " ;echo 2>&1 | tee >> /tmp/check_var2.csv
        ((X=X+1))
        ProgressBar ${X} ${#SERVERS[@]}
    done
    

    【讨论】:

      【解决方案3】:

      我会这样做(详见代码 cmets):

      #!/usr/bin/env bash
      
      # Servers file
      declare -r servers='servers'
      
      # Prepare the CSV output file
      declare -r csv_out='/tmp/check_var2.csv'
      rm -f "$csv_out"
      # Print the first CSV line with headers
      echo 'Filesystem,Size,Used,Avail,Use%,Mounted on,Hostname' >"$csv_out"
      
      # Prepare commands script sent to remote servers via ssh
      # Produces CSV data:
      # Filesystem,Size,Used,Avail,Use%,Mounted on,Hostname
      declare -- ssh_script
      IFS= read -r -d '' ssh_script <<'EOF'
      {
        df -h /var | tail -n 1
        hostname
      } | xargs | tr ' ' ','
      EOF
      typeset -r ssh_script
      
      # Fills the servers_array from the servers file
      declare -a serv_array
      IFS=$'\n' read -r -d ' ' -a serv_array <"$servers"
      
      # Get the size of the servers array
      declare -ri serv_size="${#serv_array[@]}"
      
      # Fixed progress-bar version
      progress_bar() {
        local -r width=40
        local bar
        local left
        # Build string lengths
        printf -v bar "%$((${1}*width/${2}))s"
        printf -v left "%$((width-${1}*width/${2}))s"
      
        # Build and print the progress bar
        # Output example: [########################################] 100%
        printf "\rProgress : [%s%s] %d%%" "${bar// /#}" "${left// /-}" $((${1}*100/${2}))
      }
      
      # Iterate index of servers
      for ((i = 0; i < serv_size; i++)); do
        server="${serv_array[i]}"
        ssh \
          -o "BatchMode=yes" \
          -o StrictHostKeyChecking=no \
          "$server" \
          "$ssh_script" \
          >>"$csv_out"
        progress_bar "$((i + 1))" "$serv_size"
      done
      
      echo
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        • 2010-10-25
        • 1970-01-01
        • 2020-11-13
        相关资源
        最近更新 更多