BASH在同一2行上打印2个字符串 [英] BASH printing 2 strings on the same 2 lines

查看:40
本文介绍了BASH在同一2行上打印2个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对BASH不太熟悉,我想知道如何在相同的2行上打印2个字符串.

我想做的是在BASH中创建2行进度条.创建1行进度条非常容易,我这样做是这样的:

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

但是现在我正在尝试执行相同的操作,但是只有2行,到目前为止,我尝试的所有操作都失败了.

在第二行中,我想添加一个进度详细信息",以告诉我脚本在什么位置,例如:正在收集什么变量,正在运行什么函数.但是我似乎无法创建2行进度条.

解决方案

可以使用 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%
==============

这篇关于BASH在同一2行上打印2个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆