簡體   English   中英

Linux Bash 嘗試編寫清單進度“條”之類的。 替換同一行

[英]Linux Bash Trying to write checklist progress “bar” of sorts. Replace the same line

對不起,如果我沒有給你足夠的信息,這是我第一次在這里發帖。

我正在嘗試在 bash 腳本中進行此操作。

正在下載......

“運行 bash 命令,完成后,將“正在下載...”文本替換為同一行中的文本,即空格。”

下載……完成!

go 到下一行並顯示

安裝......

“再次運行 bash 命令,完成后,將“正在安裝...”文本替換為同一行中的文本,即空格。”

安裝......完成!

我希望你明白我的意思。 提前致謝。

我試過了:

#/bin/bash
tput sc # save cursor
printf "Something that I made up for this string"
sleep 1
tput rc;tput el # rc = restore cursor, el = erase to end of line
printf "Another message for testing"
sleep 1
tput rc;tput el
printf "Yet another one"
sleep 1
tput rc;tput el

但它不會創建新行,它只是使用一行來顯示所有文本。

我假設您從某處提取了tput代碼,並且我猜測“某處”還解釋了tput被用於多次覆蓋同一行(正如您的腳本實際上所做的那樣)。

根據您的描述,聽起來您不需要覆蓋任何行,因此使用tput是錯誤的解決方案。

如果我正確理解您的描述,您應該能夠使用一些(相對)簡單的printf命令來完成您想做的一切,例如:

printf "Downloading .... "                 # no '\n' in the output so cursor remains at end of current line
# run your bash commands here 
printf "DONE!\n"                           # append to end of current line and then add a new line (\n)

printf "Installing .... "                  # no '\n' in the output so cursor remains at end of current line
# run more bash commands here
printf "DONE!\n"                           # append to end of the current line and then add a new line (\n)

請記住,如果您的任何“bash 命令”生成任何output,那么 cursor 將被移動(可能到新行),從而弄亂您的 Z78E6221F6393D1356681DB398F14CEZ6D。 最終結果是您需要確保您的“bash 命令”不會生成任何 output 到 stdout/stderr(或者,確保所有 output - stdout/stderr - 都被重定向到文件)。

如果您的要求是讓“bash 命令”將 output 發送到終端,那么您可能需要 go 重新使用tput ...但這將取決於您希望 Z78E6221F6393D1356D681CEDB 出現的確切方式。

注意:如果(以上)不符合您的要求,請更新問題並提供更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM