bluetoothctl 在哪里存储命令历史记录?

and*_*bak 7 command-history bluetooth

没有任何关于在命令历史记录man bluetoothctlinfo bluetoothctlbluetoothctl --help

and*_*bak 11

简答

bluetoothctl将命令历史存储在~/.cache/.bluetoothctl_history.


长答案

免责声明:长答案需要对编程语言 C 有一定的了解。

bluetoothctl是一个命令行工具,随BlueZ – Linux 的蓝牙协议栈一起提供。如果我们看一下 BlueZ 的源代码:

我们很快就会意识到它bluetoothctl使用GNU Readline 库作为其交互式 shell。根据Readline 的文档,函数write_history可用于将历史记录写入文件。如果我们只是在 BlueZ 源代码中搜索此函数的名称:

$ grep write_history -r
src/shared/shell.c:             write_history(data.history);
Run Code Online (Sandbox Code Playgroud)

该命令的历史中bluetoothctl写入一个文件,他的名字在外地举行.historystruct data。然后,只需搜索对该字段的访问,我们就会找到它的初始化位置:

$ grep write_history -r
src/shared/shell.c:             write_history(data.history);
Run Code Online (Sandbox Code Playgroud)

这里,XDG_CACHE_HOME来自freedesktop.org 规范。其他环境变量是 basic$HOME$PWD. 字段data.name设置在别处:

static void rl_init_history(void)
{
        const char *name;
        char *dir;

        memset(data.history, 0, sizeof(data.history));

        name = strrchr(data.name, '/');
        if (!name)
                name = data.name;
        else
                name++;

        dir = getenv("XDG_CACHE_HOME");
        if (dir) {
                snprintf(data.history, sizeof(data.history), "%s/.%s_history",
                                                        dir, name);
                goto done;
        }

        dir = getenv("HOME");
        if (dir) {
                snprintf(data.history, sizeof(data.history),
                                "%s/.cache/.%s_history", dir, name);
                goto done;
        }

        dir = getenv("PWD");
        if (dir) {
                snprintf(data.history, sizeof(data.history), "%s/.%s_history",
                                                        dir, name);
                goto done;
        }

        return;

done:
        read_history(data.history);
        using_history();
        bt_shell_set_env("HISTORY", data.history);
}
Run Code Online (Sandbox Code Playgroud)

所以char *name函数中的变量rl_init_history将包含字符串bluetoothctl,可执行文件的名称。参见C 中的描述argv

因此,在大多数遵循 freedesktop.org 规范的桌面环境中,命令行工具bluetoothctl会将命令的历史记录存储在文件~/.cache/.bluetoothctl_history. 如果XDG_CACHE_HOME定义了环境变量,则命令的历史记录将存储在$XDG_CACHE_HOME/.bluetoothctl_history.