且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

BASH在同一2行上打印2个字符串

更新时间:2023-12-04 12:00:58

可以使用 tput printf 覆盖双行,例如:

 功能状态(){[[$ i -lt 10]]&&printf"\ rStatus同步%0.0f""$((i * 5))";[[$ i -gt 10]]&&printf"\ rStatus正在完成%0.0f""$((i * 5))";printf"%% \ n";}为{1..20}中的i做状态printf%0.s =" $(seq $ i);睡.25;线程cuu1;tput el;完毕 ;printf"0 %% \ n";printf%.0s" {1..20};printf"\ rdone.\ n" 

单线:

  for {1..20}中的i;做身份;printf%0.s =" $(seq $ i);睡.25;线程cuu1;tput el;完毕 ;printf"0 %% \ n";printf%.0s" {1..20};printf"\ rdone.\ n" 

循环调用 status 函数以在特定时间显示适当的文本.

结果将类似于:

 状态完成70%============== 

I'm rather new to BASH and I was wondering how could I print 2 strings on the same 2 lines.

What I'm trying to do, is create a 2 line progress-bar in BASH. Creating 1 line progress bar is rather easy, I do it like this:

echo -en 'Progress: ###          - 33%\r'
echo -en 'Progress: #######      - 66%\r'
echo -en 'Progress: ############ - 100%\r'
echo -en '\n'

But now I'm trying to do the same thing but with 2 lines, and everything I tried failed so far.

In the second line, I want to put a "Progress Detail" that tells me at what point in the script it is, like for example: what variable is gathering, what function is it running. But I just can't seem to create a 2 line progress bar.

It's possible to overwrite double lines using tput and printf, for example:

function status() { 
    [[ $i -lt 10 ]] && printf "\rStatus Syncing %0.0f" "$(( i * 5 ))" ; 
    [[ $i -gt 10 ]] && printf "\rStatus Completing %0.0f" "$(( i * 5 ))" ;
    printf "%% \n" ;
}

for i in {1..20}
do status
    printf "%0.s=" $(seq $i) ; 
    sleep .25 ; tput cuu1 ; 
    tput el ; 
done ; printf "0%%\n" ; printf " %.0s" {1..20} ; printf "\rdone.\n"

one-liner:

for i in {1..20}; do status ; printf "%0.s=" $(seq $i) ; sleep .25 ; tput cuu1 ; tput el ; done ; printf "0%%\n" ; printf " %.0s" {1..20} ; printf "\rdone.\n"

The loop calls the status function to display the appropriate text during a particular time.

The resulting output would be similar to:

Status Completing 70%
==============