heredocs如何与xargs一起使用

heredocs如何与xargs一起使用

本文介绍了heredocs如何与xargs一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从git archive输出的某些python源代码文件中剥离所有# TODO注释的方法,然后再将其发送出去.我希望通过可在各种* nix操作系统上运行的脚本来执行此操作,因此该脚本应尽可能与POSIX兼容.

I'm looking to strip any # TODO comments from some python source code files output by git archive before sending it off. I'm looking to do so from a script that will be run from a variety of *nix OSes, so it should be as POSIX-compliant as possible.

我知道find -print0xargs -0不在基本规格中,但是它们看起来很普通,因此我可以很好地使用它们(除非存在更好的替代方法).由于sed -i不在就地编辑的基本规范中,因此我正在使用ed.假设下面的命令是从已经解压缩的git存档的目录中运行的.

I'm aware that find -print0 and xargs -0 aren't in the base spec, but they appear to be common enough that I'm fine with using them (unless a better alternative exists). I'm using ed due to sed -i not being in the base spec for in-place editing. Assume the command below is being run from the directory of an already un-tar'd git archive.

我对去除# TODO注释的整体替代解决方案感到满意,但是为了满足我的好奇心,我还想回答我遇到的命令所面临的特定问题和.

I'd be happy with an overall alternative solution for stripping the # TODO comments out, but to satisfy my curiosity I'd also like an answer to the particular problem I'm facing with the command I've come up with.

find . -type f -name "*.py" -print0 | xargs -0 -I {} ed -s {} << 'EOF'
,g/^[ \t]*#[ \t]*TODO/d
,s/[ \t]*#[ \t]*TODO//
w
EOF

预期结果

所有以".py"结尾的文件都被删除仅包含TODO注释或以TODO开头的行尾注释的完整行.

Expected Result

All files ending in ".py" are stripped of full lines that only contain TODO comments, or end-of-line comments starting with TODO.

(stdout)

,g/^[ \t]*#[ \t]*TODO/d
,s/[ \t]*#[ \t]*TODO//
w
: No such file or directory

当前理论

我相信<< 'EOF'此处文档将被应用到xargs而不是ed,而且我不确定该如何解决.

Current Theory

I believe that the << 'EOF' heredoc is being applied to xargs instead of ed, and I'm not sure how to fix that.

推荐答案

修复<< 'EOF' heredoc方向将花费一些sh -c技巧,最终导致更多问题,因此我尝试重新解决该问题,而无需heredocs完全没有.

Fixing the << 'EOF' heredoc direction would take some sh -c trickery that ended up causing even more problems, so I tried re-approaching the problem without requiring heredocs at all.

我最终降落在:

inline() {
    # Ensure a file was provided
    in="${1?No input file specified}"
    # Create a temp file location
    tmp="$(mktemp /tmp/tmp.XXXXXXXXXX)"
    # Preserve the original file's permissions
    cp -p "$in" "$tmp"
    # Make $@ be the original command
    shift
    # Send original file contents to stdin, output to temp file
    "$@" < "$in" > "$tmp"
    # Move modified temp file to original location, with permissions and all
    mv -f "$tmp" "$in"
}

find . -type f -name "*.py" -exec grep -ilE '#[ \t]*TODO' {} \+ | while read file; do
    inline "$file" sed -e '/^[ \t]*#[ \t]*TODO/ d' -e 's/[ \t]*#[ \t]*TODO//'
done

mktemp从技术上讲不是基本规格,但是似乎很轻便包括它就可以了. ed也给我带来了一些问题,所以我回到了sed并使用了一个自定义函数,以复制不可用的-i标志进行就地操作.

mktemp isn't technically in the base spec, but appears to be fairly portable so I'm fine including it. ed was also causing me some problems, so I went back to sed with a custom function to replicate the unavailable -i flag for in-place operations.

这篇关于heredocs如何与xargs一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:05