Bash脚本:使用手表分别更新屏幕的上半部分

如何解决Bash脚本:使用手表分别更新屏幕的上半部分

我正在处理一个bash脚本,该脚本在屏幕的前16行显示一个随机的ANSI艺术横幅,然后在其余行中显示我的todo.txt列表的项目视图。它打算放在tmux窗口的一个窗格中,以便屏幕的右三分之一(大约80x48个字符)始终显示我的待办事项列表。

现在,我的横幅广告在加载时随机分配,然后在我添加或删除内容时通过fswatch更新待办事项列表。我还想每15分钟左右更新一次横幅,因此我想使用诸如watch之类的命令运行命令,以每15分钟间隔显示一个随机ANSI横幅,但是当使用watch时,该脚本只会使整个屏幕空白。 (下面的代码有10秒的间隔用于测试)

是否有更好的方法来做到这一点,或者有一种让手表开始正确输出横幅的方法?

这是脚本:

#!/bin/bash
clear
banner.sh
/usr/local/bin/todo.sh projectview | fold -w 80 -s

watch -t -c -n 10 banner.sh # this just gives me a blank screen and hangs

fswatch -l 1 -o --event=Updated -e "~/.todo/.*" -i "todo.txt" ~/.todo | while read;
        do
                tput cup 18 0 && tput ed && /usr/local/bin/todo.sh projectview | fold -w 80 -s # draw the todo list starting on the 18th line.
        done

这是banner.sh:

#!/bin/bash

filename=`ls -d ~/banners/* | shuf -n 1`
tput cup 0 0 && cat $filename && echo ""

解决方法

由于观看ls或其他一些彩色输出工作正常,看来cat不会以手表可以接受的格式产生彩色输出,但是涉及cat和彩色输出的任何事情都不能使屏幕黑屏。 / p>

我的答案涉及放下手表,只是组合脚本,在后台运行bash循环,每60秒更新一次横幅。不幸的是,对于此循环或待办事项列表,对STDOUT的写入均不是原子操作,因此我只需要简单地更新包含标题和列表的变量,并在列表文件每次更改时更新整个屏幕>现在是60秒,现在是换新横幅的时候了。

这不是理想的,因为我要重画一堆我不必重画的东西,但这是解决我找不到找到写入方法的唯一方法。 STDOUT原子。

#!/bin/bash
clear
# choose a random banner and initialize variables
filename=`ls -d ~/banners/*.ans | shuf -n 1`
banner=$(head -c -1 $filename)

function todo {
        # move to the top left of the screen but don't clear
        tput cup 0 0
        # display the banner
        echo "${banner}"
        # make sure we're on line 16
        tput cup 16 0
        # update the todo list,so that it will clear each line to the end
        todolist=$(/usr/local/bin/todo.sh today | fold -w 80 -s | sed -r 's/$/\\033\[K/g')
        # display the todo list
        echo -e "${todolist}"
        # clear the rest of the screen
        tput ed
}

# CTRL-C exits cleanly,killing the banner process that was running in the background
trap 'trap - SIGTERM && tput cnorm && clear && kill 0' SIGINT SIGTERM EXIT

while true; do
        # choose a new banner
        filename=`ls -d ~/banners/*.ans | shuf -n 1`
        banner=$(head -c -1 $filename)
        # redraw the screen
        todo
        # do this every 60 seconds
        sleep 60
done &

# make the cursor invisible for now
tput civis

fswatch -l 1 -o --event=Updated -e "~/.todo/.*" -i "todo.txt" ~/.todo | while read;
        do
                # redraw the screen
                todo
        done

# make the cursor visible again
tput cnorm
exit

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res