- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有很多方法可以使用中间文件和函数或追加/截断来执行此操作,但我很好奇是否有人知道如何用一种单行方式来表达有条件的非破坏性覆盖,例如:
# outfile not touched if script1 fails
script1 > outfile
# outfile not touched if script1 or script2 fails
script1 | script2 > outfile
# or maybe a "lazy >" command, but I'd rather see how bash itself can do this
script1 | script2 | magicproxy outfile
其中 outfile 的先前内容保持不变如果其管道未能输出任何有效的流内容。
如果 一些 输出流,则可以弄乱 outfile,但如果没有 输出流,则不会。
我不知道有什么 bash-wise 可以解释这一点,因为我相当确定它不会挂起与管道 stdout 文件相关的基本文件设置,直到它看到输出流。除非有一个神奇的 set -o lazy 选项潜伏着。
因此,我正在寻找一种单行方式,使管道比急切的 wrt 基本 stdout 文件设置更懒惰,使用 bash 约定“在第一次实际输出时”触发。
最佳答案
此处的原始问题仅询问在修改输出文件之前等待至少一行输出通过管道。这与确保管道在覆盖目标文件之前以非零状态退出是不同的,但是添加了“取决于管道成功”部分,其中仅包含有关该方法的详细信息。
不幸的是,这个不像单行代码那么容易写。
shopt -s pipefail # fail if any component of a pipeline doesn't succeed
tempfile=$(mktemp "$1".XXXXXX)
if script1 | script2 >"$tempfile"; then
mv -- "$tempfile" "$1"
else
rm -f -- "$tempfile"
fi
一个明显的方法是使用eval
,但这需要非常小心以避免安全漏洞:
# DANGER: command_source *must* be a constant string; MUST NOT substitute argument values
# directly into source -- instead, refer to variables from that string.
eval_to_output_file() {
local command_source destfile tempfile retval
command_source=$1
destfile=$2
tempfile=$(mktemp -- "$destfile.XXXXXX")
if eval "$command_source" >"$tempfile"; then
mv -- "$tempfile" "$destfile"
else
retval=$?
rm -f -- "$tempfile"
return "$retval"
fi
}
...用法为:
# CRITICAL that script argument is single-quoted, and all arguments expanded by eval
# ...otherwise, expansions can perform code injection.
eval_to_output_file 'script1 "$arg1" | script2 "$arg2"' outfile
更安全的是,管道可以封装在一个函数中:
# Safer alternative to eval_to_output_file
# Requires that a pipeline be encapsulated into a function
run_to_output_file() {
local destfile retval
destfile=$1; shift
tempfile=$(mktemp -- "$destfile.XXXXXX")
if "$@" >"$tempfile"; then
mv -- "$tempfile" "$destfile"
else
retval=$?
rm -f -- "$tempfile"
return "$retval"
fi
}
# example of a function running your pipeline
# note that arguments are passed through by the above
myfunc() ( # myfunc() ( ) instead of myfunc() { } means execute in a subshell
shopt -s pipefail # because we're in a subshell, this won't propagate out
script1 "$1" | script2 "$2"
)
run_to_output_file outfile myfunc arg1 arg2
magicproxy() {
[[ $1 ]] || { echo "Usage: magicproxy filename" >&2; return 1; }
if IFS= read -r first_line; then
cat <(printf '%s\n' "$first_line") - >"$1"
fi
}
请注意,这意味着您的输出文件在某些时候将包含部分内容。
magicproxy() {
[[ $1 ]] || { echo "Usage: magicproxy filename" >&2; return 1; }
local tempfile
tempfile=$(mktemp -- "$1.XXXXXX") || return
if cat >"$tempfile" && [[ -s "$tempfile" ]]; then
mv -- "$tempfile" "$1"
else
rm -f -- "$tempfile"
fi
}
...将与您提议的管道一起工作:
script1 | script2 | magicproxy outfile
也就是说,如果您使用的是 GNU 系统并且 mktemp
使用的限制性权限对您不起作用,您可能还想添加:
# give your temporary file the same permissions as your destination
chmod --reference="$1" -- "$tempfile"
...在 mv
之前。
关于linux - Bash 一种在出现管道错误时保留原始管道输出文件的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113233/
当需要将原始类型转换为字符串时,例如传递给需要字符串的方法时,基本上有两种选择。 以int为例,给出: int i; 我们可以执行以下操作之一: someStringMethod(Integer.to
我有一个位置估计数据库,并且想要计算每月的内核利用率分布。我可以使用 R 中的 adehabitat 包来完成此操作,但我想使用引导数据库中的样本来估计这些值的 95% 置信区间。今天我一直在尝试引导
我希望使用 FTP 编写大型机作业流。为此,我可以通过 FTP 连接到大型机并运行以下命令: QUOTE TYPE E QUOTE SITE FILETYPE=JES PUT myjob.jcl 那么
我是 WPF 的新手。 目前,我正在为名为“LabeledTextbox”的表单元素制作一个用户控件,其中包含一个标签、一个文本框和一个用于错误消息的文本 block 。 当使用代码添加错误消息时,我
我们正在使用 SignalR(原始版本,而不是 Core 版本)并注意到一些无法解释的行为。我们的情况如下: 我们有一个通过 GenericCommand() 方法接受命令的集线器(见下文)。 这些命
使用 requests module 时,有没有办法打印原始 HTTP 请求? 我不只想要标题,我想要请求行、标题和内容打印输出。是否可以看到最终由 HTTP 请求构造的内容? 最佳答案 Since
与直接访问现有本地磁盘或分区的物理磁盘相比,虚拟磁盘为文件存储提供更好的可移植性和效率。VMware有三种不同的磁盘类型:原始磁盘、厚磁盘和精简磁盘,它们各自分配不同的存储空间。 VMware
我有一个用一些颜色着色器等创建的门。 前段时间我拖着门,它问我该怎么办时,我选择了变体。但现在我决定选择创建原始预制件和门颜色,或者着色器变成粉红色。 这是资源中原始预制件和变体的屏幕截图。 粉红色的
我想呈现原始翻译,所以我决定在 Twig 模板中使用“原始”选项。但它不起作用。例子: {{ form_label(form.sfGuardUserProfile.roules_acceptance)
是否可以在sqlite中制作类似的东西? FOREIGN KEY(TypeCode, 'ARawValue', IdServeur) REFERENCES OTHERTABLE(TypeCode, T
这个问题是一个更具体问题的一般版本 asked here .但是,这些答案无法使用。 问题: geoIP数据的原始来源是什么? 许多网站会告诉我我的 IP 在哪里,但它们似乎都在使用来自不到 5 家公
对于Openshift:如何基于Wildfly创建docker镜像? 这是使用的Dockerfile: FROM openshift/wildfly-101-centos7 # Install exa
结果是 127 double middle = 255 / 2 虽然这产生了 127.5 Double middle = 255 / 2 同时这也会产生 127.5 double middle = (
在此处下载带有已编译可执行文件的源代码(大小:161 KB(165,230 字节)):http://www.eyeClaxton.com/download/delphi/ColorSwap.zip 原
以下几行是我需要在 lua 中使用的任意正则表达式。 ['\";=] !^(?:(?:[a-z]{3,10}\s+(?:\w{3,7}?://[\w\-\./]*(?::\d+)?)?/[^?#]*(
这个问题是一个更具体问题的一般版本 asked here .但是,这些答案无法使用。 问题: geoIP数据的原始来源是什么? 许多网站会告诉我我的 IP 在哪里,但它们似乎都在使用来自不到 5 家公
我正在使用GoLang做服务器api,试图管理和回答所发出的请求。使用net/http和github.com/gorilla/mux。 收到请求时,我使用以下结构创建响应: type Response
tl; dr:我认为我的 static_vector 有未定义的行为,但我找不到它。 这个问题是在 Microsoft Visual C++ 17 上。我有这个简单且未完成的 static_vecto
我试图找到原始 Awk (a/k/a One True Awk) 源代码的“历史”版本。我找到了 Kernighan's occasionally-updated site ,它似乎总是链接到最新版本
我在 python 中使用原始 IPv6 套接字时遇到一些问题。我通过以下方式连接: if self._socket != None: # Close out old sock
我是一名优秀的程序员,十分优秀!