[20230825]dc命令复杂学习.txt

–//前几天学习dc使用,我当时最后举了一个累加的例子,里面
–//-e后面那一串什么意思,即使看了man dc文档,我当时也没看懂表示什么意思.尝试看了man文档,简单解析如下:
–//我从文档里面取出相关说明:

[characters]
Makes a string containing characters (contained between balanced [ and ] characters), and pushes it on the stack.For
example, [foo]P prints the characters foo (with no newline).
生成一个包含字符的字符串(包含在平衡的[和]字符之间),并将其推到堆栈上。例如,[foo]P打印字符文件(没有换行符)。

sr

Pop the value off the top of the stack and store it into register r.
推出顶端的堆栈值进入内存寄存器,保存到寄存器r。

z

Pushes the current stack depth: the number of objects on the stack before the execution of the z command.
推动当前堆栈深度:在执行z命令之前,堆栈上的对象数。

>r

Pops two values off the stack and compares them assuming they are numbers, executing the contents of register r as a
macro if the original top-of-stack is greater. Thus, 1 2>a will invoke register a’s contents and 2 1>a will not.
从堆栈中弹出两个值,并比较它们,假设它们是数字,执行寄存器r的内容作为一个宏,如果原始的堆栈顶部更大。因此,1 2>a 将调用
注册a的内容,而 2 1>a将不会。
–//主要为了下面<r的解析.

<r

Similar but invokes the macro if the original top-of-stack is less.
类似的方法,但如果原始的堆栈顶部较少,则调用宏。

p

Prints the value on the top of the stack, without altering the stack. A newline is printed after the value.
在堆栈的顶部打印该值,而不更改堆栈。在该值后将打印一个换行符。

f

Prints the entire contents of the stack without altering anything. This is a good command to use if you are lost or want
to figure out what the effect of some command has been.
打印堆栈的全部内容,而不改变任何内容。如果您丢失或想要使用这个命令,这是一个很好的命令来弄清楚一些命令的效果。

–//这样累加例子的脚本解析如下,要有点耐心还是很容易读懂的:
$ cat a.txt
1111
2222
3333
4444

$ cat a.txt | dc -f – -e “[+z1<r]srz1<rp"
11110

–//解析如下:
1111
2222
3333
4444
[+z1<r]
–//将上面文件a.txt的内容以及[+z1<r] 压入堆栈.

sr -> 保存字符串 +z1<r 到寄存器r,并且出栈.
z -> 指当前堆栈的数量.并将它推入堆栈.
1 如果1<(堆栈的数量),调用宏r.也就是执行+z1<r,也就是先相加 ,剩下的只要1<(堆栈的数量)不断的调用r.

–//我修改如下,加入f命令显示堆栈内容,就很清晰了.
$ cat a.txt | dc -f – -e “[+z1f<r]fsrz1f<rp"
+z1f<r
4444
3333
2222
1111
–//第1次执行f显示堆栈的情况.+z1f<r 作为字符串最后压入堆栈. 相当于执行fsrz1f<rp里面的f命令.
1
4
4444
3333
2222
1111
–//执行srz1f<r,sr保存字符串 +z1f<r 保存到寄存器r,并且字符串出栈.z 指当前堆栈的数量4(因为字符串已经出栈).并将它推入堆栈.
–//1 推入堆栈
–//第2次执行f 显示堆栈的情况(如上). <r 1<4为真,调用寄存器r的内容.也就是执行 +z1f<r.只要1<当前堆栈的数量,调用寄存器r的内容.
1
3
7777
2222
1111
–//第3次执行f显示堆栈的情况.
1
2
9999
1111
–//第4次执行f显示堆栈的情况.
1
1
11110
–//第5次执行f显示堆栈的情况. 1<1 为假,不再调用宏r.
11110
–//第6次执行p输出结果.

–//也可以这样执行:
$ cat a.txt | dc -e “[+z1f<r]sr" -f – -e "z1f<rp"
1
4
4444
3333
2222
1111
1
3
7777
2222
1111
1
2
9999
1111
1
1
11110
11110

$ cat a.txt | dc -e “[+z1<r]sr" -f – -e "z1<rp"
11110

总结:
–//也就是静下心来,还是很容易理解的.