Bash脚本,命令结束后更新输出 [英] Bash script, update output once command is ended

查看:60
本文介绍了Bash脚本,命令结束后更新输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了不同的方法,但是到目前为止,它们都没有奏效.

I have tried different ways but none of them have worked so far.

echo "Starting"

checklocation(){
if (command blabla)

then locationOne=$"Found"

else locationOne=$"Not found"

fi

}
checklocation &

echo "Let's check: " $locationOne

echo "Ending"

由于我的命令需要很长时间才能提供结果,因此我想继续打印所有输出并在结果准备好后显示$ locationOne的值.下面的代码可以很好地一次打印所有输出,但是不会出现$ locationOne.我也尝试过printf和\ r却没有运气.有什么建议吗?

As my command take long time to provide the results I'd like to proceed to print all the output and show the value of $locationOne once the result is ready. The following code works fine printing all the output at once however the $locationOne doesn't appear. I tried with printf and \r too without luck. Any suggestions?

为澄清起见,我想在命令完成后将变量值加载到箭头所指的位置

To clarify, I would like to load the variable value where the arrows are pointing once the command completes

推荐答案

您想返回并稍后修改屏幕输出.

You want to go back and amend screen output later.

在一般情况下这很困难,但是您可以通过确保编写的输出不会以难以预测必须在何处进行修改的方式滚动屏幕来显着简化此操作.

This is very difficult in the general case, but you can simplify it dramatically by making sure that the output you write doesn't scroll the screen in such a way that it's hard to predict where the amendment will have to be made.

此示例通过首先清除屏幕来完成此操作,因此任何多余的输出都不太可能滚动.然后可以按坐标更新:

This example does it by clearing the screen first, so that any extra output is unlikely to scroll. It can then update by coordinates:

#!/bin/bash

check() {
  sleep 3
  tput sc        # Save cursor
  tput cup 1 14  # Set y,x coordinates
  printf '%s' "Found"
  tput rc        # Restore cursor
}
check &
clear # Clear screen so we know where we are
echo "Starting"
echo "Let's check: "
echo "Ending"
wait

这表明:

Starting
Let's check:  
Ending

三秒钟,然后将其更新为:

for three seconds, then it updates it to:

Starting
Let's check:  Found
Ending

替代方法包括:

  • 将要在屏幕上显示的数据保留为一个字符串,并在每次要更新全屏显示时清除并写入
  • 跟踪您编写的行数,然后使用向上光标" ANSI命令( tput cuu )移至您认为要修改的行.
  • Keeping the data you want on screen in a string, and just clearing+writing whenever you want to update the full screen
  • Tracking the number of lines you write, then using the "cursor up" ANSI command (tput cuu) to move up to where you believe the line to amend will be.

这篇关于Bash脚本,命令结束后更新输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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