我刚刚学会了使用命令创建新文件的技巧cat
。根据我的测试,如果最后一行后面没有换行符,我必须输入ctrl+d两次才能完成输入,如下所示。
[root@192 ~]# cat > 测试 A 乙 ctrl+d[root@192 ~]# cat > 测试 A b ctrl+dctrl+d[root@192 ~]#
这是预期的吗?为什么会有这种行为?
当我需要使用捕获一些数据包时tcpdump
,我使用如下命令:
tcpdump -i eth0 "dst host 192.168.1.0"
Run Code Online (Sandbox Code Playgroud)
我一直认为dst 主机 192.168.1.0部分是称为 BPF(伯克利数据包过滤器)的东西。对我来说,这是一种过滤网络数据包的简单语言。但今天我的室友告诉我 BPF 可以用来捕获性能信息。根据他的描述,这就像Windows上的工具perfmon
。这是真的吗?它与我在问题开头提到的BPF相同吗?
我使用的是 RHEL 8.3 系统。我发现内置的vim
和vi
都是vim
. 它们具有相同的版本,但编译方式不同。以下是我的测试:
[root@192 ~]# which vim
/usr/bin/vim
[root@192 ~]# which vi
/usr/bin/vi
[root@192 ~]# ls -lah `which vim`
-rwxr-xr-x. 1 root root 3.0M Jun 3 2020 /usr/bin/vim
[root@192 ~]# ls -lah `which vi`
-rwxr-xr-x. 1 root root 1.2M Jun 3 2020 /usr/bin/vi
[root@192 ~]# vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 3 2020 09:07:54)
Included patches: 1-1763
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
Huge version without …
Run Code Online (Sandbox Code Playgroud) 当我阅读这个答案时,作者使用此命令将定界符的结果放入变量:
read -r -d '' VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
Run Code Online (Sandbox Code Playgroud)
我对这个选项有点困惑-d
。从该命令的帮助文本中read
:
-d delim
continue until the first character of DELIM is read, rather than newline
Run Code Online (Sandbox Code Playgroud)
因此,如果我将空字符串传递给-d
,则意味着读取直到第一个空字符串。这是什么意思?作者在答案下评论-d ''
说使用NUL字符串作为分隔符。这是真的吗(空字符串意味着 NUL 字符串)?为什么不使用类似-d '\0'
或-d '\x0'
等的东西?
I'm a new Linux user and I was doing some experiments and trying to understand Process Substitution. I believe I already have a basic understanding of it. But here is a case that I don't know why. I'm using Bash on Ubuntu 20.04.
echo hi
just sends the string hi
with a newline character to stdout.
root@u2004:~# echo hi | od -a
0000000 h i nl
0000003
root@u2004:~#
Run Code Online (Sandbox Code Playgroud)
cat
can read from the pipeline as its stdin, and send …
谁能帮助我了解正则表达式匹配如何与该expr
实用程序配合使用?我已阅读其手册页,下面是摘录:
STRING : REGEXP
anchored pattern match of REGEXP in STRING
Run Code Online (Sandbox Code Playgroud)
但我不明白它是如何工作的。我做了一些测试:
[root@192 ~]# expr "abc" : '.*'
3
[root@192 ~]# expr "abc" : 'b.*'
0
[root@192 ~]#
Run Code Online (Sandbox Code Playgroud)
expr
这两个命令在做什么?对于第一个命令,似乎expr
从第一个字符找到了匹配项abc
并报告了匹配的长度。但为什么它会产生0
第二个命令呢?我只是不明白这里的逻辑。
顺便说一句,我知道正则表达式是如何工作的。
为了描述这个问题,我创建了一个简单的 C 程序。
root@u2004:~# cat test.c
#include <stdio.h>
int main(){
printf("output 1\n");
fprintf(stderr, "error 1\n");
printf("output 2\n");
fprintf(stderr, "error 2\n");
return 0;
}
root@u2004:~# gcc test.c
root@u2004:~#
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下输出:
root@u2004:~# ./a.out
output 1
error 1
output 2
error 2
root@u2004:~#
Run Code Online (Sandbox Code Playgroud)
可以看到,控制台中的输出顺序与程序中定义的语句相同。现在我想将 stderr 和 stdout 重定向到单个文件,并保持输出顺序。以下是我尝试过的:
root@u2004:~# ./a.out > out 2>&1
root@u2004:~# cat out
error 1
error 2
output 1
output 2
root@u2004:~#
root@u2004:~# cat <(./a.out)
error 1
error 2
output 1
output 2
root@u2004:~#
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,在这两种情况下,错误消息首先出现,然后是正常输出。这与程序中定义的顺序不同。将 stdout 和 stderr 重定向到同一文件时如何保持输出顺序?
我正在使用 GNU Awk 5.0.1,我需要使用[}
or [)
as FS
。我无法让它发挥作用。以下是我尝试过的。
root@u2004:~# echo test | awk -F '[}' '{printf}'
awk: fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[}/
root@u2004:~# echo test | awk -F '[\}' '{printf}'
awk: warning: escape sequence `\}' treated as plain `}'
awk: fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[}/
root@u2004:~# echo test | awk -F '[\\}' '{printf}'
awk: fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[\}/
root@u2004:~# …
Run Code Online (Sandbox Code Playgroud) 在RHEL上,有一个命令lid
,可以列出组用户,无论是主组还是次要组。
[root@192 ~]# id user1
uid=1000(user1) gid=1000(user1) groups=1000(user1),1001(g1)
[root@192 ~]# id user2
uid=1001(user2) gid=1002(user2) groups=1002(user2),1001(g1)
[root@192 ~]# id user3
uid=1002(user3) gid=1001(g1) groups=1001(g1)
[root@192 ~]# lid -g g1
user3(uid=1002)
user1(uid=1000)
user2(uid=1001)
[root@192 ~]#
Run Code Online (Sandbox Code Playgroud)
但它在 Ubuntu 上不存在。有类似的吗?
我使用的是 RHEL 8 系统,默认sed
安装了 4.5。
[root@192 ~]# rpm -qf `which sed`
sed-4.5-2.el8.x86_64
[root@192 ~]#
Run Code Online (Sandbox Code Playgroud)
由于我想使用该--debug
选项来调试一些sed
命令,因此我必须升级sed
. 当我尝试卸载时sed
,我发现很多其他软件包依赖于它:
[root@192 ~]# rpm -e sed
error: Failed dependencies:
/bin/sed is needed by (installed) os-prober-1.74-6.el8.x86_64
sed is needed by (installed) krb5-libs-1.18.2-5.el8.x86_64
sed is needed by (installed) ca-certificates-2020.2.41-80.0.el8_2.noarch
sed is needed by (installed) dracut-049-95.git20200804.el8.x86_64
sed is needed by (installed) policycoreutils-2.9-9.el8.x86_64
sed is needed by (installed) sane-backends-1.0.27-19.el8_2.1.x86_64
sed is needed by (installed) authselect-libs-1.2.1-2.el8.x86_64
sed is needed by …
Run Code Online (Sandbox Code Playgroud)