2017-04-18 168 views
1

我对BASH很陌生,我想知道如何在同一行中打印两个字符串。BASH在相同的两行上打印两个字符串

我想要做的是在BASH中创建一个2行的进度条。 创建1号线的进度条是相当容易的,我不喜欢这样写道:

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

但现在我试图做同样的事情,但与2号线,以及一切我试过到目前为止还没有。

在第二行中,我想放置一个“进度详细信息”,告诉我它在脚本中的哪个位置,例如:正在收集哪个变量,正在运行哪个功能。但我似乎无法创建一个2行的进度条。

+0

对不起朋友,但我不认为你可以做到这一点。但为什么不考虑把进度放在同一行 – sjsam

+0

[如何将进度条添加到shell脚本?](http://stackoverflow.com/questions/238073/how-to-add-a-progress -bar-shell-script) –

+0

@djm不,只包含单行进度条。这是专门询问多条线路。 – tripleee

回答

0

您可以使用\033[F转到上一行,并使用\033[2K删除当前行(以防输出长度发生变化)。

这是剧本我所做的:

echo -en 'Progress: ###   - 33%\r' 
echo -en "\ntest" # writes progress detail 
echo -en "\033[F\r" # go to previous line and set cursor to beginning 

echo -en 'Progress: #######  - 66%\r' 
echo -en "\n\033[2K" # new line (go to second line) and erase current line (aka the second one) 
echo -en "test2"  # writes progress detail 
echo -en "\033[F\r" # go to previous line and set cursor to beginning 

echo -en 'Progress: ############ - 100%\r' 
echo -en "\n\033[2K" # new line and erase the line (because previous content was "test2", and echoing "test" doesn't erase the "2") 
echo -en "test"  # write progress detail 
echo -en '\n' 
1

有可能使用tputprintf覆盖双线路,例如:

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" 

一行代码:

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" 

循环调用th e status功能在特定时间显示适当的文本。

输出结果将类似于:

Status Completing 70% 
============== 
相关问题