小编MrC*_*ket的帖子

全局正则表达式匹配while()的反引号结果

此脚本搜索带有单词的行并打印它们,同时在每次迭代中重新读取源文件:

# cat mm.pl
#!/usr/bin/perl
use strict;
use warnings;

while( `cat aa` =~ /(\w+)/g ) {
    print "$1\n";
}
Run Code Online (Sandbox Code Playgroud)

输入文件:

# cat aa
aa
bb
cc
Run Code Online (Sandbox Code Playgroud)

结果:

# ./mm.pl
aa
bb
cc
Run Code Online (Sandbox Code Playgroud)

请解释一下为什么运行脚本不是无穷无尽的.

在每一个迭代抵消了正则表达式引擎应该被重置,因为表达发生改变(新分叉).

我认为perl会对cat结果进行某种缓存,但是strace声称已经产生了4次(3行为3行+ 1为假而条件):

# strace -f ./mm.pl 2>&1 | grep cat | grep -v ENOENT
[pid 22604] execve("/bin/cat", ["cat", "aa"], [/* 24 vars */] <unfinished ...>
[pid 22605] execve("/bin/cat", ["cat", "aa"], [/* 24 …
Run Code Online (Sandbox Code Playgroud)

regex perl

16
推荐指数
1
解决办法
300
查看次数

从bash脚本运行时,Perl无法杀死自我pid

以下代码在从终端运行时表现如预期:

perl -e 'kill -2, $$; warn HERE, $/'
Run Code Online (Sandbox Code Playgroud)

它在发送SIGINT到"HERE"之前发送并死亡:

~# perl -e 'kill -2, $$; warn HERE, $/'

~# echo $?
130
~#
Run Code Online (Sandbox Code Playgroud)

问题:从shell脚本运行时,相同的代码无法终止自身PID :

~# cat 1.sh
perl -e 'kill -2, $$; warn HERE, $/'
~#
~# sh 1.sh
HERE
~#
~# echo $?
0
~#
Run Code Online (Sandbox Code Playgroud)

另一方面,用killshell 替换perl的工作正常:

~# cat 2.sh
perl -e 'qx/kill -2 $$/; warn HERE, $/'
~#
~# sh 2.sh
~#
~# echo $?
130 …
Run Code Online (Sandbox Code Playgroud)

terminal perl signals kill-process

4
推荐指数
1
解决办法
350
查看次数

cherry-picking commit - 提交快照或补丁?

我有一个与挑选提交和冲突有关的问题.

'Pro Git'一书解释了提交是一种快照,而不是补丁/差异.

但挑选樱桃可能会表现得像补丁一样.


以下示例,简而言之:

  1. 创建3个提交,每次编辑文件的第一行(和单行)

  2. 将分支重置为首次提交

  3. test1:尝试樱桃挑选第三次提交(冲突)

  4. 测试2:尝试樱桃挑选第二次提交(OK)


mkdir gitlearn
cd gitlearn

touch file
git init
Initialized empty Git repository in /root/gitlearn/.git/

git add file

#fill file by single 'A'
echo A > file && cat file
A

git commit file -m A
[master (root-commit) 9d5dd4d] A
 1 file changed, 1 insertion(+)
 create mode 100644 file

#fill file by single 'B'
echo B > file && cat file
B

git commit file -m B
[master 28ad28f] B …
Run Code Online (Sandbox Code Playgroud)

git cherry-pick

2
推荐指数
1
解决办法
344
查看次数

标签 统计

perl ×2

cherry-pick ×1

git ×1

kill-process ×1

regex ×1

signals ×1

terminal ×1