RX文件管理器替代Windows资源管理器:5个必须知道的高级功能
2026/6/20 17:34:58
esh(Embedded SHell) 是一个轻量级的模板引擎,用于在任意模板中嵌入和执行 shell 命令。本文档系统性地介绍 esh 的核心概念、语法特性、高级技巧和实战应用,帮助开发者快速掌握配置文件动态生成和模板化处理的精髓。
# 在鸿蒙PC上执行tar-xzf ohos_esh_0.3.3.tar.gzcp-r esh_0.3.3/* /data/service/hnp/esh.org/esh_0.3.3/# 复制文件到安装目录mkdir-p /data/service/hnp/esh.org/esh_0.3.3/bincpbin/esh /data/service/hnp/esh.org/esh_0.3.3/bin/chmod+x /data/service/hnp/esh.org/esh_0.3.3/bin/esh# 添加到 PATHexportPATH=$PATH:/data/service/hnp/esh.org/esh_0.3.3/bin# 查看版本esh -V# 输出: esh 0.3.2# 查看帮助esh -h创建模板文件hello.esh:
Hello from<%=$USER%>!Today is<%=$(date+%Y-%m-%d)%>.执行模板:
esh hello.esh# 输出:# Hello from ohos!# Today is 2025-12-17.esh 支持以下模板语法:
| 语法 | 说明 |
|---|---|
<%= ... %> | 输出表达式结果 |
<% ... %> | 执行脚本块(不输出) |
<%+ ... %> | 包含其他模板文件 |
<%# ... %> | 注释(不会出现在输出中) |
<%- ... %> | 执行脚本块并去除前后空白 |
# 模板文件Hello<%=$USER%>!Home directory:<%=$HOME%># 命令行变量优先级高于环境变量esh template.eshAPP_NAME=MyAppAPP_PORT=8080<%name="OpenHarmony"-%>Welcome to<%=$name%>!<%if["$MODE"="debug"];then-%>debug_mode=truelog_level=debug<%else-%>debug_mode=falselog_level=info<%fi-%><%i=1;while[$i-le5];do-%>Item<%=$i%><%i=$(expr$i +1);done-%>创建模板文件hello.esh:
Hello from<%=$USER%>!Today is<%=$(date+%Y-%m-%d)%>.执行模板:
esh hello.esh输出:
Hello from ohos! Today is 2025-12-17.创建模板文件config.esh:
<%if["$MODE"="debug"];then-%>debug_mode=true<%else-%>debug_mode=false<%fi-%>执行模板:
# 调试模式MODE=debug esh config.esh# 生产模式MODE=production esh config.esh创建模板文件script.esh:
<%answer=42-%>The answer is:<%=$answer%>.<%if[$answer-gt40];then-%>The answer is greater than40.<%fi-%>执行模板:
esh script.esh输出:
The answer is: 42. The answer is greater than 40.echo"Hello <%= \$USER%>!"|esh -# 输出:# Hello ohos!创建模板文件app.esh:
App name:<%=$APP_NAME%>Port:<%=$APP_PORT%>执行模板:
# 命令行变量优先级高于环境变量esh app.eshAPP_NAME=MyAppAPP_PORT=8080输出:
App name: MyApp Port: 8080# 使用 -o 选项输出到文件esh config.esh -o /tmp/config.confcat/tmp/config.conf# 使用 -d 选项只转换不执行esh -d config.esh# 可以保存脚本并手动执行esh -d config.esh>config.shshconfig.sh创建主模板main.esh:
<%var1=42-%>Content from the file:<%+ incl/a.esh %>. The mainfilecontinues.创建包含文件incl/a.esh:
This isfileincl/a.esh. Variablevar1=<%=$var1%>执行模板:
esh main.esh输出:
Content from the file: This is file incl/a.esh. Variable var1=42 .The main file continues.This is visible text.<%# This is a comment and will not appear in output -%>This is also visible.<%# Multi-linecomment block -%>Final visible text.输出:
This is visible text. This is also visible. Final visible text.# 指定使用的 shellexportESH_SHELL=/bin/bash esh template.esh# 指定使用的 awkexportESH_AWK=gawk esh template.esh# 设置最大包含深度exportESH_MAX_DEPTH=5esh template.eshesh 提供详细的错误信息:
# 语法错误esh invalid.esh# 输出:esh: syntax error at line 5: unexpected token# 文件不存在esh missing.esh# 输出:esh: can't read missing.esh: not a file or not readable创建nginx.conf.esh:
http{access_log<%=$logs_dir%>/access.log main;server{listen<%=$port%>;server_name<%=$server_name%>;root<%=$document_root%>;<%if["$ssl"="true"];then-%>ssl_certificate<%=$ssl_cert%>;ssl_certificate_key<%=$ssl_key%>;<%fi-%>}}生成配置:
esh nginx.conf.esh\logs_dir=/var/log/nginx\port=80\server_name=example.com\document_root=/var/www/html\ssl=false\>/etc/nginx/nginx.conf创建service.unit.esh:
[Unit]Description=<%=$service_name%>ServiceAfter=network.target[Service]Type=simpleUser=<%=$run_user%>ExecStart=<%=$exec_path%>Restart=alwaysRestartSec=5[Install]WantedBy=multi-user.target生成服务文件:
esh service.unit.esh\service_name=myapp\run_user=ohos\exec_path=/usr/local/bin/myapp\>/etc/systemd/system/myapp.service创建ci-config.esh:
APP_NAME=<%=$CI_PROJECT_NAME%>BUILD_NUMBER=<%=$CI_BUILD_NUMBER%>DEPLOY_PATH=/opt/apps/<%=$APP_NAME%>BUILD_TIME=<%=$(date+%Y-%m-%d\%H:%M:%S)%>生成配置:
esh ci-config.esh\CI_PROJECT_NAME=myapp\CI_BUILD_NUMBER=123\>/tmp/ci-config.env创建system-config.esh:
<%if["$ENV"="production"];then-%>LOG_LEVEL=infoDEBUG=falseMAX_WORKERS=10<%else-%>LOG_LEVEL=debugDEBUG=trueMAX_WORKERS=2<%fi-%>HOST=<%=$HOST%>PORT=<%=$PORT%>生成配置:
# 生产环境ENV=production esh system-config.eshHOST=0.0.0.0PORT=8080>/etc/myapp.conf# 开发环境ENV=development esh system-config.eshHOST=localhostPORT=3000>/etc/myapp.conf问题:变量未定义时,输出为空。
解决:使用默认值或条件判断:
<%if[-n"$VAR"];then-%>Value:<%=$VAR%><%else-%>Value: default<%fi-%>问题:特殊字符需要转义。
解决:在模板中使用转义字符:
# 在模板中Price:\$<%=$price%># 在命令行中echo"Price: <%= \$price%>"|esh -问题:包含文件路径不正确。
解决:使用相对路径或绝对路径:
# 相对路径(相对于模板文件)<%+../common/header.esh %># 绝对路径<%+ /etc/templates/header.esh %>问题:处理大文件时性能较慢。
解决:
-d选项预生成脚本# 预生成脚本esh -d large.esh>large.shshlarge.shesh 是一个功能强大且轻量级的模板引擎,通过简单的语法就能实现复杂的模板处理。主要优势:
通过本教程,您应该能够: