I'm trying to write a script that parses a file [output.txt], each line has a 0 or 1, and the script will loop each entry in the file and display # if 1, and . if 0, which works with the below code, outputting progress as ###..##.....##.#.#.:
#!/bin/bash
entries=`cat input.txt | wc -l`
currentbar=""
while read line; do if [[ "$line" == "1" ]]; then let "parse_resp++" reqstatus="#" bar="$currentbar$reqstatus" currentbar=$bar echo -ne "$bar\r" sleep 0.1 else let "parse_resp++" reqstatus="." bar="$currentbar$reqstatus" currentbar=$bar echo -ne "$bar\r" sleep 0.1 fi
done < input.txt
echo -ne "\n"- I would like the length of the bar to be fixed and based on the number of lines (if the line is not parsed already, it will show a
-), expecting to get:###..##...--------- - The
\rbelow does not seem to work to write on top of the current line , as it outputs:
(I tried changing-to.but it has the same problem)#-----------------#----------------#---------------.--------------.-------------#------------#-----------.----------.---------.--------.-------.------#-----#----.---#--.-#.#!/bin/bash entries=`cat input.txt | wc -l` currentbar="" echo "" while read line; do if [[ "$line" == "1" ]]; then let "parse_resp++" nbleft=`echo "$entries - $parse_resp" | bc` dashleft=`seq -s- $nbleft|tr -d '[:digit:]'` reqstatus="#" bar="$currentbar$reqstatus$dashleft" currentbar=$bar echo -ne "$bar\r" else let "parse_resp++" nbleft=`echo "$entries - $parse_resp" | bc` dashleft=`seq -s- $nbleft|tr -d '[:digit:]'` reqstatus="." bar="$currentbar$reqstatus$dashleft" currentbar=$bar echo -ne "$bar\r" fi done < input.txt echo -ne "\n"
1 Answer
The \r works fine, but you're storing the entire old bar in $currentbar – including $dashleft – and you just keep appending on every iteration.
You should probably use currentbar+=$reqstatus.