本文介绍了如何使用Bash进程替换来使变量定义程序的配置而不是文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在Bash变量而不是文件中设置的配置运行程序( tmux ).从文件中为 tmux 加载配置的标准方法如下:

I want to run a program (tmux) with a configuration that is set in a Bash variable instead of a file. The standard way to load a configuration from a file for tmux is as follows:

tmux -f test.conf attach

我在Bash变量 configurationTmux 中指定了一个配置:

I have a configuration specified in Bash variable configurationTmux:

IFS= read -d '' configurationTmux << "EOF"
set -g set-remain-on-exit on
new -s "STANDARD INTERFACE"
set-option -g prefix C-a
unbind C-b
bind - split-window -v
bind | split-window -h
## colours
set-option -g window-status-current-bg yellow
set-option -g pane-active-border-fg yellow
set -g status-fg black
set -g status-bg '#FEFE0A'
set -g message-fg black
set -g message-bg '#FEFE0A'
set -g message-command-fg black
set -g message-command-bg '#FEFE0A'
set-option -g mode-keys vi
set -g history-limit 5000
## mouse mode
set -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-select-window on
set -g mouse-resize-pane on # resize panes with mouse (drag borders)
## status
set-option -g status-interval 1
set-option -g status-left-length 20
set-option -g status-left ''
set-option -g status-right '%Y-%m-%dT%H%M%S '
## run programs in panes
# split left-right
split-window -h
# split left up
select-pane -t 0
split-window -v
select-pane -t 0
split-window -v
select-pane -t 0
split-window -v
select-pane -t 0
send-keys 'ranger' Enter
select-pane -t 2
send-keys 'htop' Enter
select-pane -t 3
send-keys 'elinks http://arxiv.org/list/hep-ph/new' Enter
select-pane -t 4
send-keys 'vi' Enter
set -g set-remain-on-exit off
EOF

我尝试通过以下方式使用此变量而不是文件:

I have tried using this variable instead of a file in the following way:

tmux -f <(${configurationTmux}) attach

这将导致以下错误:

bash: set: -g: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
no sessions

我在做什么错了?

推荐答案

流程替代运行一个流程.您正在给它一个shell变量,它将它解释为要运行的脚本(因此 set 错误,因为shell set 无法理解 -g 作为参数).

Process substitution runs a process. You are giving it a shell variable which it is interpreting as a script to run (hence the set error since shell set doesn't understand -g as an argument).

您需要将字符串作为流程替换的输出返回.< $(echo"$ configurationTmux")

You need your string returned as output from the process substitution. <$(echo "$configurationTmux")

话虽这么说,但如果tmux希望能够在配置文件中寻找或做其他事情,那将是行不通的.

That being said if tmux expects to be able to seek or do anything else fancy around the config file this will not work.

在一般情况下,也可以使用类似以下的内容(假设存在/dev/stdin 或可以找到类似的入口点/proc/self/fd/0 或类似的代码),并且这样做不会损害随后产生的进程,这些进程将/可能会继承标准输入(因此在 tmux 情况下显然没有用,谢谢@chepner).

In the general case something like the following can also be used (assuming /dev/stdin exists or a similar entry point can be found /proc/self/fd/0 or some-such) and that doing this doesn't harm subsequently spawned processes that will/may inherit standard input (so not useful in the tmux case apparently, thanks @chepner).

您可能还可以执行类似 echo"$ configurationTmux" |的操作.tmux -f/dev/stdin附上.

You can probably also do something like echo "$configurationTmux" | tmux -f /dev/stdin attach.

这篇关于如何使用Bash进程替换来使变量定义程序的配置而不是文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 12:43