linux - 如何使用 shell 脚本在文件中写入历史记录
问题描述
我做了一个长时间的操作,所以我想把我的历史记录并放入一个文件“text.txt”然后我 rm 历史记录,但它不起作用,我不知道为什么。
#!/bin/bash
while true
do
history -r
history >> root/history.txt
echo "history.txt rempli" &
echo "test automatise" >> history.txt
rm history
sleep 10
done
它只写“测试自动化”,历史是橙色的,但 bash 告诉我他不能 rm 历史,因为他不知道。
我做 ./test.sh & 当我想启动脚本时。
谢谢 !!
解决方案
rm history
将尝试删除历史文件或目录,它可能不存在并且会抛出错误,而是清除您的历史记录history -c
history 不起作用,因为它没有在当前的 shell 脚本中运行,你可以让它工作source
:
#!/bin/bash
while true
do
history -r
history >> root/history.txt
echo "history.txt rempli" &
echo "test automatise" >> history.txt
history -c
sleep 10
done
然后运行
$ source test.sh &
了解有关来源的更多信息:https ://superuser.com/questions/46139/what-does-source-do
详细了解为什么没有源代码就无法运行历史:https ://unix.stackexchange.com/questions/5684/history-command-inside-bash-script
推荐阅读
- tsql - SQL Server:如何按顺序从许多其他列中自定义 Order By 子句中的一列
- angular - 如何推断组件的@Input 的正确类型
- javascript - TypeScript 在调用对象(这是一个函数)的值时无法推断出正确的类型
- vba - 重新查询后恢复连续形式
- python - 在 Tensorflow 2.3 中利用调整层的正确方法?
- reactjs - React 的 SweetAlert 总是删除/编辑动态列表的最后一项
- ios - 我从自定义构建配置存档时缺少文件
- python - 如何使用 Beautifulsoup 抓取网站
- angular - Angular 11 向 .Net Core 后端发布 pdf 请求
- if-statement - IF 函数(SharePoint 计算列)返回的数据类型是什么?