electerm的快速命令能提高效率,但是有批量命令想添加,手动添加还是嫌慢,如果能找到快速命令配置文件位置,直接循环填入就好了。
在软件页面添加快速命令和使用的方法:
在网上查找快速命令配置文件位置没找到,猜想软件会把用户添加的快速命令保存在某个配置文件中,直接查找软件使用目录(日志目录所在的目录)下最新更新的文件就应该能找到了。
这里用python代码来查找:
import os
from datetime import datetime
# 指定目标目录
directory = r"C:\Users\用户名\AppData\Roaming\electerm"
# 获取所有文件的路径,包括子目录
files = []
for root, dirs, filenames in os.walk(directory):
for filename in filenames:
files.append(os.path.join(root, filename))
# 获取文件的更新时间并排序
files_sorted = sorted(files, key=lambda x: os.path.getmtime(x), reverse=True)
# 输出排序后的文件及其更新时间
for file in files_sorted[:10]:
mod_time = datetime.fromtimestamp(os.path.getmtime(file))
print(f"{file} - Last Modified: {mod_time}")
查看输出并确认,electerm的快速命令配置文件位置在
C:\Users\用户名\AppData\Roaming\electerm\users\default_user\electerm.quickCommands.nedb
查看里面的内容如下
{"_id":"iZOsU1a","name":"source source_glm.sh","commands":[{"command":"source source_glm.sh","id":"LQO7LJc","delay":100}],"labels":[],"clickCount":1}
{"_id":"test12","name":"echo123","commands":[{"command":"echo 1234","id":"test34","delay":100}],"labels":[],"clickCount":3}
{"_id":"test12","name":"echo123","commands":[{"command":"echo 1234","id":"test34","delay":100}],"labels":[],"clickCount":4}
{"_id":"lNi110O","name":"进入abef","commands":[{"command":"cd ab/ef","id":"RdGBVpN","delay":100}],"inputOnly":true,"labels":[]}
{"_id":"lNi110O","name":"进入abef","commands":[{"command":"cd ab/ef","id":"RdGBVpN","delay":100}],"inputOnly":true,"labels":[],"clickCount":1}
{"_id":"lNi110O","name":"进入abef","commands":[{"command":"cd ab/ef","id":"RdGBVpN","delay":100}],"inputOnly":true,"labels":[],"clickCount":2}
{"_id":"test12","name":"echo123","commands":[{"command":"echo 1234","id":"test34","delay":100}],"labels":[],"clickCount":5}
{"_id":"lNi110O","name":"进入abef","commands":[{"command":"cd ab/ef","id":"RdGBVpN","delay":100}],"inputOnly":true,"labels":[],"clickCount":3}
经过实验和分析得知:
- 软件页面新建快速命令后,配置文件会增加一行命令信息,其中没有clickCount属性
- 点击命令后,clickCount从无到有且值为1,之后每次点击增加1
- 退出软件时文件不变化,启动后文件中同一个命令只保留一行命令信息,含有最后的clickCount属性
也就是我们想批量添加快速命令,只需要一行信息就够了。经过测试,id的值可以自己给定。下面用python程序将命令列表转换为配置文件需要的格式。
cmds列表中的每行中,第一个元素为1表示"inputOnly":true,否则不添加"inputOnly"属性。
第二个元素是command的值。
第三个元素是name的值,如果不存在,就采用第二个元素。
_id和id分别是test01和_test01,数字递增。
import json
# 输入的命令列表
cmds = [
[0,"source source_glm.sh"],
[0,"deactivate"],
[0,"systemctl restart nginx"],
[0,"nginx -t"],
[0,"ps aux|grep nginx"],
[1,"nohup python basic_demo/web_demo_gradio.py &"],
[0,"ps aux | grep python"],
[0,"nohup jupyter notebook --allow-root &",'nohup-jupyter'],
]
# 生成的命令配置列表
commands = []
# ID初始值
base_id = 1
for idx, cmd in enumerate(cmds):
input_only = cmd[0] == 1
command = cmd[1]
name = cmd[2] if len(cmd) > 2 else command
# 生成_id和id
_id = f"_test{base_id:02d}"
command_id = f"test{base_id:02d}"
# 创建命令配置字典
command_config = {
"_id": _id,
"name": name,
"commands": [{"command": command, "id": command_id, "delay": 100}],
"labels": [],
}
# 根据inputOnly的值进行设置
if input_only:
command_config["inputOnly"] = True
# 添加到命令配置列表
commands.append(command_config)
# 增加ID
base_id += 1
# 将JSON格式的命令配置转换为紧凑的单行格式
compact_json = [json.dumps(command, ensure_ascii=False) for command in commands]
for line in compact_json:
print(line)
运行结果:
{"_id": "_test01", "name": "source source_glm.sh", "commands": [{"command": "source source_glm.sh", "id": "test01", "delay": 100}], "labels": []}
{"_id": "_test02", "name": "deactivate", "commands": [{"command": "deactivate", "id": "test02", "delay": 100}], "labels": []}
{"_id": "_test03", "name": "systemctl restart nginx", "commands": [{"command": "systemctl restart nginx", "id": "test03", "delay": 100}], "labels": []}
{"_id": "_test04", "name": "nginx -t", "commands": [{"command": "nginx -t", "id": "test04", "delay": 100}], "labels": []}
{"_id": "_test05", "name": "ps aux|grep nginx", "commands": [{"command": "ps aux|grep nginx", "id": "test05", "delay": 100}], "labels": []}
{"_id": "_test06", "name": "nohup python basic_demo/web_demo_gradio.py &", "commands": [{"command": "nohup python basic_demo/web_demo_gradio.py &", "id": "test06", "delay": 100}], "labels": [], "inputOnly": true}
{"_id": "_test07", "name": "ps aux | grep python", "commands": [{"command": "ps aux | grep python", "id": "test07", "delay": 100}], "labels": []}
{"_id": "_test08", "name": "nohup-jupyter", "commands": [{"command": "nohup jupyter notebook --allow-root &", "id": "test08", "delay": 100}], "labels": []}
添加到上述的C:\Users\用户名\AppData\Roaming\electerm\users\default_user\electerm.quickCommands.nedb
文件中,退出软件重新打开就能看到新的命令了。