iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >shell中长命令的换行处理方法示例
  • 932
分享到

shell中长命令的换行处理方法示例

shell命令换行shell太长换行shell长命令换行 2022-06-04 22:06:31 932人浏览 安东尼
摘要

前言 考察下面的脚本: emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --source-map-bas

前言

察下面的脚本:


emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --source-map-base dist -s MODULARIZE=1 -s "EXPORT_NAME=\"Test\"" -s USE_SDL=2 -s LEGACY_GL_EMULATioN=1 --pre-js ./pre.js --post-js ./post.js --cpuprofiler --memoryprofiler --threadprofilermain.cpp

这里在调用 emcc 进行 WEBAssembly 编译时,组织了很多参数。整个命令都在一行之中,不是很好阅读和维护。

换行

可通过加 \ 的方式来进行换行拆分。

改造后看起来像这样,一个参数占一行:


emcc -o ./dist/test.html\
 --shell-file ./tmp.html\
 --source-map-base dist\
 -O3\
 -g4\
 --source-map-base dist\
 -s MODULARIZE=1\
 -s "EXPORT_NAME=\"Test\""\
 -s USE_SDL=2\
 -s LEGACY_GL_EMULATION=1\
 --pre-js ./pre.js\
 --post-js ./post.js\
 --cpuprofiler\
 --memoryprofiler\
 --threadprofiler\
 main.cpp

注释

通过 \(backslash) 换行后,整体阅读体验好了很多。进一步,我们想要为每个参数添加注释,发现不能简单地这样来:


emcc -o ./dist/test.html\ # 目标文件
 --shell-file ./tmp.html\ # 模板文件
 --source-map-base dist\
 -O3\
 -g4\
 --source-map-base dist\
 -s MODULARIZE=1\
 -s "EXPORT_NAME=\"Test\""\
 -s USE_SDL=2\
 -s LEGACY_GL_EMULATION=1\
 --pre-js ./pre.js\
 --post-js ./post.js\
 --cpuprofiler\
 --memoryprofiler\
 --threadprofiler\
 main.cpp

这样会导致整个 shell 脚本解析失败。

实测发现,也不能这样:


emcc -o\
 # 目标文件
 ./dist/test.html\ 
  # 模板文件
 --shell-file ./tmp.html\
 --source-map-base dist\
 -O3\
 -g4\
 --source-map-base dist\
 -s MODULARIZE=1\
 -s "EXPORT_NAME=\"Test\""\
 -s USE_SDL=2\
 -s LEGACY_GL_EMULATION=1\
 --pre-js ./pre.js\
 --post-js ./post.js\
 --cpuprofiler\
 --memoryprofiler\
 --threadprofiler\
 main.cpp

同样会导致解析失败。

说到底,通过 \ 拆分的命令,只是呈现上变成了多行,其中插入的注释是会破坏掉语义的。

但也不是没办法添加注释了,几经周转发现如下写法是可行的:


emcc -o ./dist/test.html `# 目标文件` \
 --shell-file ./tmp.html `# 模板文件` \
 --source-map-base dist `# source map 根路径` \
 -O3 `# 优化级别` \
 -g4 `# 生成 debug 信息` \
 --source-map-base dist\
 `# -s MODULARIZE=1\`
 -s "EXPORT_NAME=\"Test\""\
 -s USE_SDL=2\
 -s LEGACY_GL_EMULATION=1\
 --pre-js ./pre.js\
 --post-js ./post.js\
 --cpuprofiler\
 --memoryprofiler\
 --threadprofiler\
 main.cpp

即通过 `(backtick) 来包裹我们的注释,就不会破坏掉脚本的语义了,能够正确解析执行。

进一步,解决了注释的问题,如果我们不想要某一行,同时又不想删除,可以像下面这样来注释:


emcc -o ./dist/test.html `# 目标文件` \
 --shell-file ./tmp.html `# 模板文件` \
 --source-map-base dist `# source map 根路径` \
 -O3 `# 优化级别` \
 -g4 `# 生成 debug 信息` \
 --source-map-base dist\
 -s MODULARIZE=1\
 -s "EXPORT_NAME=\"Test\""\
 -s USE_SDL=2\
 -s LEGACY_GL_EMULATION=1\
 `# --pre-js ./pre.js`\
 --post-js ./post.js\
 --cpuprofiler\
 `# --threadprofiler`\
 --memoryprofiler\
 main.cpp

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参eBkev考学习价值,谢谢大家对我们的支持。

--结束END--

本文标题: shell中长命令的换行处理方法示例

本文链接: https://www.lsjlt.com/news/20444.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作