小编ibu*_*fen的帖子

如何创建到 inode (ext4) 的硬链接?

如果我知道文件的索引节点(inode),但不知道它的路径(或其任何路径),是否可以直接创建到该 inode 的硬链接?

我可以找到文件sudo find / -inum 123546,然后创建一个硬链接,但这对我的应用程序来说太慢了。

注意我使用的是 ext4 文件系统。

ext4 hard-link inode

20
推荐指数
1
解决办法
2044
查看次数

可以在一行中声明多个局部变量吗

假设有一个 function/method foo。可以使用local关键字在一行中声明多个变量,还是必须为每个变量用一个声明语句分隔?

foo()
{
    local x y z
}
Run Code Online (Sandbox Code Playgroud)

或者

foo()
{
    local x
    local y
    local z
}
Run Code Online (Sandbox Code Playgroud)

并进一步:

foo()
{
    local -i x -a y  z
}

foo()
{
    local -i x=2 -a y=()  z
}

…
Run Code Online (Sandbox Code Playgroud)

或等效的一行一行声明。


是的。可以测试,但对于一个我找不到任何 Q/A,第二个 - 可能有一些隐藏的警告

bash

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

解析 terminfo u6 字符串

查看terminfoParameterized Strings

一些例子来自infocmp -1 xterm

  • cud=\E[%p1%dB,给定参数13

    • \E =><ESC>
    • [ => [
    • %p1将参数 1 (13)推入堆栈
    • %d POP 并从堆栈打印为有符号十进制 => 13
      • 结果: <ESC>[13B
  • csr=\E[%i%p1%d;%p2%dr,给定参数13, 16

    • \E =><ESC>
    • [ => [
    • %i 递增参数 1 和 2:++13、++16 给出 14、17
    • %p1将参数 1 (14)推入堆栈。
    • %dPOP 并从堆栈打印为有符号十进制。=>14
    • ; => ;
    • %p2将参数 2 (17)推入堆栈。
    • %dPOP 并从堆栈打印为有符号十进制。=>17
    • r => r
      • 结果: <ESC>14;17r

但是,……怎么读这个?

  • u6=\E[%i%d;%dR

处理后, …

terminal xterm ncurses console terminfo

6
推荐指数
1
解决办法
586
查看次数

用指针选择屏幕上的区域

是否有一个命令行工具可以让人们用指针选择一个区域并将结果打印到终端?

很像各种屏幕捕获工具,但我想要坐标。

例如x,y,width,height或矩形结构,如下所示left,top,right,bottom

command-line desktop utilities

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

NUL 分隔变量

GNU bash,版本 4.4.19(1)-release (x86_64-pc-linux-gnu)

想法是将变量设置为 NUL 分隔的数据集。这里$samples

然而,这会导致:

警告:命令替换:忽略输入中的空字节

做的时候:

samples="$(find . -type d -iregex './sample[0-9][0-9]' -printf "%f\0" | sort -z)"
Run Code Online (Sandbox Code Playgroud)

以为我可以重复使用这个变量,因为我需要多次迭代相同的值:

while IFS= read -rd '' sample; do
    echo $sample
done<<< "$samples"
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我可以在find命令中使用\nover ,但想知道如果可能的话,一般来说如何使用 NUL 分隔符来做到这一点。\0

(可选)我可以这样做:

while IFS= read -rd '' sample; do
    echo $sample
done< <(find . -type d -iregex './E[0-9][0-9]' -printf "%f\0" | sort -z)
Run Code Online (Sandbox Code Playgroud)

但是 - 因为我需要多次循环它,所以会产生一些非常冗余的代码 - 并且每次都必须运行findsort命令。

也许将结果转换为数组?


  • 这可能吗?
  • 为什么不能按原样使用 NUL 分隔的数据?

bash command-substitution

4
推荐指数
2
解决办法
945
查看次数

获取 termcap 功能的终端状态

如何检索像smam和这样的终端设置的状态rmam

原因是我设置rmam的:

tput rmam
Run Code Online (Sandbox Code Playgroud)

在脚本中,然后smam在退出时继续设置:

tput smam
Run Code Online (Sandbox Code Playgroud)

但是如果终端rmam在脚本启动时设置,我不想smam在退出时设置。


如何才能做到这一点?

scripting terminal tput terminfo

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

如何根据行模式的开头将文本文件拆分为多个文件?

我有一些文本文件,我想根据我放在各行开头的任意“标签”将它们分成不同的文件。

示例文本文件:

I CELEBRATE myself, and sing myself,  
And what I assume you shall assume, 
For every atom belonging to me as good belongs to you.

#here I loafe and invite my soul, 
#here I lean and loafe at my ease observing a spear of summer grass.

#there My tongue, every atom of my blood, form'd from this soil, this air,
#there Born here of parents born here from parents the same, and their parents the same, 
#here I, now …
Run Code Online (Sandbox Code Playgroud)

shell awk text-processing macos

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

测试表达式中的参数扩展是否可能?

当我在中尝试以下表达式时,bash收到一条奇怪的错误消息:

[: -lt: unary operator expected
Run Code Online (Sandbox Code Playgroud)

首先是函数定义

some_func () {
  (( 3 + 5 ))
}
Run Code Online (Sandbox Code Playgroud)

还有表情

[ $(some_func) -lt 10 ] && true
Run Code Online (Sandbox Code Playgroud)

我想问题是将运算符-lt与命令替换和/或算术扩展混合在一起?

退出代码$?为 2,消息为an unary op was expected

bash arithmetic command-substitution test

0
推荐指数
1
解决办法
99
查看次数

如何忽略 mv 中的错误:无法将 'XXXXXX 移动到自身的子目录

在我们的 RHEL 7.6 服务器上,我们创建以下文件夹

# mkdir -p /var/data/data-logs_temp
Run Code Online (Sandbox Code Playgroud)

第二部分是将所有内容移动 /var/data//var/data/data-logs_temp

# mv /var/data/* /var/data/data-logs_temp
Run Code Online (Sandbox Code Playgroud)

但是我们从mv命令中得到的输出是:

mv: cannot move ‘/var/data/data-logs_temp’ to a sub directory of itself, ‘/var/data/data-logs_temp/data-logs_temp’
Run Code Online (Sandbox Code Playgroud)

mv命令是正确的

但; 是否可以mv忽略这一点,因为我们需要mv命令中的退出代码0

或者可能有任何其他选项忽略移动自身的子目录?

linux bash rhel shell-script mv

0
推荐指数
1
解决办法
236
查看次数