笔者一直使用iTerm2作为日常工作的终端工具,用起来非常不错。突然到了某个的今天,想把一些脚本文件上传到服务器上的时候,自然而然的就想到了lrzsz。于是就登录到服务器上,然后输入rz命令,duang~duang~duang,iTerm2不但没有弹出文件选择窗口,还卡住了!
赶紧求助万能的谷歌,还好在Github上找到了一个解决方法,特此记录在案!
1️⃣ 安装lrzsz
在mac中先安装lrzsz:
brew install lrzsz
2️⃣ 创建rzsz脚本
在mac的/usr/local/bin/目录下,分别创建iterm2-send-zmodem.sh 和iterm2-recv-zmodem.sh两个脚本文件,并设置权限为777。
然后两个文件的内容分别如下:
iterm2-recv-zmodem.sh
#!/bin/bash
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 folder with prompt "Choose a folder to place received files in"' -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 folder with prompt "Choose a folder to place received files in"' -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
cd "$FILE"
/usr/local/bin/rz -E -e -b --bufsize 4096
sleep 1
echo
echo
echo \# Sent \-\> $FILE
fi
iterm2-recv-zmodem.sh
#!/bin/bash
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" --escape --binary --bufsize 4096
sleep 1
echo
echo \# Received $FILE
fi
3️⃣ 设置iTerm2
打开Settings->Profiles->Advanced,点击Edit。

在弹出框中新增两条Trigger。
Trigger填写的的内容分别如下:
第1个
- Regular expression: rz waiting to receive.**B0100
- Action: Run Silent Coprocess
- Parameters: /usr/local/bin/iterm2-send-zmodem.sh
- Instant: selected
第2个
- Regular expression: **B0100000023be50
- Action: Run Silent Coprocess
- Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
- Instant: selected
最后,再次在iTerm2中输入rz命令,就能正常弹出文件选择框了。
原创不易,如果觉得此文对你有帮助,不妨点赞+收藏+关注,你的鼓励是我支持创作的动力!

原创文章,作者:诺多,如若转载,请注明出处:https://www.huluohu.com/posts/1150/
微信扫一扫
支付宝扫一扫
评论列表(1条)
来看看 顺便学习学习