如何在tmux.conf中设置和使用变量取决于是否设置了环境变量 [英] How to set and use variable in tmux.conf depending on whether an environment variable is set

查看:76
本文介绍了如何在tmux.conf中设置和使用变量取决于是否设置了环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(免责声明:我完全知道我在下面描述的问题有解决方案,涉及编写和调用与正在运行的 tmux 服务器交互的 shell 脚本,或者在启动 tmux 服务器之前设置必要的环境变量.我特别发布此问题以查看是否可以在使用此类脚本的情况下解决此问题.)

(Disclaimer: I am fully aware that there are solutions to the problem I describe below that involve writing and calling shell scripts that interact with a running tmux server, or set the necessary environment variables before starting the tmux server. I am specifically posting this questions to see if it possible to solve this problem without the use of such scripts.)

在我的 .tmux.conf 文件中,我试图将局部变量 VALUE 设置为不同的值,具体取决于环境变量 FOO 是否在调用 tmux 之前已设置.然后我想在其他 tmux 命令中使用 VALUE .不幸的是,我要么无法正确设置 VALUE,要么在设置后访问它.

In my .tmux.conf file, I am trying to set a local variable VALUE to different values depending on whether an environment variable FOO has been set before invoking tmux or not. I then want to use VALUE in other tmux commands. Unfortunately, I either cannot set VALUE correctly or access it after it has been set.

根据我在联机帮助页和其他包含示例 tmux 代码的问答帖子中找到的内容,有多种方法可以实现上述内容.

According to what I have found in the manpage and in other Q&A posts that contain sample tmux code, there are several ways to implement the above.

我首先尝试使用 if-shell 命令.我尝试在带有和不带有 -b 标志的情况下使用此命令;两种情况下的结果都是一样的.

I first tried using the if-shell command. I attempted using this command both with and without the -b flag; the result was the same in either case.

我从示例中看到,我可以使用语法 VALUE=bar 分配变量.鉴于此,这是我的配置的最小示例:

I have seen from examples that I can assign variables with the syntax VALUE=bar. Given that, here is a minimal example of my configuration:

if-shell '[ -z "$FOO" ]' \
    'VALUE=bar' \
    'VALUE=baz'

set-environment -g RESULT $VALUE

终端会话:

$ echo $FOO

$ tmux
[detached (from session 0)]
$ tmux showenv -g VALUE
VALUE=bar
$ tmux showenv -g RESULT
RESULT=
$ killall tmux
$ export FOO=foo
$ echo $FOO
foo
$ tmux
[detached (from session 0)]
$ tmux showenv -g VALUE
VALUE=baz
$ tmux showenv -g RESULT
RESULT=

因此,虽然 VALUE 似乎已正确设置,但 RESULT 似乎无法访问 VALUE.

So while VALUE seems to have been set correctly, RESULT does not seem to able to access VALUE.

手册页还提到可以使用 %if 语句有条件地执行命令.使用这种格式,我尝试了以下配置:

The manpage also mentions that commands can be conditionally executed using %if statements. Using this format, I tried the following configuration:

%if #{#(if [ -z "$FOO" ]; then echo 1; else echo 0)}
VALUE=bar
%else
VALUE=baz
%endif

set-environment -g RESULT $VALUE

对于%if语句中的表达式,我尝试了几种变体,比如

For the expression in the %if statement, I tried several variations, such as

  • #{#([ -z "$FOO" ])}(我认为这应该不起作用,因为此命令不会产生任何输出,但值得一试.)
  • #{==:#(if [-z "$FOO" ]; then echo 1; else echo 0),1}(以防万一显式比较有效)
  • #{#([ -z "$FOO" ])} (I believe this shouldn't work since this command does not produce any output, but it was worth a try.)
  • #{==:#(if [-z "$FOO" ]; then echo 1; else echo 0),1} (Just in case an explicit comparison would work)

即使有这些变化,无论是否设置了 FOO,我都会得到以下信息:

Even with these variations, regardless of whether FOO was set or not, I got the following:

$ tmux
[detached (from session 0)]
$ tmux showenv -g VALUE
VALUE=baz
$ tmux showenv -g RESULT
RESULT=baz

因此,虽然 VALUE 是可访问的,但它始终是 baz.

Thus while VALUE was accessible, it was always baz.

不幸的是,我找不到关于条件语句中使用的格式的有用示例.手册页描述了如何访问 tmux 变量和一些格式提示;然而,关于访问环境变量,我能找到的只是一种使用 shell 命令的方法:

Unfortunately, I have been able to find no useful examples regarding the formats used in conditional statements. The manpage describes how to access tmux variables and some formatting hints; however, regarding accessing environment variables, all I could find was a way to use shell commands:

此外,可以使用 #() 插入 shell 命令输出的第一行.例如,#(uptime) 将插入系统的正常运行时间.构造格式时,tmux 不会等待 #() 命令完成;相反,使用运行相同命令的先前结果,或者如果之前未运行过该命令,则使用占位符.

In addition, the first line of a shell command's output may be inserted using #(). For example, #(uptime) will insert the system's uptime. When constructing formats, tmux does not wait for #() commands to finish; instead, the previous result from running the same command is used, or a placeholder if the command has not been run before.

我不确定这是否意味着我需要在 #() 中调用命令两次以避免使用占位符值,这对我来说可能是一个错误.

I am unsure of whether this means I need to call commands in #() twice to avoid using a placeholder value, which may be a possible error on my part.

我也无法找到一种方法来轻松打印 #{} 命令的结果来调试这部分语句.

I was also unable to find a way to print the result of #{} commands easily to debug this part of the statement.

虽然我很感激任何可以帮助我解决这个问题的信息,但对我来说最紧迫的问题是:

While I would appreciate any pointers to information that may help me solve this problem, the most pressing questions for me are:

  • 为什么 VALUE 设置正确,但 RESULT 在尝试 1 中无法访问?
  • 应该如何在尝试 2 中编写我的条件以确保 VALUE 设置正确?
  • Why is VALUE being set correctly, yet not accessible to RESULT in Attempt 1?
  • How should my conditional be written in Attempt 2 to ensure that VALUE is set correctly?

推荐答案

tmux 运行 config 的方式是将 config 文件解析成一组命令,然后执行(有一个命令队列,所以 config 文件被解析并附加到队列中,然后从队列中执行).所以有不同的解析和执行步骤.

The way tmux runs the config is by parsing the config file into a set of commands, and then executing them (there is a command queue, so the config file is parsed and appended to the queue and then executed from the queue). So there are distinct parse and execution steps.

您在尝试 1 中遇到的问题是 if-shell 在执行时运行,但 $VALUE 扩展发生在解析时.解析 set-environment 命令时未设置 VALUE.

The problem you are running into with attempt 1, is that the if-shell is run at execution time, but the $VALUE expansion happens at parse time. VALUE is not set when the set-environment command is parsed.

在尝试 2 中,#() 未在 %if 内处理,因此不起作用.但是,您可以直接在格式中使用该变量(如果已设置).%if 发生在解析时.

In attempt 2, #() is not processed inside %if so that won't work. However, you can use the variable directly in formats (if it is set). %if happens at parse time.

因此您需要确保分配和扩展以正确的顺序进行.您有几个选择.

So you need to make sure assignment and expansion happen in the right order. You have a couple of choices.

您可以让 tmux 在命令执行时而不是解析时扩展变量.您可以通过将 setenv 包装在 run-shell 中来做到这一点,例如:

You could make tmux expand the variable at command execution time rather than parse time. You can do this by wrapping the setenv inside run-shell, so something like:

   if-shell '[ -z "$FOO" ]' \
       'VALUE=bar' \
       'VALUE=baz'
   run 'tmux setenv -g RESULT $VALUE'

或者您可以像在尝试 2 中尝试的那样在解析时进行分配,但是您不能使用 #() - 您需要使用一种格式:

Or you could do the assignment at parse time like you tried in attempt 2, but you can't use #() - you need to use a format instead:

   %if #{==:#{FOO},}
   VALUE=bar
   %else
   VALUE=baz
   %endif
   setenv -g RESULT $VALUE

(请注意,配置文件中的 X=Y 等效于 setenv -g X=Y 除非它发生在解析而不是执行时 - 两者都设置全局环境.因此您可以摆脱 VALUE 并执行 RESULT=bar或 setenv -g RESULT 栏内的 %if.)

(Note that X=Y in the config file is equivalent to setenv -g X=Y except it happens when parsing rather than executing - both set the global environment. So you could get rid of VALUE and do either RESULT=bar or setenv -g RESULT bar inside the %if.)

这篇关于如何在tmux.conf中设置和使用变量取决于是否设置了环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆