0

I would like to ask you an small question, if i have this progress bar from 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'

where in this code i can combine in my loop?

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
user404
  • 5
  • 2
  • What kind of progress you want to show? How many lines have been read from the file `server`? – ceving Mar 09 '20 at 09:35
  • the command was just an example.. i want it to show the loop progress after i run it ( some commands are taking time such as cleaning the inodes or naking other things.. ) , and it is about 2 lines, of 2 servers ( server list) – user404 Mar 09 '20 at 09:58

3 Answers3

2

It is everything here already.

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
ceving
  • 21,900
  • 13
  • 104
  • 178
0

You can combine the for loops as such:

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
Bayou
  • 3,293
  • 1
  • 9
  • 22
0

I'd do it this way (see code comments for detail):

#!/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
Léa Gris
  • 17,497
  • 4
  • 32
  • 41