如何在tmux命令中使用shell变量?

时间:2016-05-06 10:37:50

标签: python bash shell tmux

我正在编写一个确定一些变量的shell脚本,例如:一个开放的港口。然后它打开一些tmux窗口,其中执行python程序。这些python程序应该将端口作为命令行参数,如下所示:

function find_open_port(){
    # Ports between 49152 - 65535 are usually unused.
    port=$(shuf -i '49152-65535' -n '1')

    # Then check if port is open
    if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
        find_open_port
    else
        # There is no service currently running on this port
        return $port
    fi
}

find_open_port
echo "Using port: $port"

tmux new-session -d -s '1' 'python server.py -p $port'
sleep 2
tmux split-window -v -t '1' 'python client.py -p $port'
sleep 1
tmux split-window -h -t '1' 'python client.py -p $port'

如果我将端口确定为整数而不是变量,它可以工作(例如1025),但是我想运行这个的并行实例,因此我需要随机选择几个不同的端口。 tmux命令似乎没有“获取”端口变量。

如何让tmux获取变量的值?

1 个答案:

答案 0 :(得分:2)

单引号不允许变量扩展,请使用双引号:

tmux split-window -v -t '1' "python client.py -p $port"