bash - 在运行命令 bash 之前保存输入
问题描述
我正在尝试将我输入的命令及其各自的输出保存在一个文件中。
我目前在做什么:
mylog() {
echo "----------------" >> ${PWD}/cmds.log
echo "$@" >> ${PWD}/cmds.log
# read foo # Tried reading the piped value in to
# echo "$foo" >> ${PWD}/cmds.log
# MYCMD="${@}"
# echo "$MYCMD" >> ${PWD}/cmds.log
"$@" | tee -a ${PWD}/cmds.log
}
它目前mylog cat file.txt | mylog sort
的功能如下: 输出cmds.log
为:
----------------
cat file.txt
----------------
sort
....
output from cat
...
output from sort
...
我想要的是:
----------------
cat file.txt
....
output from cat
...
----------------
sort
...
output from sort
...
在代码中,您可以看到我尝试使用 read 并遵循此处描述的其他方法。
我也试过这样做VAR=${@}
,但这只是保存了两次命令并且没有执行它。
知道如何实现我的目标吗?
稍微相关的是,我像这样手动保存终端输出,而不是使用,script
因为交互式外壳会导致很多转义字符等被捕获。
解决方案
您修改后的脚本...
#!/usr/bin/env bash
mylog() {
local output="log"
# This is the key part right here. The 'tac' command here
# prints stdin in reverse. To do this, it must read _all_
# of stdin, so it buffers until it reads everything. Of course,
# we must use two 'tac's so stdin doesn't actually get reversed.
# You could also use `sponge(1)` here, but that does not come standard
# on all Linux distributions / MacOS
local buffer="$("$@" | tac | tac)"
# Nothing below will be printed until 'buffer' is completely filled with stdin
{
echo "----------------"
echo "$@"
echo "----------------"
echo "$buffer"
echo
} >> "$output"
printf "%s" "$buffer"
}
rm -f log
mylog cat file.txt | mylog sort >/dev/null
所结果的log
----------------
cat file.txt
----------------
bravo
alfa
charlie
delta
echo
----------------
sort
----------------
alfa
bravo
charlie
delta
echo
推荐阅读
- hive - 我们如何使用 URL 将数据加载到配置单元中
- struct - 如何在 Julia 中创建结构?
- android - 活动的 Edittext 自动填充覆盖在片段上可见
- java - Googlemaps V2:在 GeoJsonPolygon 上添加信息窗口
- microsoft-graph-api - 通过 Graph API 访问和创建团队聊天
- reactjs - react-setState 不清除文本框
- arrays - “() => x”类型中缺少属性“x”,但在“x”类型中是必需的
- next.js - NextJS - react-springy-parallax 不起作用
- python - 如何在python的其他行中用值填充NA
- javascript - 如何仅动画负边距