此脚本搜索带有单词的行并打印它们,同时在每次迭代中重新读取源文件:
# 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) 以下代码在从终端运行时表现如预期:
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) 我有一个与挑选提交和冲突有关的问题.
'Pro Git'一书解释了提交是一种快照,而不是补丁/差异.
但挑选樱桃可能会表现得像补丁一样.
以下示例,简而言之:
创建3个提交,每次编辑文件的第一行(和单行)
将分支重置为首次提交
test1:尝试樱桃挑选第三次提交(冲突)
测试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)