如何在bash脚本中嵌入和运行存储在bash变量中的多行perl脚本(不立即运行perl) [英] How to embed and run a multi-line perl script stored in a bash variable in a bash script (without immediately running perl)
本文介绍了如何在bash脚本中嵌入和运行存储在bash变量中的多行perl脚本(不立即运行perl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用运行此perl脚本存储在bash变量中?
How do I replace [RUN_ABOVE_PERL_SORTING_SCRIPT_HERE]
with something that runs this perl script stored in a bash variable?
#!/usr/bin/env bash
# The perl script to sort getfacl output:
# https://github.com/philips/acl/blob/master/test/sort-getfacl-output
find /etc -name .git -prune -o -print | xargs getfacl -peL | [RUN_ABOVE_PERL_SORTING_SCRIPT_HERE] > /etc/.facl.nogit.txt
注意:
- 我不想使用2个文件(bash脚本和perl脚本)来解决此问题;我希望将功能全部存储在一个bash脚本文件中。
- 在存储perl-script变量时,我不想立即运行perl脚本,因为我想稍后再运行它在 getfacl(1) bash管道中显示。
- 有很多类似的stackoverflow问题和答案,但我都找不到(解决问题的代码清晰吗?),可以解决以下问题:a)多行和b)延迟执行(或嵌入式perl脚本) )这个问题的一部分。
- 并澄清一下:这个问题不是专门针对getfacl(1)的,它只是探索如何嵌入perl脚本以及其他脚本的催化剂诸如python之类的语言-转换为bash变量,以便在bash脚本中延迟执行。)
- I do not want to employ 2 files (a bash script and a perl script) to solve this problem; I want the functionality to be stored all in one bash script file.
- I do not want to immediately run the perl script when storing the perl-script variable, because I want to run it later in the getfacl(1) bash pipeline shown below.
- There's many similar stackoverflow questions+answers, but none that I can find (that has clean-reading code, anyway?) that solve both the a) multi-line and b) delayed-execution (or the embedded perl script) portion of this problem.
- And to clarify: this problem is not specifically about getfacl(1), which is simply an catalyst to explore how to embed perl scripts--and possibly other scripting languages like python--into bash variables for delayed execution in a bash script.)
推荐答案
使用 bash rea d命令,它将perl脚本读入一个变量,该变量稍后在bash脚本中执行。
Employ the bash read command, which reads the perl script into a variable that's executed later in the bash script.
#!/usr/bin/env bash
# sort getfacl output: the following code is copied from:
# https://github.com/philips/acl/blob/master/test/sort-getfacl-output
read -r -d '' SCRIPT <<'EOS'
#!/usr/bin/env perl -w
undef $/;
print join("\n\n", sort split(/\n\n/, <>)), "\n\n";
EOS
find /etc -name .git -prune -o -print | xargs getfacl -peL | perl -e "$SCRIPT" > /etc/.facl.nogit.txt
这篇关于如何在bash脚本中嵌入和运行存储在bash变量中的多行perl脚本(不立即运行perl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文