0

我有一个 Python 脚本,它定义了 tmux 的配​​置,然后使用此配置启动 tmux。配置在 Python 中存储为 heredoc,我想在为 tmux 执行 Bash 启动命令时在 Bash 进程替换中使用它。

所以,我想做这样的事情:

configuration = \
"""
set -g set-remain-on-exit on
new -s "FULL"
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 3
split-window -v
select-pane -t 0
send-keys 'ranger' Enter
select-pane -t 1
send-keys 'clear' 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 'cmus' Enter
select-pane -t 5
send-keys 'ranger' Enter
select-pane -t 4
set -g set-remain-on-exit off
"""

command = executable + " -f <(echo " + configuration + ") attach"
os.system(command)

我应该改变什么才能让它工作?

4

1 回答 1

2

Pythonos.system()是根据 C 的system()函数定义的,它通过/bin/sh -c <the command>. 即使 /bin/sh 是 bash 的别名,当 bash 以 启动时 sh它在 POSIX 模式下运行,并且 POSIX 不提供进程替换(<(<command>))。

看起来您最好的选择可能是将配置写入临时文件,然后在命令中指定该文件的名称。如果您有mktemp可用的命令,那么它是制作临时文件的理想选择。也许沿着这些思路可以做到这一点:

command = "config=`mktemp` && { echo \"" + configuration + "\" > $config; "
        + executable + " -f $config attach; unlink $config; }"

此外,即使您可以使用 bash 进程替换,它也是一个重定向。如果进程正在启动,它会影响标准输入(或标准输出),并且不会代替文件名。

于 2015-06-18T15:03:24.053 回答