rz/sz 命令卡住的解决

通过 rz/sz 向服务器传输文件时,文件选择框没弹起,而是出现等待卡死的界面:

$ rz -be
�z waiting to receive.**B0100000063f694

奇怪的是首次在本机 mac 上通过 iTerm2 使用时是成功的,过了几天才出现的该问题。

无奈查看解决方案,发现需要进行如下一系列操作。

安装 lrzsz

首先需要在本机安装 rz/sz 命令。

$ brew install lrzsz

检查安装:

$ rz --version                                                                                          10:29:13
rz (lrzsz) 0.12.20

准备脚本

cd 到 /usr/local/bin 下,准备好 rz/sz 时需要执行的两个脚本:

$ cd /usr/local/bin
$ touch iterm2-send-zmodem.sh iterm2-recv-zmodem.sh

其中 iterm2-send-zmodem.sh 写入如下内容:

iterm2-send-zmodem.sh
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
    FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
    FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
    echo Cancelled.
    # Send ZModem cancel
    echo -e \\x18\\x18\\x18\\x18\\x18
    sleep 1
    echo
    echo \# Cancelled transfer
else
    /usr/local/bin/sz "$FILE" -e -b
    sleep 1
    echo
    echo \# Received $FILE
fi

iterm2-recv-zmodem.sh 写入如下内容:

iterm2-recv-zmodem.sh
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
    FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
    FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
    echo Cancelled.
    # Send ZModem cancel
    echo -e \\x18\\x18\\x18\\x18\\x18
    sleep 1
    echo
    echo \# Cancelled transfer
else
    /usr/local/bin/sz "$FILE" -e -b
    sleep 1
    echo
    echo \# Received $FILE
fi

赋予脚本可执行权限

脚本准备好之后,需要通过 chmod 为其赋予可选择权限。

$ sudo chmod 777 /usr/local/bin/iterm2-*

配置 iTerm2

command + , 打开设置后依次进入 Profiles -> Default -> Advanced -> Triggers -> Edit,打开触发器的编辑界面。

依次添加两个触发器,配置如下:

Regular expression: rz waiting to receive.\*\*B0100
            Action: Run Silent Coprocess
        Parameters: /usr/local/bin/iterm2-send-zmodem.sh
           Instant: checked

Regular expression: \*\*B00000000000000
            Action: Run Silent Coprocess
        Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
           Instant: checked

iTerm2 触发器编辑界面

iTerm2 触发器编辑界面

相关资源