<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Kai Lin</title>
<link>https://demo1.661366.xyz/</link>
<description>Kai Lin 的个人网站。Java 全栈开发，写后端、架构和折腾服务器的笔记，也在这里放自己做的东西。</description>
<language>zh-CN</language>
<atom:link href="https://demo1.661366.xyz/rss.xml" rel="self" type="application/rss+xml"/>
<lastBuildDate>Tue, 24 Nov 2026 14:19:48 GMT</lastBuildDate>
<item><title>从零搭建这个网站</title><link>https://demo1.661366.xyz/blog/building-this-site/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/building-this-site/</guid><pubDate>Sat, 07 Nov 2026 16:00:00 GMT</pubDate><description>这个网站是怎么做出来的：不用前端框架的静态构建、Java 写的后台和评论、git push 上线，以及最近一次把主页从「什么都想放」删到只剩要紧的东西。</description><content:encoded><![CDATA[<p>想要一个自己的网站很久了。用过几个现成的博客程序，总有些地方不顺手：主题改起来要先学一套模板语法，插件一多就慢，数据放在别人的平台上也不踏实。最后决定自己写一个，要求只有三条：</p>
<ul>
<li><strong>快</strong>：访客打开的是现成的静态页面，不在服务器上临时拼。</li>
<li><strong>能自己掌控</strong>：代码、文章、图片、数据库都在自己的服务器上，换地方的时候整个搬走。</li>
<li><strong>好看，但不花哨</strong>：参照苹果和 SpaceX 的网页，干净、留白多、细节到位。</li>
</ul>
<p>这篇按做的顺序记下来，也算是这个网站的说明书。</p>
<h2 id="整体结构">整体结构</h2>
<p>网站分成两半：访客看到的全是<strong>静态文件</strong>，写文章和评论这两件需要“动”的事交给<strong>两个 Java 小服务</strong>。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:68"><div class="code-head"><span class="code-lang">纯文本</span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">             ┌───────── 我在后台写 ─────────┐</span>
<span class="line">             ▼                              │ 只有我知道的地址</span>
<span class="line">   PostgreSQL（文章、历史版本、评论）    后台服务（Java）</span>
<span class="line">             │ 点「发布」                    │</span>
<span class="line">             ▼                              │</span>
<span class="line">  导出文章 → Node 构建静态页面 → /srv/blog/live ◀── Nginx ◀── 访客</span>
<span class="line">                                                 │ /api/comments</span>
<span class="line">                                                 ▼</span>
<span class="line">                                       评论服务（Java）── PostgreSQL</span></code></pre></div>
<p>这样分有一个很实在的好处：数据库或者 Java 服务停了，文章照样能看，只有评论区会暂时隐藏。</p>
<h2 id="不用前端框架">不用前端框架</h2>
<p>页面模板就是普通的 JavaScript 模板字符串，构建脚本 <code>scripts/build.mjs</code> 读 Markdown、套模板、写出 HTML。整个前端只依赖三个包：<code>marked</code> 解析 Markdown，<code>highlight.js</code> 做代码高亮，<code>sharp</code> 处理图片。</p>
<p>不用框架不是为了标新立异，而是这个网站本来就不需要：页面上没有复杂的状态，几个交互（翻页、目录、图片全屏、评论）各写一个小脚本就够了。结果是首页的 JavaScript 只有十几 KB，关掉 JavaScript 也能正常阅读。</p>
<aside class="callout is-tip" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.6.5 1 1.2 1 2V16h5.2v-.2c0-.8.4-1.5 1-2A6 6 0 0 0 12 3z"/></svg>提示</p><p>代码高亮是在构建时做好的，配色照着 Xcode 的默认主题，亮色暗色各一套。访客不用为它多下载任何脚本。</p>
</aside>
<h2 id="图片-只下载用得到的那一张">图片：只下载用得到的那一张</h2>
<p>文章里的每张图片在构建时都会：</p>
<ol>
<li>缩成 480、800、1200、1600 四种宽度；</li>
<li>转成 AVIF 和 WebP，浏览器挑自己支持的、适合屏幕的那一张；</li>
<li>写好宽高，加载时页面不会跳；</li>
<li>去掉 EXIF 里的拍摄地点和手机型号。</li>
</ol>
<p>处理结果缓存在 <code>.cache/</code> 里，同一张图只处理一次。</p>
<h2 id="中文字体-只切用得到的字">中文字体：只切用得到的字</h2>
<p>这是最近才加上的。苹果设备上中文用的是苹方，本来就很好看；但在 Windows 上会落到微软雅黑，小字号时边缘发虚，笔画也不够饱满。</p>
<p>想换成思源黑体，问题是一套中文字体有好几 MB。解决办法是<strong>只切用得到的字</strong>：构建时统计每一页出现的汉字，很多页面都用到的字放进一个共用文件，缓存之后换页不用再下；其余的字每页单独切一小份。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:97"><div class="code-head"><span class="code-title">scripts/fonts.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">cut</span>(<span class="hljs-params">chars, weight</span>) {</span>
<span class="line">  <span class="hljs-keyword">const</span> text = [...chars].<span class="hljs-title function_ invoke__">sort</span>().<span class="hljs-title function_ invoke__">join</span>(<span class="hljs-string">&#x27;&#x27;</span>);</span>
<span class="line">  <span class="hljs-keyword">const</span> key = <span class="hljs-title function_ invoke__">createHash</span>(<span class="hljs-string">&#x27;sha256&#x27;</span>).<span class="hljs-title function_ invoke__">update</span>(<span class="hljs-string">`<span class="hljs-subst">${weight}</span>:<span class="hljs-subst">${text}</span>`</span>).<span class="hljs-title function_ invoke__">digest</span>(<span class="hljs-string">&#x27;hex&#x27;</span>).<span class="hljs-title function_ invoke__">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">12</span>);</span>
<span class="line">  <span class="hljs-comment">// 同样的字、同样的字重只切一次，结果缓存在 .cache/fonts</span></span>
<span class="line">  buf = <span class="hljs-keyword">await</span> <span class="hljs-title function_ invoke__">subsetFont</span>(font, text, { <span class="hljs-attr">targetFormat</span>: <span class="hljs-string">&#x27;woff2&#x27;</span>, <span class="hljs-attr">variationAxes</span>: { <span class="hljs-attr">wght</span>: weight } });</span>
<span class="line">}</span></code></pre></div>
<p>字重也分两种：常规字重覆盖所有的字，半粗字重只包含标题和加粗文字里的字。苹果设备继续用系统自带的苹方，什么都不下载。</p>
<h2 id="上线-git-push-就够了">上线：git push 就够了</h2>
<p>服务器上有一个 Git 仓库，推送代码后钩子自动构建。构建到一个带时间戳的新目录，成功了才把网站指过去：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:68"><div class="code-head"><span class="code-title">deploy/build-site.sh</span><span class="code-side"><span class="code-lang">Shell</span></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 先建好新的链接，再用 rename 一步替换：访客不会遇到“换到一半”的状态</span></span>
<span class="line"><span class="hljs-built_in">ln</span> -sfn <span class="hljs-string">&quot;<span class="hljs-variable">$BUILDS</span>/<span class="hljs-variable">$stamp</span>&quot;</span> <span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/.live.new&quot;</span></span>
<span class="line"><span class="hljs-built_in">mv</span> -T <span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/.live.new&quot;</span> <span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/live&quot;</span></span></code></pre></div>
<p>构建失败什么都不变，网站还是上一个版本。最近 5 个版本都留着，后台里点一下就能切回去。</p>
<aside class="callout is-note" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 11v5"/><path d="M12 7.6v.4"/></svg>说明</p><p>文章不在 Git 仓库里，而是存在数据库中，所以代码可以放心开源，草稿不会跟着出去。</p>
</aside>
<h2 id="后台和安全">后台和安全</h2>
<p>后台在一串随机生成的地址下面，登录要密码加手机验证码。常见的后台地址，比如 <code>/admin/</code>、<code>/wp-login.php</code>，放的是一个永远登录不进去的假登录页，用来记录每天来试探的扫描器。</p>
<p>两个 Java 服务都不依赖任何第三方库，连 PostgreSQL 的通信协议都是自己实现的，几百行代码。这样做最大的好处是：不会因为某个依赖爆出漏洞而被迫升级。</p>
<h2 id="把主页删到只剩要紧的">把主页删到只剩要紧的</h2>
<p>做到后来，主页越来越满：首屏底下有一条仿 SpaceX 直播的遥测条，弧线、时钟、四个仪表盘；每一屏都有编号和巨大的标题；文章、作品、服务都套着带阴影的卡片。每个单独看都不错，放在一起就吵了。</p>
<p>这一次的改法只有减法：</p>
<ul>
<li>遥测条缩成角落里的一行小字：城市、时间、气温、下一次日落；</li>
<li>标题从 84 像素缩到正文大小，首屏的名字成了页面上唯一的大字；</li>
<li>卡片全部去掉，换成一条 1 像素的分隔线；</li>
<li>整页只用三种字号，可以点的东西只用一种颜色，也就是苹果的链接蓝。</li>
</ul>
<p>改完再看，内容本身反而更清楚了。留白不是浪费空间，它告诉你哪些东西是一组的。</p>
<h2 id="接下来">接下来</h2>
<ul>
<li>给每个作品写一页详细介绍，用和文章一样的排版；</li>
<li>作品截图统一成 16:10、浅灰背景；</li>
<li>把评论区的样式也按这次的规矩再过一遍。</li>
</ul>
<p>如果你也想要一个自己的网站，希望这篇能省你一点时间。有问题欢迎在下面留言，或者到 <a href="https://github.com/2738506523" target="_blank" rel="noopener">GitHub</a> 找我。</p>
]]></content:encoded><category>前端</category><category>架构</category><category>博客</category></item>
<item><title>这个博客是怎么做的：技术栈与实现细节</title><link>https://demo1.661366.xyz/blog/how-this-blog-works/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/how-this-blog-works/</guid><pubDate>Sat, 07 Nov 2026 16:00:00 GMT</pubDate><description>一篇长文，把这个网站从里到外拆开讲一遍：构建脚本、Markdown 与代码高亮、图片和中文字体的处理、WebGL 首屏、明暗切换动画、零依赖的 Java 后端、自己实现的 PostgreSQL 客户端、部署与安全。附部分实现代码。</description><content:encoded><![CDATA[<p><a href="https://demo1.661366.xyz/blog/building-this-site/" data-ref="building-this-site">上一篇</a>讲了为什么要自己做这个网站、大致的思路。这一篇把它从里到外拆开，每一部分讲清楚用了什么、为什么这样做，再贴上关键的实现代码。篇幅比较长，左边的刻度就是目录，鼠标移上去（手机上点一下）可以直接跳到想看的部分。</p>
<aside class="callout is-note" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 11v5"/><path d="M12 7.6v.4"/></svg>说明</p><p>文中的代码都摘自网站本身的源码，为了好读删掉了一部分细节，删掉的地方用 <code>// …</code> 标出来。</p>
</aside>
<h2 id="技术栈一览">技术栈一览</h2>
<p>先列一张总表。整个网站分成三块：生成页面的<strong>构建脚本</strong>、浏览器里运行的<strong>前端脚本</strong>、服务器上的<strong>两个 Java 服务</strong>。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>部分</th>
<th>用了什么</th>
<th>第三方依赖</th>
</tr>
</thead>
<tbody><tr>
<td>构建</td>
<td>Node.js 20+，ES 模块</td>
<td>marked、highlight.js、sharp、subset-font</td>
</tr>
<tr>
<td>页面模板</td>
<td>JavaScript 模板字符串</td>
<td>无</td>
</tr>
<tr>
<td>样式</td>
<td>原生 CSS，CSS 变量做主题</td>
<td>无</td>
</tr>
<tr>
<td>前端交互</td>
<td>原生 JavaScript，按页面打包成一个文件</td>
<td>无</td>
</tr>
<tr>
<td>首屏</td>
<td>WebGL 片元着色器</td>
<td>无</td>
</tr>
<tr>
<td>评论和后台</td>
<td>Java 21，JDK 自带的 <code>HttpServer</code></td>
<td><strong>无</strong>（连数据库驱动都没有）</td>
</tr>
<tr>
<td>数据库</td>
<td>PostgreSQL 14+</td>
<td>—</td>
</tr>
<tr>
<td>部署</td>
<td>Git 钩子 + Bash，Nginx 或 Caddy</td>
<td>—</td>
</tr>
</tbody></table></div>
<p>依赖少是刻意的。前端构建的四个包都只在我自己的电脑和服务器上运行，访客的浏览器不会下载任何第三方代码；后端干脆一个依赖都没有，所以不存在“某个库爆出漏洞、被迫连夜升级”的情况。</p>
<h3 id="目录结构">目录结构</h3>
<div class="codeblock has-lines" style="--ln:2;--rows:13;--cols:75"><div class="code-head"><span class="code-lang">纯文本</span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">site.config.mjs        域名、语言、评论和后台开关</span>
<span class="line">content/</span>
<span class="line">  site.json            主页上的文字</span>
<span class="line">  blog/*.md            文章（本地预览用；线上的文章在数据库里）</span>
<span class="line">src/</span>
<span class="line">  templates/           页面模板：home.mjs、blog.mjs、layout.mjs</span>
<span class="line">  styles/              base.css 全站 · home.css 主页 · blog.css 博客</span>
<span class="line">  scripts/             浏览器里跑的脚本：明暗切换、着色器、刻度导航……</span>
<span class="line">  fonts/               Inter 子集 + 思源黑体（只在构建时用）</span>
<span class="line">  public/              原样复制到网站根目录的文件</span>
<span class="line">scripts/               构建：build.mjs、highlight.mjs、media.mjs、fonts.mjs</span>
<span class="line">server/                Java 服务：src/、schema.sql、systemd 配置</span>
<span class="line">deploy/                Nginx / Caddy 配置、部署钩子、备份脚本</span></code></pre></div>
<h2 id="构建-从-markdown-到静态页面">构建：从 Markdown 到静态页面</h2>
<p>构建脚本 <code>scripts/build.mjs</code> 四百多行，做的事情按顺序是：</p>
<ol>
<li>读出 <code>content/blog/</code> 里的每个 Markdown 文件，解析开头的文章信息；</li>
<li>用 marked 把正文转成 HTML，再做几处加工（标题加锚点、提示框、外链加箭头）；</li>
<li>处理文章里引用的图片和视频；</li>
<li>打包 CSS 和 JavaScript，文件名里带上内容哈希；</li>
<li>用模板生成每一个页面，统计用到的汉字，切出共用的和每页自己的中文字体；</li>
<li>生成 RSS、sitemap、robots.txt；</li>
<li>正式构建时把所有文本文件预先压缩成 <code>.br</code> 和 <code>.gz</code>。</li>
</ol>
<h3 id="文章信息">文章信息</h3>
<p>文件开头两条 <code>---</code> 之间是文章信息。完整的 YAML 解析器太重了，这里只需要“键: 值”和简单的列表，十几行就能写完：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:21;--cols:118"><div class="code-head"><span class="code-title">scripts/build.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">function</span> <span class="hljs-title function_">frontmatter</span>(<span class="hljs-params">src</span>) {</span>
<span class="line">  <span class="hljs-keyword">const</span> m = <span class="hljs-regexp">/^---\r?\n([\s\S]*?)\r?\n---\r?\n?/</span>.<span class="hljs-title function_ invoke__">exec</span>(src);</span>
<span class="line">  <span class="hljs-keyword">if</span> (!m) <span class="hljs-keyword">return</span> { <span class="hljs-attr">data</span>: {}, <span class="hljs-attr">body</span>: src };</span>
<span class="line">  <span class="hljs-keyword">const</span> <span class="hljs-title function_ invoke__">unq</span> = (<span class="hljs-params">v</span>) =&gt; v.<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/^([&#x27;&quot;])(.*)\1$/</span>, <span class="hljs-string">&#x27;$2&#x27;</span>);</span>
<span class="line">  <span class="hljs-keyword">const</span> data = {};</span>
<span class="line">  <span class="hljs-keyword">let</span> key = <span class="hljs-literal">null</span>;</span>
<span class="line">  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> m[<span class="hljs-number">1</span>].<span class="hljs-title function_ invoke__">split</span>(<span class="hljs-regexp">/\r?\n/</span>)) {</span>
<span class="line">    <span class="hljs-keyword">if</span> (!line.<span class="hljs-title function_ invoke__">trim</span>() || line.<span class="hljs-title function_ invoke__">trim</span>().<span class="hljs-title function_ invoke__">startsWith</span>(<span class="hljs-string">&#x27;#&#x27;</span>)) <span class="hljs-keyword">continue</span>;</span>
<span class="line">    <span class="hljs-keyword">const</span> item = <span class="hljs-regexp">/^\s*-\s+(.*)$/</span>.<span class="hljs-title function_ invoke__">exec</span>(line);</span>
<span class="line">    <span class="hljs-keyword">if</span> (item &amp;&amp; key) { <span class="hljs-keyword">if</span> (!<span class="hljs-title class_">Array</span>.<span class="hljs-title function_ invoke__">isArray</span>(data[key])) data[key] = []; data[key].<span class="hljs-title function_ invoke__">push</span>(<span class="hljs-title function_ invoke__">unq</span>(item[<span class="hljs-number">1</span>].<span class="hljs-title function_ invoke__">trim</span>())); <span class="hljs-keyword">continue</span>; }</span>
<span class="line">    <span class="hljs-keyword">const</span> kv = <span class="hljs-regexp">/^([A-Za-z_][\w-]*)\s*:\s*(.*)$/</span>.<span class="hljs-title function_ invoke__">exec</span>(line);</span>
<span class="line">    <span class="hljs-keyword">if</span> (!kv) <span class="hljs-keyword">continue</span>;</span>
<span class="line">    key = kv[<span class="hljs-number">1</span>];</span>
<span class="line">    <span class="hljs-keyword">const</span> v = kv[<span class="hljs-number">2</span>].<span class="hljs-title function_ invoke__">trim</span>();</span>
<span class="line">    <span class="hljs-keyword">if</span> (v === <span class="hljs-string">&#x27;&#x27;</span>) data[key] = [];</span>
<span class="line">    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regexp">/^\[.*\]$/</span>.<span class="hljs-title function_ invoke__">test</span>(v)) data[key] = v.<span class="hljs-title function_ invoke__">slice</span>(<span class="hljs-number">1</span>, -<span class="hljs-number">1</span>).<span class="hljs-title function_ invoke__">split</span>(<span class="hljs-string">&#x27;,&#x27;</span>).<span class="hljs-title function_ invoke__">map</span>(<span class="hljs-function">(<span class="hljs-params">s</span>) =&gt;</span> <span class="hljs-title function_ invoke__">unq</span>(s.<span class="hljs-title function_ invoke__">trim</span>())).<span class="hljs-title function_ invoke__">filter</span>(<span class="hljs-title class_">Boolean</span>);</span>
<span class="line">    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (v === <span class="hljs-string">&#x27;true&#x27;</span> || v === <span class="hljs-string">&#x27;false&#x27;</span>) data[key] = v === <span class="hljs-string">&#x27;true&#x27;</span>;</span>
<span class="line">    <span class="hljs-keyword">else</span> data[key] = <span class="hljs-title function_ invoke__">unq</span>(v);</span>
<span class="line">  }</span>
<span class="line">  <span class="hljs-keyword">return</span> { data, <span class="hljs-attr">body</span>: src.<span class="hljs-title function_ invoke__">slice</span>(m[<span class="hljs-number">0</span>].<span class="hljs-property">length</span>) };</span>
<span class="line">}</span></code></pre></div>
<p>阅读时间也是在这里算的：中文按每分钟 400 字，英文按每分钟 220 个词，两者相加。</p>
<h3 id="加工-html">加工 HTML</h3>
<p>marked 输出的是很“素”的 HTML，构建时再加几道工序。比如 GitHub 风格的提示框：引用块第一行写
<code>[!TIP]</code>，就换成带图标的 <code>&lt;aside&gt;</code>；指向别的网站的链接自动加上 <code>target=&quot;_blank&quot;</code>：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:12;--cols:123"><div class="code-head"><span class="code-title">scripts/build.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">function</span> <span class="hljs-title function_">enhance</span>(<span class="hljs-params">html</span>) {</span>
<span class="line">  <span class="hljs-keyword">return</span> html</span>
<span class="line">    <span class="hljs-comment">// 正文里的换行出现在两个汉字之间时，浏览器会显示成一个空格：直接去掉</span></span>
<span class="line">    .<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;(p|li|td|th)&gt;([\s\S]*?)&lt;\/\1&gt;/g</span>, <span class="hljs-function">(<span class="hljs-params">m, tag, inner</span>) =&gt;</span> <span class="hljs-string">`&lt;<span class="hljs-subst">${tag}</span>&gt;<span class="hljs-subst">${inner.<span class="x-f">replace</span>(<span class="x-c">CJK_BREAK</span>, <span class="hljs-string">&#x27;$1&#x27;</span>)}</span>&lt;/<span class="hljs-subst">${tag}</span>&gt;`</span>)</span>
<span class="line">    <span class="hljs-comment">// 二级到四级标题加上 id，目录和锚点链接靠它</span></span>
<span class="line">    .<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;h([234])&gt;([\s\S]*?)&lt;\/h\1&gt;/g</span>, <span class="hljs-function">(<span class="hljs-params">m, level, inner</span>) =&gt;</span> <span class="hljs-string">`&lt;h<span class="hljs-subst">${level}</span> id=&quot;<span class="hljs-subst">${<span class="x-f">slugify</span>(inner)}</span>&quot;&gt;<span class="hljs-subst">${inner}</span>&lt;/h<span class="hljs-subst">${level}</span>&gt;`</span>)</span>
<span class="line">    .<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;a href=&quot;(https?:\/\/[^&quot;]+)&quot;/g</span>, <span class="hljs-string">&#x27;&lt;a href=&quot;$1&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&#x27;</span>)</span>
<span class="line">    .<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;blockquote&gt;\s*&lt;p&gt;\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]([\s\S]*?)&lt;\/blockquote&gt;/g</span>, <span class="hljs-function">(<span class="hljs-params">m, type, rest</span>) =&gt;</span> {</span>
<span class="line">      <span class="hljs-keyword">const</span> [label, icon] = <span class="hljs-variable constant_">CALLOUTS</span>[type];</span>
<span class="line">      <span class="hljs-keyword">return</span> <span class="hljs-string">`&lt;aside class=&quot;callout is-<span class="hljs-subst">${type.<span class="x-f">toLowerCase</span>()}</span>&quot; role=&quot;note&quot;&gt;…&lt;/aside&gt;`</span>;</span>
<span class="line">    });</span>
<span class="line">}</span></code></pre></div>
<aside class="callout is-tip" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.6.5 1 1.2 1 2V16h5.2v-.2c0-.8.4-1.5 1-2A6 6 0 0 0 12 3z"/></svg>提示</p><p>第一条规则是最近才加的。中文写作时习惯在一句话中间换行，Markdown 会把这个换行当成空格，于是页面上莫名其妙多出一个空隙。英文需要这个空格，中文不需要，所以只在两个汉字之间去掉。</p>
</aside>
<h3 id="去掉文章里能运行的代码">去掉文章里能运行的代码</h3>
<p>文章只有我自己写，但后台支持导入别人的 Markdown 文件，里面可能夹带 <code>&lt;script&gt;</code> 或者 <code>onerror=</code>。构建时会把这些去掉，并在构建日志里提醒：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:124"><div class="code-head"><span class="code-title">scripts/build.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">function</span> <span class="hljs-title function_">scrub</span>(<span class="hljs-params">html</span>) {</span>
<span class="line">  <span class="hljs-keyword">let</span> out = html, before;</span>
<span class="line">  <span class="hljs-keyword">do</span> {</span>
<span class="line">    before = out;</span>
<span class="line">    out = out</span>
<span class="line">      .<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;(script|style|template|object|applet|frameset|noscript)\b[^&gt;]*&gt;[\s\S]*?&lt;\/\1\s*&gt;/gi</span>, <span class="hljs-string">&#x27;&#x27;</span>)</span>
<span class="line">      .<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;\/?(script|style|template|object|applet|frameset|frame|embed|base|meta|link|form|noscript)\b[^&gt;]*&gt;/gi</span>, <span class="hljs-string">&#x27;&#x27;</span>);</span>
<span class="line">  } <span class="hljs-keyword">while</span> (out !== before); <span class="hljs-comment">// 反复做到没有变化为止，防止 &lt;scr&lt;script&gt;ipt&gt; 这种拼接</span></span>
<span class="line">  <span class="hljs-keyword">return</span> out.<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/&lt;[a-z][^&gt;]*&gt;/gi</span>, scrubTag); <span class="hljs-comment">// 再逐个标签去掉 on* 属性和 javascript: 链接</span></span>
<span class="line">}</span></code></pre></div>
<p>这只是第二道防线。第一道是每个页面都带的内容安全策略（CSP）：只允许网站自己的脚本运行，就算有漏网的 <code>&lt;script&gt;</code>，浏览器也不会执行。</p>
<h2 id="代码高亮-仿-xcode-的配色">代码高亮：仿 Xcode 的配色</h2>
<p>代码高亮在构建时完成，访客不用下载高亮库。highlight.js 负责识别关键字、字符串、注释，但它分不出“类型名”“方法调用”“属性”，而 Xcode 会把这些分别上色。所以在它的结果上再走一遍：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:19;--cols:104"><div class="code-head"><span class="code-title">scripts/highlight.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-comment">/* identifiers in plain code text: Type, CONSTANT, call(, .property */</span></span>
<span class="line"><span class="hljs-keyword">function</span> <span class="hljs-title function_">semantic</span>(<span class="hljs-params">text</span>) {</span>
<span class="line">  <span class="hljs-keyword">let</span> out = <span class="hljs-string">&#x27;&#x27;</span>;</span>
<span class="line">  <span class="hljs-keyword">const</span> re = <span class="hljs-regexp">/(&amp;[a-z#0-9]+;)|([A-Za-z_$][\w$]*)|([\s\S])/g</span>;</span>
<span class="line">  <span class="hljs-keyword">let</span> m;</span>
<span class="line">  <span class="hljs-keyword">while</span> ((m = re.<span class="hljs-title function_ invoke__">exec</span>(text))) {</span>
<span class="line">    <span class="hljs-keyword">if</span> (!m[<span class="hljs-number">2</span>]) { out += m[<span class="hljs-number">0</span>]; <span class="hljs-keyword">continue</span>; }</span>
<span class="line">    <span class="hljs-keyword">const</span> id = m[<span class="hljs-number">2</span>];</span>
<span class="line is-hl">    <span class="hljs-keyword">const</span> before = text.<span class="hljs-title function_ invoke__">slice</span>(<span class="hljs-number">0</span>, m.<span class="hljs-property">index</span>).<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">&#x27;&#x27;</span>);</span>
<span class="line is-hl">    <span class="hljs-keyword">const</span> after = text.<span class="hljs-title function_ invoke__">slice</span>(re.<span class="hljs-property">lastIndex</span>).<span class="hljs-title function_ invoke__">replace</span>(<span class="hljs-regexp">/^\s+/</span>, <span class="hljs-string">&#x27;&#x27;</span>);</span>
<span class="line is-hl">    <span class="hljs-keyword">let</span> cls = <span class="hljs-string">&#x27;&#x27;</span>;</span>
<span class="line is-hl">    <span class="hljs-keyword">if</span> (after.<span class="hljs-title function_ invoke__">startsWith</span>(<span class="hljs-string">&#x27;(&#x27;</span>) || before.<span class="hljs-title function_ invoke__">endsWith</span>(<span class="hljs-string">&#x27;::&#x27;</span>)) cls = <span class="hljs-string">&#x27;x-f&#x27;</span>; <span class="hljs-comment">// 调用，或者方法引用 User::getName</span></span>
<span class="line">    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regexp">/^[A-Z][A-Z0-9_]+$/</span>.<span class="hljs-title function_ invoke__">test</span>(id)) cls = <span class="hljs-string">&#x27;x-c&#x27;</span>;             <span class="hljs-comment">// 常量</span></span>
<span class="line">    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regexp">/^[A-Z]/</span>.<span class="hljs-title function_ invoke__">test</span>(id)) cls = <span class="hljs-string">&#x27;x-t&#x27;</span>;                          <span class="hljs-comment">// 类型</span></span>
<span class="line">    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (before.<span class="hljs-title function_ invoke__">endsWith</span>(<span class="hljs-string">&#x27;.&#x27;</span>) &amp;&amp; !before.<span class="hljs-title function_ invoke__">endsWith</span>(<span class="hljs-string">&#x27;..&#x27;</span>)) cls = <span class="hljs-string">&#x27;x-p&#x27;</span>; <span class="hljs-comment">// 属性</span></span>
<span class="line">    out += cls ? <span class="hljs-string">`&lt;span class=&quot;<span class="hljs-subst">${cls}</span>&quot;&gt;<span class="hljs-subst">${id}</span>&lt;/span&gt;`</span> : id;</span>
<span class="line">  }</span>
<span class="line">  <span class="hljs-keyword">return</span> out;</span>
<span class="line">}</span></code></pre></div>
<p>规则很朴素：后面跟着括号的是调用，全大写的是常量，大写开头的是类型，前面有个点的是属性。这个判断只在字符串和注释<strong>之外</strong>的文本上做，靠的是一个记录当前处在哪种 <code>&lt;span&gt;</code> 里的小栈。</p>
<p>另一个细节是行号。highlight.js 输出的 HTML 里，一个多行注释的 <code>&lt;span&gt;</code> 会跨越好几行；想给每行单独包一层，就得在换行处把还没闭合的标签先关上、下一行再重新打开：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:17;--cols:89"><div class="code-head"><span class="code-title">scripts/highlight.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">function</span> <span class="hljs-title function_">lines</span>(<span class="hljs-params">html</span>) {</span>
<span class="line">  <span class="hljs-keyword">const</span> out = [];</span>
<span class="line">  <span class="hljs-keyword">const</span> stack = [];</span>
<span class="line">  <span class="hljs-keyword">let</span> cur = <span class="hljs-string">&#x27;&#x27;</span>;</span>
<span class="line">  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> tok <span class="hljs-keyword">of</span> html.<span class="hljs-title function_ invoke__">match</span>(<span class="hljs-regexp">/&lt;span class=&quot;[^&quot;]*&quot;&gt;|&lt;\/span&gt;|\n|[^&lt;\n]+/g</span>) || []) {</span>
<span class="line">    <span class="hljs-keyword">if</span> (tok === <span class="hljs-string">&#x27;\n&#x27;</span>) {</span>
<span class="line">      out.<span class="hljs-title function_ invoke__">push</span>(cur + <span class="hljs-string">&#x27;&lt;/span&gt;&#x27;</span>.<span class="hljs-title function_ invoke__">repeat</span>(stack.<span class="hljs-property">length</span>)); <span class="hljs-comment">// 这一行结束：关上所有还开着的标签</span></span>
<span class="line">      cur = stack.<span class="hljs-title function_ invoke__">join</span>(<span class="hljs-string">&#x27;&#x27;</span>);                           <span class="hljs-comment">// 下一行开头：原样再打开</span></span>
<span class="line">    } <span class="hljs-keyword">else</span> {</span>
<span class="line">      <span class="hljs-keyword">if</span> (tok.<span class="hljs-title function_ invoke__">startsWith</span>(<span class="hljs-string">&#x27;&lt;span&#x27;</span>)) stack.<span class="hljs-title function_ invoke__">push</span>(tok);</span>
<span class="line">      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (tok === <span class="hljs-string">&#x27;&lt;/span&gt;&#x27;</span>) stack.<span class="hljs-title function_ invoke__">pop</span>();</span>
<span class="line">      cur += tok;</span>
<span class="line">    }</span>
<span class="line">  }</span>
<span class="line">  out.<span class="hljs-title function_ invoke__">push</span>(cur);</span>
<span class="line">  <span class="hljs-keyword">return</span> out;</span>
<span class="line">}</span></code></pre></div>
<p>颜色本身写在 CSS 变量里，亮色和暗色各一套，跟着网站的主题切换：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:36"><div class="code-head"><span class="code-title">src/styles/base.css</span><span class="code-side"><span class="code-lang">CSS</span></span></div><pre><code class="hljs language-css"><span class="line"><span class="hljs-selector-tag">html</span><span class="hljs-selector-attr">[data-theme=<span class="hljs-string">&quot;light&quot;</span>]</span> {</span>
<span class="line">  <span class="hljs-attr">--x-kw</span>: <span class="hljs-number">#9b2393</span>;    <span class="hljs-comment">/* 关键字 */</span></span>
<span class="line">  <span class="hljs-attr">--x-str</span>: <span class="hljs-number">#c41a16</span>;   <span class="hljs-comment">/* 字符串 */</span></span>
<span class="line">  <span class="hljs-attr">--x-type</span>: <span class="hljs-number">#3900a0</span>;  <span class="hljs-comment">/* 类型 */</span></span>
<span class="line">  <span class="hljs-attr">--x-fn</span>: <span class="hljs-number">#6c36a9</span>;    <span class="hljs-comment">/* 方法调用 */</span></span>
<span class="line">  <span class="hljs-attr">--x-com</span>: <span class="hljs-number">#5d6c79</span>;   <span class="hljs-comment">/* 注释 */</span></span>
<span class="line">}</span></code></pre></div>
<h2 id="图片-每位访客只下载合适的那一张">图片：每位访客只下载合适的那一张</h2>
<p>文章里引用的每张图片，构建时都会经过 sharp 处理：</p>
<ul>
<li>缩成 480、800、1200、1600 四种宽度（比原图宽的跳过）；</li>
<li>每种宽度各出一份 AVIF 和 WebP；</li>
<li>截图（PNG）压得轻一些保住文字清晰，照片压得狠一些；</li>
<li><code>.rotate()</code> 按 EXIF 把手机竖拍的照片转正，同时去掉拍摄地点等信息。</li>
</ul>
<div class="codeblock has-lines" style="--ln:2;--rows:15;--cols:135"><div class="code-head"><span class="code-title">scripts/media.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">const</span> shot = <span class="hljs-regexp">/\.png$/i</span>.<span class="hljs-title function_ invoke__">test</span>(src);</span>
<span class="line"><span class="hljs-keyword">const</span> q = { <span class="hljs-attr">avif</span>: shot ? <span class="hljs-number">62</span> : <span class="hljs-number">50</span>, <span class="hljs-attr">webp</span>: shot ? <span class="hljs-number">88</span> : <span class="hljs-number">78</span> };</span>
<span class="line"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> w <span class="hljs-keyword">of</span> widths) {</span>
<span class="line">  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> fmt <span class="hljs-keyword">of</span> [<span class="hljs-string">&#x27;avif&#x27;</span>, <span class="hljs-string">&#x27;webp&#x27;</span>]) {</span>
<span class="line">    <span class="hljs-keyword">const</span> cached = path.<span class="hljs-title function_ invoke__">join</span>(cacheDir, <span class="hljs-string">`<span class="hljs-subst">${id}</span>-<span class="hljs-subst">${w}</span>-<span class="hljs-subst">${fmt}</span><span class="hljs-subst">${q[fmt]}</span>.<span class="hljs-subst">${fmt}</span>`</span>);</span>
<span class="line">    <span class="hljs-keyword">let</span> data;</span>
<span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-title function_ invoke__">existsSync</span>(cached)) data = <span class="hljs-keyword">await</span> <span class="hljs-title function_ invoke__">readFile</span>(cached);  <span class="hljs-comment">// 处理过的直接用缓存</span></span>
<span class="line">    <span class="hljs-keyword">else</span> {</span>
<span class="line">      <span class="hljs-keyword">const</span> pipe = <span class="hljs-title function_ invoke__">sharp</span>(buf).<span class="hljs-title function_ invoke__">rotate</span>().<span class="hljs-title function_ invoke__">resize</span>({ <span class="hljs-attr">width</span>: w, <span class="hljs-attr">withoutEnlargement</span>: <span class="hljs-literal">true</span> });</span>
<span class="line">      data = <span class="hljs-keyword">await</span> (fmt === <span class="hljs-string">&#x27;avif&#x27;</span> ? pipe.<span class="hljs-title function_ invoke__">avif</span>({ <span class="hljs-attr">quality</span>: q.<span class="hljs-property">avif</span>, <span class="hljs-attr">effort</span>: <span class="hljs-number">4</span> }) : pipe.<span class="hljs-title function_ invoke__">webp</span>({ <span class="hljs-attr">quality</span>: q.<span class="hljs-property">webp</span>, <span class="hljs-attr">effort</span>: <span class="hljs-number">5</span> })).<span class="hljs-title function_ invoke__">toBuffer</span>();</span>
<span class="line">      <span class="hljs-keyword">await</span> <span class="hljs-title function_ invoke__">writeFile</span>(cached, data);</span>
<span class="line">    }</span>
<span class="line">    <span class="hljs-comment">// …</span></span>
<span class="line">  }</span>
<span class="line">}</span></code></pre></div>
<p>输出的是一个 <code>&lt;picture&gt;</code>：浏览器先看自己支不支持 AVIF，再按屏幕宽度和像素密度从 <code>srcset</code> 里挑一张。图片写好了宽高，加载过程中页面不会跳动；离屏幕还远的图片不加载。比如下面这张分享卡片的原图是 JPG，你现在看到的多半是 AVIF：</p>
<figure class="media is-image" style="--ar:1.9048;--w:1200px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-480.avif 480w, https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-800.avif 800w, https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-1200.avif 1200w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-1200.webp" srcset="https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-480.webp 480w, https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-800.webp 800w, https://demo1.661366.xyz/assets/img/og-4dd8c14aa8-1200.webp 1200w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1200" height="630" alt="分享到微信或 X 时显示的预览图" loading="lazy" decoding="async"></picture><figcaption class="media-cap">这张图也是脚本画的：npm run og 按名字和职位生成</figcaption></figure>
<h2 id="中文字体-共用一份-每页再补一点">中文字体：共用一份，每页再补一点</h2>
<p>Windows 上的中文默认会落到微软雅黑，小字号时笔画发虚。想换成思源黑体，难处在于一套中文字体好几 MB。做法是在构建时统计每一页用到的汉字，只从字体里切出这些字，并且分成两份：</p>
<ul>
<li><strong>共用文件</strong>：出现在 30% 以上页面的字。下载一次之后就在缓存里，换页几乎不用再下；</li>
<li><strong>每页自己的文件</strong>：只有这一页（或很少几页）才用到的字。</li>
</ul>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:119"><div class="code-head"><span class="code-title">scripts/fonts.mjs</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">async</span> <span class="hljs-title function_ invoke__">plan</span>(<span class="hljs-params">pages</span>) {</span>
<span class="line">  <span class="hljs-keyword">const</span> count = { <span class="hljs-attr">all</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(), <span class="hljs-attr">heavy</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>() };</span>
<span class="line">  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> p <span class="hljs-keyword">of</span> pages) {</span>
<span class="line">    <span class="hljs-keyword">const</span> c = <span class="hljs-title function_ invoke__">charsOf</span>(p.<span class="hljs-property">html</span>, p.<span class="hljs-property">extra</span>);</span>
<span class="line">    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> kind <span class="hljs-keyword">of</span> [<span class="hljs-string">&#x27;all&#x27;</span>, <span class="hljs-string">&#x27;heavy&#x27;</span>]) <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> ch <span class="hljs-keyword">of</span> c[kind]) count[kind].<span class="hljs-title function_ invoke__">set</span>(ch, (count[kind].<span class="hljs-title function_ invoke__">get</span>(ch) || <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);</span>
<span class="line">  }</span>
<span class="line">  <span class="hljs-keyword">const</span> least = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">max</span>(<span class="hljs-number">2</span>, <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">ceil</span>(pages.<span class="hljs-property">length</span> * <span class="hljs-variable constant_">SHARE</span>));</span>
<span class="line">  <span class="hljs-keyword">const</span> <span class="hljs-title function_ invoke__">pick</span> = (<span class="hljs-params">m</span>) =&gt; <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([...m].<span class="hljs-title function_ invoke__">filter</span>(<span class="hljs-function">(<span class="hljs-params">[, n]</span>) =&gt;</span> n &gt;= least).<span class="hljs-title function_ invoke__">map</span>(<span class="hljs-function">(<span class="hljs-params">[ch]</span>) =&gt;</span> ch));</span>
<span class="line">  shared = { <span class="hljs-attr">all</span>: <span class="hljs-title function_ invoke__">pick</span>(count.<span class="hljs-property">all</span>), <span class="hljs-attr">heavy</span>: <span class="hljs-title function_ invoke__">pick</span>(count.<span class="hljs-property">heavy</span>) };</span>
<span class="line">}</span></code></pre></div>
<p>几个要点：</p>
<ul>
<li>源字体是思源黑体的<strong>可变字重</strong>版本，裁剪时用 <code>variationAxes: { wght: 400 }</code> 把字重固定下来，得到的静态字体比保留可变轴的小将近一半；</li>
<li>同一个字被两个 <code>@font-face</code> 覆盖时，浏览器用后声明的那个。所以共用文件声明整个汉字范围，每页自己的文件放在后面，只列出自己的字；</li>
<li>半粗字重只包含标题和加粗文字里的字，它的 <code>unicode-range</code> 精确列出每一个字。别的字如果被要求加粗，浏览器会回落到常规字重再自己加粗，不会退回微软雅黑；</li>
<li>苹果设备上 <code>html.apple</code> 会把这个字体从字体栈里拿掉，继续用苹方，一个字节都不下载。</li>
</ul>
<p>实测下来，第一次打开主页要下载约 75 KB，之后再打开别的页面，多数只要再下载几 KB 到 30 KB，特别长的文章会多一些。连续看 6 个页面一共约 200 KB；原来每页各裁一份时约 340 KB。</p>
<h2 id="首屏-一个着色器画出整个场景">首屏：一个着色器画出整个场景</h2>
<p>首页那颗星球、地平线的光、日月和星星，没有一张图片，全部是一个 WebGL 片元着色器逐像素算出来的。好处是任何屏幕上都清晰，而且页面不需要下载图片。</p>
<p>核心思路是<strong>有向距离</strong>：算出每个像素到星球边缘（一个巨大的圆）的距离 <code>d</code>，在星球里面是负数，外面是正数。地平线那条极细的亮边，就是 <code>d</code> 接近 0 的地方：</p>
<div class="codeblock" style="--ln:1;--rows:6;--cols:79"><div class="code-head"><span class="code-lang">GLSL</span><span class="code-side"></span></div><pre><code class="hljs language-glsl"><span class="line"><span class="hljs-keyword">vec2</span> rel = p - uPlanet.xy;</span>
<span class="line"><span class="hljs-keyword">float</span> dist = <span class="hljs-built_in">length</span>(rel);</span>
<span class="line"><span class="hljs-keyword">float</span> d = dist - uPlanet.z;                 <span class="hljs-comment">// 到地平线的有向距离，星球内部 &lt; 0</span></span>
<span class="line"><span class="hljs-keyword">float</span> body = <span class="hljs-number">1.</span> - <span class="hljs-built_in">smoothstep</span>(-aa, aa, d);   <span class="hljs-comment">// 星球本身，边缘抗锯齿</span></span>
<span class="line"><span class="hljs-keyword">float</span> line = <span class="hljs-built_in">exp</span>(-d*d / (<span class="hljs-number">.36</span> + aa*aa*<span class="hljs-number">.3</span>));  <span class="hljs-comment">// 那条极细的亮边</span></span>
<span class="line"><span class="hljs-keyword">float</span> near = <span class="hljs-built_in">exp</span>(-along / (uPlanet.z*<span class="hljs-number">.16</span>)); <span class="hljs-comment">// 离日月正下方越近越亮</span></span></code></pre></div>
<p>星星是“格子 + 哈希”：把画面切成小格，每格用哈希值决定有没有星星、亮度多少、偏暖还是偏冷。三种格子大小叠在一起，就有了大小不一、疏密自然的星空：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:102"><div class="code-head"><span class="code-lang">GLSL</span><span class="code-side"></span></div><pre><code class="hljs language-glsl"><span class="line"><span class="hljs-keyword">float</span> hash(<span class="hljs-keyword">vec2</span> p){p=<span class="hljs-built_in">fract</span>(p*<span class="hljs-keyword">vec2</span>(<span class="hljs-number">443.897</span>,<span class="hljs-number">441.423</span>));p+=<span class="hljs-built_in">dot</span>(p,p.yx+<span class="hljs-number">19.19</span>);<span class="hljs-keyword">return</span> <span class="hljs-built_in">fract</span>((p.x+p.y)*p.x);}</span>
<span class="line"><span class="hljs-keyword">vec3</span> stars(<span class="hljs-keyword">vec2</span> p,<span class="hljs-keyword">float</span> cs,<span class="hljs-keyword">float</span> prob,<span class="hljs-keyword">float</span> seed,<span class="hljs-keyword">float</span> r,<span class="hljs-keyword">float</span> b0,<span class="hljs-keyword">float</span> b1,<span class="hljs-keyword">float</span> halo){</span>
<span class="line">  <span class="hljs-keyword">vec2</span> cell=<span class="hljs-built_in">floor</span>(p/cs);</span>
<span class="line">  <span class="hljs-keyword">if</span>(hash(cell+seed)&gt;prob)<span class="hljs-keyword">return</span> <span class="hljs-keyword">vec3</span>(<span class="hljs-number">0.</span>);</span>
<span class="line">  <span class="hljs-keyword">vec2</span> sp=(cell+<span class="hljs-number">.15</span>+<span class="hljs-number">.7</span>*<span class="hljs-keyword">vec2</span>(hash(cell+seed+<span class="hljs-number">1.7</span>),hash(cell+seed+<span class="hljs-number">5.3</span>)))*cs;</span>
<span class="line">  <span class="hljs-keyword">vec2</span> v=p-sp;<span class="hljs-keyword">float</span> d2=<span class="hljs-built_in">dot</span>(v,v);</span>
<span class="line">  <span class="hljs-keyword">float</span> b=<span class="hljs-built_in">mix</span>(b0,b1,<span class="hljs-built_in">pow</span>(hash(cell+seed+<span class="hljs-number">9.1</span>),<span class="hljs-number">3.</span>));</span>
<span class="line">  <span class="hljs-keyword">float</span> t=hash(cell+seed+<span class="hljs-number">13.7</span>);</span>
<span class="line">  <span class="hljs-keyword">vec3</span> tint=t&lt;<span class="hljs-number">.22</span>?<span class="hljs-keyword">vec3</span>(<span class="hljs-number">1.</span>,<span class="hljs-number">.86</span>,<span class="hljs-number">.74</span>):(t&gt;<span class="hljs-number">.8</span>?<span class="hljs-keyword">vec3</span>(<span class="hljs-number">.78</span>,<span class="hljs-number">.86</span>,<span class="hljs-number">1.</span>):<span class="hljs-keyword">vec3</span>(<span class="hljs-number">.94</span>,<span class="hljs-number">.96</span>,<span class="hljs-number">1.</span>));</span>
<span class="line">  <span class="hljs-keyword">return</span> tint*b*(<span class="hljs-built_in">exp</span>(-d2/(r*r))+halo*<span class="hljs-built_in">exp</span>(-d2/(r*r*<span class="hljs-number">14.</span>)));</span>
<span class="line">}</span></code></pre></div>
<p>最后一行是个小技巧：在颜色上加一点点随机噪声（抖动），大面积的渐变就不会出现一圈圈的色带。</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:43"><div class="code-head"><span class="code-lang">GLSL</span><span class="code-side"></span></div><pre><code class="hljs language-glsl"><span class="line">col += (hash(<span class="hljs-built_in">gl_FragCoord</span>.xy) - <span class="hljs-number">.5</span>) / <span class="hljs-number">255.</span>;</span></code></pre></div>
<p>日月的高度由访客当地的时间决定：白天是太阳，晚上是月亮。每隔一阵还会有一颗流星划过，只在夜里、只在首屏停留时出现。不支持 WebGL 的浏览器，会用普通的 2D 画布画一个简化版。</p>
<h2 id="明暗切换-像素波纹">明暗切换：像素波纹</h2>
<p>右上角的按钮切换明暗时，新主题从按钮的位置像素块一样扩散开。实现用的是浏览器的 View Transitions：浏览器先给旧画面拍一张快照，换成新主题后再拍一张，然后由我决定新画面怎么“露出来”。</p>
<p>露出的形状是一个 <code>clip-path</code>：屏幕切成 20 像素的方块，每个方块有一个固定的随机阈值，波纹的半径超过“距离 + 阈值”时它就翻过来。因为阈值固定，每个方块只会翻一次，不会闪烁：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:21;--cols:108"><div class="code-head"><span class="code-title">src/scripts/theme.js</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">function</span> <span class="hljs-title function_">clipPath</span>(<span class="hljs-params">g, o, <span class="x-t">R</span>, <span class="x-t">W</span>, <span class="x-t">H</span>, full</span>) {</span>
<span class="line">  <span class="hljs-keyword">if</span> (full) <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;path(&quot;M0 0H&#x27;</span> + <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">ceil</span>(<span class="x-t">W</span>) + <span class="hljs-string">&#x27;V&#x27;</span> + <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">ceil</span>(<span class="x-t">H</span>) + <span class="hljs-string">&#x27;H0Z&quot;)&#x27;</span>;</span>
<span class="line">  <span class="hljs-keyword">var</span> out = <span class="hljs-string">&#x27;&#x27;</span>;</span>
<span class="line">  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> j = <span class="hljs-number">0</span>; j &lt; g.<span class="hljs-property">rows</span>; j++) {</span>
<span class="line">    <span class="hljs-keyword">var</span> cy = j * <span class="hljs-variable constant_">CS</span> + <span class="hljs-variable constant_">CS</span> / <span class="hljs-number">2</span>, run = -<span class="hljs-number">1</span>;</span>
<span class="line">    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt;= g.<span class="hljs-property">cols</span>; i++) {</span>
<span class="line">      <span class="hljs-keyword">var</span> on = <span class="hljs-literal">false</span>;</span>
<span class="line">      <span class="hljs-keyword">if</span> (i &lt; g.<span class="hljs-property">cols</span>) {</span>
<span class="line">        <span class="hljs-keyword">var</span> cx = i * <span class="hljs-variable constant_">CS</span> + <span class="hljs-variable constant_">CS</span> / <span class="hljs-number">2</span>, dx = cx - o.<span class="hljs-property">x</span>, dy = cy - o.<span class="hljs-property">y</span>;</span>
<span class="line">        on = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">sqrt</span>(dx * dx + dy * dy) &lt; <span class="x-t">R</span> - <span class="hljs-variable constant_">BAND</span> + g.<span class="hljs-property">noise</span>[j * g.<span class="hljs-property">cols</span> + i] * <span class="hljs-variable constant_">BAND</span>;</span>
<span class="line">      }</span>
<span class="line">      <span class="hljs-comment">// 同一行里连着翻过来的方块合并成一个长方形，路径短很多</span></span>
<span class="line">      <span class="hljs-keyword">if</span> (on &amp;&amp; run &lt; <span class="hljs-number">0</span>) run = i;</span>
<span class="line">      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!on &amp;&amp; run &gt;= <span class="hljs-number">0</span>) {</span>
<span class="line">        out += <span class="hljs-string">&#x27;M&#x27;</span> + run * <span class="hljs-variable constant_">CS</span> + <span class="hljs-string">&#x27; &#x27;</span> + j * <span class="hljs-variable constant_">CS</span> + <span class="hljs-string">&#x27;h&#x27;</span> + (i - run) * <span class="hljs-variable constant_">CS</span> + <span class="hljs-string">&#x27;v&#x27;</span> + <span class="hljs-variable constant_">CS</span> + <span class="hljs-string">&#x27;h&#x27;</span> + (run - i) * <span class="hljs-variable constant_">CS</span> + <span class="hljs-string">&#x27;z&#x27;</span>;</span>
<span class="line">        run = -<span class="hljs-number">1</span>;</span>
<span class="line">      }</span>
<span class="line">    }</span>
<span class="line">  }</span>
<span class="line">  <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;path(&quot;&#x27;</span> + (out || <span class="hljs-string">&#x27;M0 0z&#x27;</span>) + <span class="hljs-string">&#x27;&quot;)&#x27;</span>;</span>
<span class="line">}</span></code></pre></div>
<aside class="callout is-warning" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M10.3 4.2 2.8 17.5A2 2 0 0 0 4.5 20.5h15a2 2 0 0 0 1.7-3L13.7 4.2a2 2 0 0 0-3.4 0z"/><path d="M12 9.5v4"/><path d="M12 16.8v.2"/></svg>注意</p><p>这里踩过一个坑：两帧之间如果让浏览器自动插值，两个恰好都有 N 个长方形的路径会被“变形”过去，屏幕上出现奇怪的长条。解决办法是每一帧都用 <code>easing: &#39;step-end&#39;</code>，不插值，一帧一帧地换。</p>
</aside>
<p>主题默认跟着访客当地的时间走：白天浅色，晚上深色。手动切换后记住 6 小时，之后恢复自动。为了不在加载时闪一下错误的颜色，判断主题的那几行脚本直接写在 <code>&lt;head&gt;</code> 里，在页面画出来之前执行。</p>
<h2 id="液态玻璃-边缘的折射">液态玻璃：边缘的折射</h2>
<p>顶部的胶囊、首屏的信息条、视频控制条都是一层“液态玻璃”：背景模糊、提亮，再盖一层半透明的色调，左上和右下各一道边缘光。真正让它像玻璃的，是边缘那一圈的折射：靠近边缘的地方，背后的内容会被弯过来。</p>
<p>做法是按元素自己的尺寸和圆角画一张<strong>位移贴图</strong>，红色通道控制左右偏移，绿色通道控制上下偏移，128 表示不动。每个像素先算出到圆角矩形边缘的距离，只有边缘一圈才偏移，越靠边越多，中间保持平整，文字不受影响：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:102"><div class="code-head"><span class="code-title">src/scripts/glass.js</span><span class="code-side"><span class="code-lang">JavaScript</span></span></div><pre><code class="hljs language-javascript"><span class="line"><span class="hljs-keyword">var</span> qx = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">abs</span>(px) - (hw - r), qy = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">abs</span>(py) - (hh - r);</span>
<span class="line"><span class="hljs-keyword">var</span> ox = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">max</span>(qx, <span class="hljs-number">0</span>), oy = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">max</span>(qy, <span class="hljs-number">0</span>);</span>
<span class="line"><span class="hljs-keyword">var</span> dist = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">min</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">max</span>(qx, qy), <span class="hljs-number">0</span>) + <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">sqrt</span>(ox * ox + oy * oy) - r; <span class="hljs-comment">// 到边缘的距离，内部为负</span></span>
<span class="line"><span class="hljs-keyword">var</span> depth = -dist, m = depth &gt;= <span class="hljs-number">0</span> &amp;&amp; depth &lt; band ? <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">pow</span>(<span class="hljs-number">1</span> - depth / band, <span class="hljs-number">2</span>) : <span class="hljs-number">0</span>;</span>
<span class="line"><span class="hljs-comment">// … 算出最近那条边的法线 nx、ny</span></span>
<span class="line">d[i] = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">round</span>(<span class="hljs-number">128</span> - nx * m * <span class="hljs-number">127</span>);     <span class="hljs-comment">// 红：左右</span></span>
<span class="line">d[i + <span class="hljs-number">1</span>] = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_ invoke__">round</span>(<span class="hljs-number">128</span> - ny * m * <span class="hljs-number">127</span>); <span class="hljs-comment">// 绿：上下</span></span></code></pre></div>
<p>这张图交给 SVG 的 <code>feDisplacementMap</code>，再用 <code>backdrop-filter: url(#…)</code> 作用到元素背后的内容上。</p>
<aside class="callout is-note" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 11v5"/><path d="M12 7.6v.4"/></svg>说明</p><p>目前只有 Chrome、Edge 这类 Chromium 浏览器支持把 SVG 滤镜用在元素背后。Safari 和 Firefox 保持普通的磨砂玻璃，系统开了“减少透明度”时，玻璃直接变成实色。</p>
</aside>
<p>玻璃只在有颜色的背景上才好看，放在白纸上几乎看不出来。所以网站有两种材质：浮在画面上的用玻璃，放在页面底色上的内容卡片用另一套：顶部一道内发光、1 像素描边、由近到远的多重投影。</p>
<h2 id="很长的文章-看不见的部分先不排版">很长的文章：看不见的部分先不排版</h2>
<p>这篇文章有二十多个代码块。一屏之外的代码块和图片用 <code>content-visibility: auto</code> 跳过排版和绘制，滚到附近才处理。</p>
<p>跳过的前提是知道它有多高，否则滚动条会乱跳，目录也会跳错位置。代码块的行数在构建时就知道，高度可以直接算出来；图片的高度同样由比例算出：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:68"><div class="code-head"><span class="code-title">src/styles/blog.css</span><span class="code-side"><span class="code-lang">CSS</span></span></div><pre><code class="hljs language-css"><span class="line"><span class="hljs-selector-class">.prose</span> <span class="hljs-selector-class">.codeblock</span> {</span>
<span class="line">  <span class="hljs-attribute">content-visibility</span>: auto;</span>
<span class="line">  <span class="hljs-attribute">contain-intrinsic-size</span>: auto <span class="hljs-built_in">calc</span>(<span class="hljs-number">80px</span> + <span class="hljs-built_in">var</span>(--rows, <span class="hljs-number">8</span>) * <span class="hljs-number">22.4px</span>);</span>
<span class="line">}</span></code></pre></div>
<p>把页面里的代码块复制到 312 个（约 1.9 万个节点）测了一下，排版和绘制从 150 ms 左右降到 5 ms 左右。</p>
<p>图片加载时先显示一张 16 像素宽的小图，模糊后铺满整个框，每张只占一百多字节，直接写在页面里；原图下载好后再淡入。文章开头的第一张图不等滚动，优先下载。</p>
<p>代码块的标题栏在滚动时固定在顶部。遇到特别长的行，标题栏上会多一个“换行”按钮，选择会被记住；这个按钮只在最长的一行放不下时才出现，判断用的是构建时记下的列数，不需要先把代码块排版一遍。</p>
<h2 id="后端-零依赖的-java-服务">后端：零依赖的 Java 服务</h2>
<p>需要“动”的只有两件事：评论，和写文章的后台。它们是同一个 Java 程序的两种运行方式，用 JDK 自带的 <code>com.sun.net.httpserver.HttpServer</code>，不依赖任何第三方库。</p>
<p>两个服务都只肯监听本机地址，外面的请求一律经过 Nginx：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:112"><div class="code-head"><span class="code-title">server/src/blog/server/Main.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-keyword">if</span> (!<span class="x-t">InetAddress</span>.<span class="x-f">getByName</span>(host).<span class="x-f">isLoopbackAddress</span>()) {</span>
<span class="line">  <span class="x-t">System</span>.<span class="x-p">err</span>.<span class="x-f">println</span>(<span class="hljs-string">&quot;listen.host 是 &quot;</span> + host + <span class="hljs-string">&quot;：服务只能监听本机地址（127.0.0.1），由 Nginx / Caddy 转发。&quot;</span>);</span>
<span class="line">  <span class="x-t">System</span>.<span class="x-f">exit</span>(<span class="hljs-number">1</span>);</span>
<span class="line">}</span>
<span class="line"><span class="hljs-keyword">return</span> <span class="x-t">HttpServer</span>.<span class="x-f">create</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">InetSocketAddress</span>(host, port), <span class="hljs-number">128</span>);</span></code></pre></div>
<h3 id="自己实现的-postgresql-客户端">自己实现的 PostgreSQL 客户端</h3>
<p>不用 JDBC 驱动，意味着要自己和数据库“说话”。PostgreSQL 的通信协议其实很规整：每条消息是一个字节的类型、四个字节的长度，然后是内容。一次带参数的查询，就是连着发出 Parse、Bind、Describe、Execute、Sync 五条消息：</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:34;--cols:114;--fold:18" data-lines="34"><div class="code-head"><span class="code-title">server/src/blog/server/Pg.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-type">Rows</span> <span class="hljs-title function_">query</span><span class="hljs-params">(<span class="x-t">String</span> sql, <span class="x-t">Object</span>... params)</span> {</span>
<span class="line">  <span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">all</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();</span>
<span class="line">  <span class="hljs-comment">/* Parse：SQL 文本，参数用 $1、$2 占位，类型交给服务器推断 */</span></span>
<span class="line">  <span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">p</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();</span>
<span class="line">  <span class="hljs-type">DataOutputStream</span> <span class="hljs-variable">pd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DataOutputStream</span>(p);</span>
<span class="line">  <span class="x-f">cstr</span>(pd, <span class="hljs-string">&quot;&quot;</span>);</span>
<span class="line">  <span class="x-f">cstr</span>(pd, sql);</span>
<span class="line">  pd.<span class="x-f">writeShort</span>(<span class="hljs-number">0</span>);</span>
<span class="line">  <span class="x-f">frame</span>(all, <span class="hljs-string">&#x27;P&#x27;</span>, p.<span class="x-f">toByteArray</span>());</span>
<span class="line">  <span class="hljs-comment">/* Bind：参数的值单独发送，和 SQL 文本完全分开 */</span></span>
<span class="line">  <span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">bnd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();</span>
<span class="line">  <span class="hljs-type">DataOutputStream</span> <span class="hljs-variable">bd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DataOutputStream</span>(bnd);</span>
<span class="line">  <span class="x-f">cstr</span>(bd, <span class="hljs-string">&quot;&quot;</span>);</span>
<span class="line">  <span class="x-f">cstr</span>(bd, <span class="hljs-string">&quot;&quot;</span>);</span>
<span class="line">  bd.<span class="x-f">writeShort</span>(<span class="hljs-number">0</span>);</span>
<span class="line">  bd.<span class="x-f">writeShort</span>(params.<span class="x-p">length</span>);</span>
<span class="line">  <span class="hljs-keyword">for</span> (<span class="x-t">Object</span> v : params) {</span>
<span class="line">    <span class="hljs-keyword">if</span> (v == <span class="hljs-literal">null</span>) {</span>
<span class="line">      bd.<span class="x-f">writeInt</span>(-<span class="hljs-number">1</span>); <span class="hljs-comment">// SQL NULL</span></span>
<span class="line">    } <span class="hljs-keyword">else</span> {</span>
<span class="line">      <span class="hljs-keyword">byte</span>[] <span class="hljs-variable">bytes</span> <span class="hljs-operator">=</span> (v <span class="hljs-keyword">instanceof</span> <span class="x-t">Boolean</span> bo ? (bo ? <span class="hljs-string">&quot;t&quot;</span> : <span class="hljs-string">&quot;f&quot;</span>) : v.<span class="x-f">toString</span>()).<span class="x-f">getBytes</span>(<span class="x-t">StandardCharsets</span>.<span class="x-c">UTF_8</span>);</span>
<span class="line">      bd.<span class="x-f">writeInt</span>(bytes.<span class="x-p">length</span>);</span>
<span class="line">      bd.<span class="x-f">write</span>(bytes);</span>
<span class="line">    }</span>
<span class="line">  }</span>
<span class="line">  bd.<span class="x-f">writeShort</span>(<span class="hljs-number">0</span>);</span>
<span class="line">  <span class="x-f">frame</span>(all, <span class="hljs-string">&#x27;B&#x27;</span>, bnd.<span class="x-f">toByteArray</span>());</span>
<span class="line">  <span class="x-f">frame</span>(all, <span class="hljs-string">&#x27;D&#x27;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[] {<span class="hljs-string">&#x27;P&#x27;</span>, <span class="hljs-number">0</span>});</span>
<span class="line">  <span class="x-f">frame</span>(all, <span class="hljs-string">&#x27;E&#x27;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[] {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>});</span>
<span class="line">  <span class="x-f">frame</span>(all, <span class="hljs-string">&#x27;S&#x27;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[<span class="hljs-number">0</span>]);</span>
<span class="line">  out.<span class="x-f">write</span>(all.<span class="x-f">toByteArray</span>()); <span class="hljs-comment">// 五条消息一次发出去，只有一个来回</span></span>
<span class="line">  out.<span class="x-f">flush</span>();</span>
<span class="line">  <span class="hljs-comment">// … 读回 RowDescription、DataRow、CommandComplete，直到 ReadyForQuery</span></span>
<span class="line">}</span></code></pre></div>
<p>因为值和 SQL 文本是分开发送的，访客在评论框里输入什么都改变不了查询本身，SQL 注入从协议层面就不存在。</p>
<p>登录用的是 SCRAM-SHA-256：客户端和服务器互相证明自己知道密码，密码本身从不在网络上传输，最后还会验证服务器的签名，防止连到冒充的数据库。整个客户端（连接池、事务、SCRAM）四百多行。</p>
<h3 id="评论-先审核再显示">评论：先审核再显示</h3>
<p>评论接口只做两件事：读已经通过的评论、提交一条新评论。提交时有几道关卡：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:141"><div class="code-head"><span class="code-title">server/src/blog/server/Comments.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-comment">/* 机器人：填了隐藏的诱饵输入框，或者页面打开不到三秒就提交。照常回答“已提交”，但什么都不存 */</span></span>
<span class="line"><span class="hljs-keyword">if</span> (!<span class="x-f">text</span>(in, <span class="hljs-string">&quot;website2&quot;</span>).<span class="x-f">isEmpty</span>() || <span class="x-f">number</span>(in, <span class="hljs-string">&quot;t&quot;</span>) &lt; <span class="hljs-number">3000</span>) {</span>
<span class="line">  <span class="x-t">Log</span>.<span class="x-f">info</span>(<span class="hljs-string">&quot;dropped a bot submission&quot;</span>);</span>
<span class="line">  <span class="x-t">Http</span>.<span class="x-f">send</span>(x, <span class="hljs-number">202</span>, <span class="x-t">Map</span>.<span class="x-f">of</span>(<span class="hljs-string">&quot;status&quot;</span>, <span class="hljs-string">&quot;pending&quot;</span>), <span class="hljs-string">&quot;no-store&quot;</span>);</span>
<span class="line">  <span class="hljs-keyword">return</span>;</span>
<span class="line">}</span>
<span class="line"><span class="hljs-keyword">if</span> (!limiter.<span class="x-f">allow</span>(<span class="hljs-string">&quot;post10:&quot;</span> + ipHash, cfg.<span class="x-p">perIp10Min</span>, <span class="hljs-number">10</span> * <span class="x-c">MINUTE</span>) || !limiter.<span class="x-f">allow</span>(<span class="hljs-string">&quot;postday:&quot;</span> + ipHash, cfg.<span class="x-p">perIpDay</span>, <span class="hljs-number">24</span> * <span class="hljs-number">60</span> * <span class="x-c">MINUTE</span>)) {</span>
<span class="line">  <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Http</span>.<span class="x-f">Fail</span>(<span class="hljs-number">429</span>, <span class="hljs-string">&quot;rate&quot;</span>, <span class="hljs-string">&quot;评论太频繁了，请稍后再试&quot;</span>);</span>
<span class="line">}</span>
<span class="line"><span class="hljs-comment">// … 昵称、邮箱、网址、内容逐项检查；链接超过 3 个的不收；同样的内容一天内不能重复提交</span></span></code></pre></div>
<p>给机器人“假装成功”是故意的：如果直接报错，它就知道换个方式再试。</p>
<p>访客的 IP 从来不存原样，只存一个加了盐的摘要，够用来限流和识别刷屏就行：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:110"><div class="code-head"><span class="code-title">server/src/blog/server/Guard.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-title function_">ipHash</span><span class="hljs-params">(<span class="x-t">String</span> salt, <span class="x-t">String</span> ip)</span> {</span>
<span class="line">  <span class="hljs-keyword">byte</span>[] <span class="hljs-variable">d</span> <span class="hljs-operator">=</span> <span class="x-f">hmac</span>(salt.<span class="x-f">getBytes</span>(<span class="x-t">StandardCharsets</span>.<span class="x-c">UTF_8</span>), <span class="x-t">String</span>.<span class="x-f">valueOf</span>(ip).<span class="x-f">getBytes</span>(<span class="x-t">StandardCharsets</span>.<span class="x-c">UTF_8</span>));</span>
<span class="line">  <span class="hljs-keyword">return</span> <span class="x-t">HexFormat</span>.<span class="x-f">of</span>().<span class="x-f">formatHex</span>(d, <span class="hljs-number">0</span>, <span class="hljs-number">12</span>);</span>
<span class="line">}</span></code></pre></div>
<h3 id="数据库权限-按角色拆开">数据库权限：按角色拆开</h3>
<p>两个服务用两个不同的数据库账号，各自只拿到需要的那几列权限。评论服务是所有访客都能访问的那个，所以它的权限最小：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:133"><div class="code-head"><span class="code-title">server/schema.sql</span><span class="code-side"><span class="code-lang">SQL</span></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-comment">-- 评论服务：读评论（不含邮箱和浏览器信息）、加新评论、记假后台的访问。别的都不行</span></span>
<span class="line"><span class="hljs-keyword">GRANT</span> <span class="hljs-keyword">SELECT</span> (id, post, parent_id, author_name, website, content, status, is_owner, ip_hash, created_at) <span class="hljs-keyword">ON</span> blog.comment <span class="hljs-keyword">TO</span> blog_web;</span>
<span class="line"><span class="hljs-keyword">GRANT</span> <span class="hljs-keyword">INSERT</span> (post, parent_id, author_name, email, website, content, ip_hash, user_agent) <span class="hljs-keyword">ON</span> blog.comment <span class="hljs-keyword">TO</span> blog_web;</span>
<span class="line"><span class="hljs-keyword">GRANT</span> <span class="hljs-keyword">INSERT</span> (ip, kind, <span class="hljs-keyword">method</span>, path, user_agent, username) <span class="hljs-keyword">ON</span> blog.trap_hit <span class="hljs-keyword">TO</span> blog_web;</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">-- 每条语句最多 5 秒，事务不许挂着不提交：服务出了问题也拖不垮数据库</span></span>
<span class="line"><span class="hljs-keyword">ALTER</span> ROLE blog_web <span class="hljs-keyword">SET</span> statement_timeout <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;5s&#x27;</span>;</span>
<span class="line"><span class="hljs-keyword">ALTER</span> ROLE blog_web <span class="hljs-keyword">SET</span> idle_in_transaction_session_timeout <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;10s&#x27;</span>;</span></code></pre></div>
<p>就算评论服务被攻破，攻击者也读不到文章草稿和访客邮箱，删不了、改不了任何一条评论。</p>
<h2 id="后台的登录与安全">后台的登录与安全</h2>
<p>后台在一串随机生成的秘密地址下面，登录要两样东西：管理密码，和手机验证器上的 6 位数。</p>
<p>密码在配置文件里只存摘要，用 PBKDF2-HMAC-SHA256 迭代 31 万次。比较的时候用常量时间，不会因为“前几位对了所以慢一点”泄露信息：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:81"><div class="code-head"><span class="code-title">server/src/blog/server/Guard.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title function_">verifyPassword</span><span class="hljs-params">(<span class="x-t">String</span> stored, <span class="x-t">String</span> password)</span> {</span>
<span class="line">  <span class="hljs-type">String</span>[] <span class="hljs-variable">p</span> <span class="hljs-operator">=</span> stored.<span class="x-f">split</span>(<span class="hljs-string">&quot;\\$&quot;</span>);                 <span class="hljs-comment">// pbkdf2-sha256$轮数$盐$摘要</span></span>
<span class="line">  <span class="hljs-keyword">if</span> (p.<span class="x-p">length</span> != <span class="hljs-number">4</span> || !p[<span class="hljs-number">0</span>].<span class="x-f">equals</span>(<span class="hljs-string">&quot;pbkdf2-sha256&quot;</span>)) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;</span>
<span class="line">  <span class="hljs-keyword">int</span> <span class="hljs-variable">rounds</span> <span class="hljs-operator">=</span> <span class="x-t">Integer</span>.<span class="x-f">parseInt</span>(p[<span class="hljs-number">1</span>]);</span>
<span class="line">  <span class="hljs-keyword">byte</span>[] <span class="hljs-variable">salt</span> <span class="hljs-operator">=</span> <span class="x-t">Base64</span>.<span class="x-f">getDecoder</span>().<span class="x-f">decode</span>(p[<span class="hljs-number">2</span>]);</span>
<span class="line">  <span class="hljs-keyword">byte</span>[] <span class="hljs-variable">want</span> <span class="hljs-operator">=</span> <span class="x-t">Base64</span>.<span class="x-f">getDecoder</span>().<span class="x-f">decode</span>(p[<span class="hljs-number">3</span>]);</span>
<span class="line">  <span class="hljs-keyword">byte</span>[] <span class="hljs-variable">got</span> <span class="hljs-operator">=</span> <span class="x-f">pbkdf2</span>(password.<span class="x-f">getBytes</span>(<span class="x-t">StandardCharsets</span>.<span class="x-c">UTF_8</span>), salt, rounds);</span>
<span class="line">  <span class="hljs-keyword">return</span> <span class="x-t">MessageDigest</span>.<span class="x-f">isEqual</span>(want, got);          <span class="hljs-comment">// 常量时间比较</span></span>
<span class="line">}</span></code></pre></div>
<p>手机验证码是标准的 TOTP（RFC 6238），算法短得出乎意料：用共享密钥对“当前是第几个 30 秒”做 HMAC，再从结果里截出 6 位数字：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:107"><div class="code-head"><span class="code-title">server/src/blog/server/Totp.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-keyword">static</span> <span class="hljs-type">String</span> <span class="hljs-title function_">code</span><span class="hljs-params">(<span class="hljs-keyword">byte</span>[] key, <span class="hljs-keyword">long</span> step)</span> {</span>
<span class="line">  <span class="hljs-type">Mac</span> <span class="hljs-variable">mac</span> <span class="hljs-operator">=</span> <span class="x-t">Mac</span>.<span class="x-f">getInstance</span>(<span class="hljs-string">&quot;HmacSHA1&quot;</span>);</span>
<span class="line">  mac.<span class="x-f">init</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SecretKeySpec</span>(key, <span class="hljs-string">&quot;HmacSHA1&quot;</span>));</span>
<span class="line">  <span class="hljs-keyword">byte</span>[] <span class="hljs-variable">h</span> <span class="hljs-operator">=</span> mac.<span class="x-f">doFinal</span>(<span class="x-t">ByteBuffer</span>.<span class="x-f">allocate</span>(<span class="hljs-number">8</span>).<span class="x-f">putLong</span>(step).<span class="x-f">array</span>());</span>
<span class="line">  <span class="hljs-keyword">int</span> <span class="hljs-variable">o</span> <span class="hljs-operator">=</span> h[h.<span class="x-p">length</span> - <span class="hljs-number">1</span>] &amp; <span class="hljs-number">0xf</span>;   <span class="hljs-comment">// 最后一个字节的低 4 位决定从哪里截</span></span>
<span class="line">  <span class="hljs-keyword">int</span> <span class="hljs-variable">n</span> <span class="hljs-operator">=</span> ((h[o] &amp; <span class="hljs-number">0x7f</span>) &lt;&lt; <span class="hljs-number">24</span>) | ((h[o + <span class="hljs-number">1</span>] &amp; <span class="hljs-number">0xff</span>) &lt;&lt; <span class="hljs-number">16</span>) | ((h[o + <span class="hljs-number">2</span>] &amp; <span class="hljs-number">0xff</span>) &lt;&lt; <span class="hljs-number">8</span>) | (h[o + <span class="hljs-number">3</span>] &amp; <span class="hljs-number">0xff</span>);</span>
<span class="line">  <span class="hljs-keyword">return</span> <span class="x-t">String</span>.<span class="x-f">format</span>(<span class="hljs-string">&quot;%06d&quot;</span>, n % <span class="hljs-number">1_000_000</span>);</span>
<span class="line">}</span></code></pre></div>
<p>校验时前后各放宽一个周期，容忍手机时间差 30 秒；每个验证码只能用一次。</p>
<h3 id="假后台">假后台</h3>
<p><code>/admin/</code>、<code>/wp-login.php</code> 这类地址每天都有扫描器来试。它们会看到一个和网站风格一致的登录页，填什么都会在一两秒后提示“系统错误，请稍后再试”，谁也进不去。来访会被记下来（时间、地址、填的用户名），
<strong>密码从来不读</strong>。停顿的那一两秒是故意的，看起来像真的系统在处理：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:12;--cols:82"><div class="code-head"><span class="code-title">server/src/blog/server/Trap.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-comment">/* the password field is never read */</span></span>
<span class="line"><span class="x-f">record</span>(ip, <span class="hljs-string">&quot;login&quot;</span>, method, path, agent, user);</span>
<span class="line"><span class="x-t">Log</span>.<span class="x-f">warn</span>(<span class="hljs-string">&quot;trap login from %s path=%s&quot;</span>, ip, path.<span class="x-f">replaceAll</span>(<span class="hljs-string">&quot;\\s&quot;</span>, <span class="hljs-string">&quot;_&quot;</span>));</span>
<span class="line"><span class="hljs-keyword">if</span> (waiting.<span class="x-f">tryAcquire</span>()) {   <span class="hljs-comment">// 同时最多让 8 个请求等待，多了直接回答，拖不垮自己</span></span>
<span class="line">  <span class="hljs-keyword">try</span> {</span>
<span class="line">    <span class="x-t">Thread</span>.<span class="x-f">sleep</span>(<span class="hljs-number">1200</span> + <span class="x-t">ThreadLocalRandom</span>.<span class="x-f">current</span>().<span class="x-f">nextInt</span>(<span class="hljs-number">1600</span>));</span>
<span class="line">  } <span class="hljs-keyword">catch</span> (<span class="x-t">InterruptedException</span> e) {</span>
<span class="line">    <span class="x-t">Thread</span>.<span class="x-f">currentThread</span>().<span class="x-f">interrupt</span>();</span>
<span class="line">  } <span class="hljs-keyword">finally</span> {</span>
<span class="line">    waiting.<span class="x-f">release</span>();</span>
<span class="line">  }</span>
<span class="line">}</span></code></pre></div>
<h3 id="systemd-沙箱">systemd 沙箱</h3>
<p>两个服务都以专用的系统账号运行，再套一层 systemd 的沙箱。评论服务的配置是这样的：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:53"><div class="code-head"><span class="code-title">server/blog-comments.service</span><span class="code-side"><span class="code-lang">INI</span></span></div><pre><code class="hljs language-ini"><span class="line"><span class="hljs-attr">User</span>=blog-comments</span>
<span class="line"><span class="hljs-attr">MemoryMax</span>=<span class="hljs-number">300</span>M</span>
<span class="line"><span class="hljs-attr">NoNewPrivileges</span>=<span class="hljs-literal">yes</span></span>
<span class="line">CapabilityBoundingSet=</span>
<span class="line"><span class="hljs-comment"># 整个文件系统只读；看不到 /home，也就看不到 SSH 密钥</span></span>
<span class="line"><span class="hljs-attr">ProtectSystem</span>=strict</span>
<span class="line"><span class="hljs-attr">ProtectHome</span>=<span class="hljs-literal">yes</span></span>
<span class="line"><span class="hljs-attr">PrivateTmp</span>=<span class="hljs-literal">yes</span></span>
<span class="line"><span class="hljs-attr">PrivateDevices</span>=<span class="hljs-literal">yes</span></span>
<span class="line"><span class="hljs-attr">RestrictAddressFamilies</span>=AF_INET AF_INET6 AF_UNIX</span>
<span class="line"><span class="hljs-attr">SystemCallFilter</span>=@system-service</span></code></pre></div>
<h2 id="部署-git-push-就上线">部署：git push 就上线</h2>
<p>服务器上有一个 Git 裸仓库。推送代码时，<code>post-receive</code> 钩子依次做这些事，输出直接显示在本地的终端里：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:27;--cols:110"><div class="code-head"><span class="code-title">deploy/post-receive</span><span class="code-side"><span class="code-lang">Shell</span></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">say</span> <span class="hljs-string">&quot;检出 <span class="hljs-variable">${new:0:7}</span>&quot;</span></span>
<span class="line"><span class="x-cmd">git</span> --work-tree=<span class="hljs-string">&quot;<span class="hljs-variable">$SRC</span>&quot;</span> read-tree --reset -u <span class="hljs-string">&quot;<span class="hljs-variable">$new</span>&quot;</span></span>
<span class="line"><span class="x-cmd">git</span> --work-tree=<span class="hljs-string">&quot;<span class="hljs-variable">$SRC</span>&quot;</span> clean -fdq -e node_modules -e .cache -e .deploy-commit</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># package.json 变了才重新装依赖</span></span>
<span class="line">deps_hash=<span class="hljs-string">&quot;<span class="hljs-subst">$({ cat package.json; cat package-lock.json 2&gt;/dev/null || true; } | sha256sum | cut -c1-16)</span>&quot;</span></span>
<span class="line"><span class="hljs-keyword">if</span> [ ! -d node_modules ] || [ <span class="hljs-string">&quot;<span class="hljs-subst">$(cat node_modules/.deploy-hash 2&gt;/dev/null)</span>&quot;</span> != <span class="hljs-string">&quot;<span class="hljs-variable">$deps_hash</span>&quot;</span> ]; <span class="hljs-keyword">then</span></span>
<span class="line">  <span class="x-cmd">say</span> <span class="hljs-string">&quot;安装依赖&quot;</span></span>
<span class="line">  <span class="hljs-keyword">if</span> [ -f package-lock.json ]; <span class="hljs-keyword">then</span> npm ci --no-audit --no-fund; <span class="hljs-keyword">else</span> npm install --no-audit --no-fund; <span class="hljs-keyword">fi</span></span>
<span class="line">  <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$deps_hash</span>&quot;</span> &gt; node_modules/.deploy-hash</span>
<span class="line"><span class="hljs-keyword">fi</span></span>
<span class="line"></span>
<span class="line"><span class="x-cmd">say</span> <span class="hljs-string">&quot;构建&quot;</span></span>
<span class="line"><span class="hljs-keyword">if</span> ! bash <span class="hljs-string">&quot;<span class="hljs-variable">$SRC</span>/deploy/build-site.sh&quot;</span> <span class="hljs-string">&quot;代码 <span class="hljs-variable">${new:0:7}</span>：<span class="hljs-subst">$(git log -1 --format=%s <span class="hljs-string">&quot;<span class="hljs-variable">$new</span>&quot;</span>)</span>&quot;</span>; <span class="hljs-keyword">then</span></span>
<span class="line">  <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;修好后再推送一次就行。&quot;</span> &gt;&amp;2</span>
<span class="line">  <span class="hljs-built_in">exit</span> 1</span>
<span class="line"><span class="hljs-keyword">fi</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># server/ 变了才重新编译 Java 服务并重启</span></span>
<span class="line"><span class="hljs-keyword">if</span> [ <span class="hljs-string">&quot;<span class="hljs-variable">$old</span>&quot;</span> = <span class="hljs-string">&quot;0000000000000000000000000000000000000000&quot;</span> ] || ! git diff --quiet <span class="hljs-string">&quot;<span class="hljs-variable">$old</span>&quot;</span> <span class="hljs-string">&quot;<span class="hljs-variable">$new</span>&quot;</span> -- server; <span class="hljs-keyword">then</span></span>
<span class="line">  ./server/build.sh</span>
<span class="line">  <span class="x-cmd">install</span> -m 644 server/build/blog-server.jar <span class="hljs-string">&quot;<span class="hljs-variable">$JAR_DIR</span>/blog-server.jar.new&quot;</span></span>
<span class="line">  <span class="hljs-built_in">mv</span> -f <span class="hljs-string">&quot;<span class="hljs-variable">$JAR_DIR</span>/blog-server.jar.new&quot;</span> <span class="hljs-string">&quot;<span class="hljs-variable">$JAR_DIR</span>/blog-server.jar&quot;</span></span>
<span class="line">  <span class="hljs-keyword">for</span> svc <span class="hljs-keyword">in</span> blog-comments blog-admin; <span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">sudo</span> -n systemctl restart <span class="hljs-string">&quot;<span class="hljs-variable">$svc</span>&quot;</span></span>
<span class="line">  <span class="hljs-keyword">done</span></span>
<span class="line"><span class="hljs-keyword">fi</span></span></code></pre></div>
<p>真正上线的那一步在 <code>build-site.sh</code> 里：每次构建到一个带时间戳的新目录，成功了才用 <code>rename</code> 把 <code>live</code>
这个链接一步指过去。<code>rename</code> 是原子操作，访客不会碰到“换到一半”的网站；构建失败则什么都不变。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:105"><div class="code-head"><span class="code-title">deploy/build-site.sh</span><span class="code-side"><span class="code-lang">Shell</span></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">if</span> ! CONTENT_DIR=<span class="hljs-string">&quot;<span class="hljs-variable">$content</span>&quot;</span> MEDIA_DIR=<span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/media&quot;</span> OUT_DIR=<span class="hljs-string">&quot;<span class="hljs-variable">$BUILDS</span>/<span class="hljs-variable">$stamp</span>&quot;</span> node scripts/build.mjs; <span class="hljs-keyword">then</span></span>
<span class="line">  <span class="hljs-built_in">rm</span> -rf <span class="hljs-string">&quot;<span class="hljs-variable">${BUILDS:?}</span>/<span class="hljs-variable">$stamp</span>&quot;</span></span>
<span class="line">  <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;构建失败，网站还是之前的样子。&quot;</span> &gt;&amp;2</span>
<span class="line">  <span class="hljs-built_in">exit</span> 1</span>
<span class="line"><span class="hljs-keyword">fi</span></span>
<span class="line"><span class="hljs-built_in">ln</span> -sfn <span class="hljs-string">&quot;<span class="hljs-variable">$BUILDS</span>/<span class="hljs-variable">$stamp</span>&quot;</span> <span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/.live.new&quot;</span></span>
<span class="line"><span class="hljs-built_in">mv</span> -T <span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/.live.new&quot;</span> <span class="hljs-string">&quot;<span class="hljs-variable">$BASE</span>/live&quot;</span></span></code></pre></div>
<p>最近 5 个版本都留在服务器上，后台的「发布」页里点一下就能切回去。</p>
<h3 id="nginx">Nginx</h3>
<p>Nginx 负责 HTTPS、压缩和限流。构建时已经生成了压缩好的 <code>.gz</code>，Nginx 直接发送，不用每次请求都现压：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:82"><div class="code-head"><span class="code-title">deploy/nginx.conf</span><span class="code-side"><span class="code-lang">Nginx</span></span></div><pre><code class="hljs language-nginx"><span class="line"><span class="hljs-attribute">limit_req_zone</span>  <span class="hljs-variable">$binary_remote_addr</span> zone=comments:<span class="hljs-number">10m</span>    rate=60r/m;</span>
<span class="line"><span class="hljs-attribute">limit_req_zone</span>  <span class="hljs-variable">$binary_remote_addr</span> zone=admin_login:<span class="hljs-number">10m</span> rate=6r/m;</span>
<span class="line"></span>
<span class="line"><span class="hljs-attribute">gzip_static</span> <span class="hljs-literal">on</span>;</span>
<span class="line"><span class="hljs-attribute">add_header</span> Strict-Transport-Security <span class="hljs-string">&quot;max-age=31536000; includeSubDomains&quot;</span> always;</span>
<span class="line"><span class="hljs-attribute">add_header</span> X-Frame-Options DENY always;</span></code></pre></div>
<h2 id="性能">性能</h2>
<p>做完这一切，首页首次访问（空缓存）的情况：</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>内容</th>
<th>大小（压缩后）</th>
</tr>
</thead>
<tbody><tr>
<td>HTML</td>
<td>5 KB</td>
</tr>
<tr>
<td>CSS（全站一个文件）</td>
<td>13 KB</td>
</tr>
<tr>
<td>JavaScript（含着色器）</td>
<td>10 KB</td>
</tr>
<tr>
<td>时区表（估算访客所在城市）</td>
<td>5 KB</td>
</tr>
<tr>
<td>英文字体 Inter，三个字重（苹果设备不下载）</td>
<td>55 KB</td>
</tr>
<tr>
<td>中文字体，共用文件和这一页的文件（苹果设备不下载）</td>
<td>约 75 KB</td>
</tr>
</tbody></table></div>
<p>苹果设备上只有前四行，加起来约 33 KB。字体都不会挡住页面显示：文字先用系统字体画出来，字体到了再换上。</p>
<p><code>/assets/</code> 下的文件名带内容哈希，可以让浏览器缓存一年；HTML 不缓存，发了新版访客立刻能看到。</p>
<h2 id="写在最后">写在最后</h2>
<p>回头看，这个网站没有用到什么高深的技术，大部分是把基础的东西认真做一遍：
HTTP 缓存、图片格式、字体子集、数据库权限、原子替换。真正花时间的是细节，比如一个中文换行变成的空格、一帧动画里冒出来的长条、一个机器人应该看到什么样的回答。</p>
<p>这篇本身也是一次测试：长文章的目录、代码块的折叠和高亮、表格、提示框、图片，都在上面用到了。如果哪里显示得不对，欢迎在下面留言告诉我。</p>
]]></content:encoded><category>架构</category><category>前端</category><category>Java</category></item>
<item><title>这个博客的 Markdown 写法</title><link>https://demo1.661366.xyz/blog/markdown-guide/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/markdown-guide/</guid><pubDate>Thu, 29 Oct 2026 16:00:00 GMT</pubDate><description>这个博客支持的全部排版：文字、链接、列表、提示框、表格、图片、图集和视频，以及屏幕边上的文章目录。也是我自己写文章时查的备忘。</description><content:encoded><![CDATA[<p>这里的每篇文章都是一个 Markdown 文件，文件名就是文章的网址，比如这篇是 <code>/blog/markdown-guide/</code>。下面把支持的写法挨个过一遍，每一种都直接显示出效果。</p>
<h2 id="文件开头的信息">文件开头的信息</h2>
<p>文件最上面两条 <code>---</code> 之间是文章信息：</p>
<ul>
<li><code>title</code>：标题</li>
<li><code>date</code>：发布日期，格式 <code>2026-10-30</code></li>
<li><code>tags</code>：标签，写成 <code>[Java, 后端]</code></li>
<li><code>description</code>：摘要，会显示在列表和分享卡片里</li>
<li><code>draft: true</code>：草稿，本地预览能看到，正式构建时会被跳过</li>
<li><code>cover: /images/封面.jpg</code>：分享这篇文章时的预览图，不写就用文章里的第一张图片</li>
<li><code>updated: 2026-11-13</code>：大改过以后的更新日期，显示在发布日期旁边。一年前的文章会在开头提醒读者内容可能过时，填了更新日期就从更新那天算</li>
<li><code>series: 博客使用指南</code>：系列。写了同一个系列名的文章连成一组，开头列出整个系列，结尾有「下一篇」，比如这篇</li>
<li><code>pinned: true</code>：置顶，固定显示在博客首页最上面的大卡片里（不写就显示最新的一篇）</li>
</ul>
<h2 id="常用排版">常用排版</h2>
<h3 id="文字和链接">文字和链接</h3>
<p><strong>加粗</strong>、<em>斜体</em>、<del>删除线</del>、<code>行内代码</code>，还有键盘按键 <kbd>⌘</kbd> <kbd>K</kbd>。</p>
<p>站内链接像<a href="https://demo1.661366.xyz/blog/">全部文章</a>这样；指向别的网站的链接，比如 <a href="https://spring.io" target="_blank" rel="noopener">Spring 官网</a>，后面会自动带一个小箭头，并且在新标签页打开。</p>
<h3 id="文章之间的链接">文章之间的链接</h3>
<p>链接到另一篇文章，用思源和 Obsidian 的双方括号写法就行，不用记完整网址：</p>
<ul>
<li><code>[[how-this-blog-works]]</code>：链接文字自动用那篇的标题，<a class="wikilink" href="https://demo1.661366.xyz/blog/how-this-blog-works/" data-ref="how-this-blog-works">这个博客是怎么做的：技术栈与实现细节</a></li>
<li><code>[[code-blocks|代码块那篇]]</code>：竖线后面是自己写的链接文字，比如<a class="wikilink" href="https://demo1.661366.xyz/blog/code-blocks/" data-ref="code-blocks">代码块那篇</a></li>
<li><code>[[code-blocks#长代码会折叠]]</code>：直接跳到那篇的某一节，<a class="wikilink" href="https://demo1.661366.xyz/blog/code-blocks/#长代码会折叠" data-ref="code-blocks" data-part="长代码会折叠">代码块：仿 Xcode 的语法高亮 › 长代码会折叠</a></li>
<li><code>[[#文章目录]]</code>：跳到本文的某一节，<a class="wikilink" href="#文章目录">文章目录</a></li>
</ul>
<p>方括号里写网址（文件名）最稳妥，写文章标题也能找到。文章换过网址也不怕，旧网址照样能找到它。找不到的链接会显示成灰色虚线，构建时也会提醒。</p>
<p>鼠标停在这种链接上，会浮出那篇文章的摘要卡片。被链接的文章底部会多一块「提到这篇的文章」，列出谁链接了它，以及链接所在的那句话。普通的 Markdown 链接 <code>[文字](/blog/网址/)</code> 也算在里面。</p>
<h3 id="列表">列表</h3>
<p>无序列表用 <code>-</code> 开头，缩进两个空格就是下一级：</p>
<ul>
<li>后端：Spring Boot、MyBatis<ul>
<li>数据库：MySQL、Redis<ul>
<li>第三级用短横线</li>
</ul>
</li>
</ul>
</li>
<li>前端：Vue、TypeScript</li>
</ul>
<p>有序列表用数字开头：</p>
<ol>
<li>写好 Markdown</li>
<li>运行 <code>npm run dev</code> 预览</li>
<li>运行 <code>npm run build</code>，把 <code>dist/</code> 传到服务器</li>
</ol>
<p>待办清单用 <code>- [ ]</code> 和 <code>- [x]</code>：</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> 买域名</li>
<li><input checked="" disabled="" type="checkbox"> 写第一篇文章</li>
<li><input disabled="" type="checkbox"> 配好 HTTPS</li>
</ul>
<h3 id="引用和提示框">引用和提示框</h3>
<blockquote>
<p>引用的内容会显示成这样，适合放别人的观点或者一段需要强调的话。</p>
</blockquote>
<p>引用的第一行写上 <code>[!NOTE]</code>、<code>[!TIP]</code>、<code>[!IMPORTANT]</code>、<code>[!WARNING]</code> 或 <code>[!CAUTION]</code>，就变成带颜色的提示框：</p>
<aside class="callout is-note" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 11v5"/><path d="M12 7.6v.4"/></svg>说明</p><p>补充一些背景信息，比如这篇文章基于 Spring Boot 3。</p>
</aside>
<aside class="callout is-tip" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.6.5 1 1.2 1 2V16h5.2v-.2c0-.8.4-1.5 1-2A6 6 0 0 0 12 3z"/></svg>提示</p><p><code>npm run dev</code> 运行时，保存文件页面会自动刷新。</p>
</aside>
<aside class="callout is-warning" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M10.3 4.2 2.8 17.5A2 2 0 0 0 4.5 20.5h15a2 2 0 0 0 1.7-3L13.7 4.2a2 2 0 0 0-3.4 0z"/><path d="M12 9.5v4"/><path d="M12 16.8v.2"/></svg>注意</p><p>服务器密码和私钥不要写进文章，也不要提交到仓库。</p>
</aside>
<p><code>[!TIP]</code> 后面接着写字就是自己的标题；写成 <code>[!TIP]-</code> 会折叠起来，点标题才展开，<code>[!TIP]+</code> 是默认展开的折叠框。
Obsidian 里的 <code>[!info]</code>、<code>[!question]</code>、<code>[!danger]</code> 这些名字也认得，会归到最接近的一种颜色：</p>
<aside class="callout is-tip" role="note"><p class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.6.5 1 1.2 1 2V16h5.2v-.2c0-.8.4-1.5 1-2A6 6 0 0 0 12 3z"/></svg>本地预览的小技巧</p><p>草稿也能在 <code>npm run dev</code> 里看到，正式构建时才会被跳过。</p>
</aside>
<details class="callout is-note"><summary class="callout-title"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 11v5"/><path d="M12 7.6v.4"/></svg>点开看：为什么不用前端框架<svg class="callout-chev" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 6 6 6-6 6"/></svg></summary><div class="callout-body"><p>这个网站的页面都是构建时生成的静态 HTML，用不到框架的运行时。少一层依赖，也就少一份要跟着升级的东西。</p>
</div></details>
<p>不是提示框、只想把一段长内容收起来，用 HTML 的 <code>&lt;details&gt;</code>，里面照样能写 Markdown（上下各空一行）：</p>
<details>
<summary>完整的启动日志</summary><div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:42"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ npm run dev</span>
<span class="line"><span class="x-cmd">built</span> 14 pages, 6 posts <span class="hljs-keyword">in</span> 1680 ms → dist/</span>
<span class="line"><span class="x-cmd">dev</span> server → http://localhost:4321</span></code></pre></div>
</details><h3 id="脚注">脚注</h3>
<p>正文里写 <code>[^1]</code>，在文章任意地方（一般是最后）另起一行写 <code>[^1]: 脚注内容</code>，就成了脚注⁠<sup class="fn-ref" id="fnref-1"><a href="#fn-1" data-fn="1" aria-label="注释 1">1</a></sup>。编号按在正文里第一次出现的顺序自动排，方括号里写什么都行，比如 <code>[^spring]</code>⁠<sup class="fn-ref" id="fnref-2"><a href="#fn-2" data-fn="2" aria-label="注释 2">2</a></sup>。鼠标停在小数字上能直接看到内容，点它会跳到文章末尾的「注释」，再点 ↩ 回到原处。</p>
<h3 id="代码">代码</h3>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:72"><div class="code-head"><span class="code-lang">Java</span><span class="code-side"></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">&quot;/hello&quot;</span>)</span>
<span class="line"><span class="hljs-keyword">public</span> <span class="hljs-type">String</span> <span class="hljs-title function_">hello</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam</span>(defaultValue = <span class="hljs-string">&quot;world&quot;</span>) <span class="x-t">String</span> name)</span> {</span>
<span class="line">    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello, &quot;</span> + name;</span>
<span class="line">}</span></code></pre></div>
<p>代码块右上角有复制按钮，左上角显示语言（写在三个反引号后面，比如 java、bash、yaml）。</p>
<h3 id="数学公式">数学公式</h3>
<p>公式用 LaTeX 写，构建时就排好版，读者那边不用加载任何脚本。行内的公式写在一对 <code>$</code> 之间，比如 <code>$E = mc^2$</code> 显示成 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">E = mc^2</annotation></semantics></math></span></span>，
<code>$O(n \log n)$</code> 显示成 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>log</mi><mo>⁡</mo><mi>n</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(n \log n)</annotation></semantics></math></span></span>。单独成行的公式用 <code>$$</code> 包起来：</p>
<div class="math-display"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex">x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}</annotation></semantics></math></span></span></span></div>
<p>写成 <code>```math</code> 代码块也一样（GitHub 也认这种写法）：</p>
<div class="math-display"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><munderover><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mi>i</mi><mo>=</mo><mfrac><mrow><mi>n</mi><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow><mn>2</mn></mfrac></mrow><annotation encoding="application/x-tex">\sum_{i=1}^{n} i = \frac{n(n+1)}{2}</annotation></semantics></math></span></span></span></div>
<p>只是写钱数的 <code>$</code> 不会被当成公式：开头的 <code>$</code> 后面紧跟空格、结尾的 <code>$</code> 后面紧跟数字都不算，所以「$5 和 $10」照常显示。真要写一个单独的美元符号，写成 <code>\$</code>。公式写错了会显示成红色，构建时也会提醒是哪一条。</p>
<h3 id="表格">表格</h3>
<div class="table-wrap"><table>
<thead>
<tr>
<th>写法</th>
<th>效果</th>
</tr>
</thead>
<tbody><tr>
<td><code>## 标题</code></td>
<td>二级标题</td>
</tr>
<tr>
<td><code>&gt; 引用</code></td>
<td>引用块</td>
</tr>
<tr>
<td><code>- 列表</code></td>
<td>无序列表</td>
</tr>
<tr>
<td><code>![说明](路径)</code></td>
<td>图片或视频</td>
</tr>
</tbody></table></div>
<h3 id="图片">图片</h3>
<p>图片放在 <code>src/public/images/</code> 里，然后这样引用：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:48"><div class="code-head"><span class="code-lang">Markdown</span><span class="code-side"></span></div><pre><code class="hljs language-markdown"><span class="line">![<span class="hljs-string">说明文字</span>](<span class="hljs-link">/images/文件名.jpg &quot;图注，可以不写&quot;</span>)</span></code></pre></div>
<p>单独占一行的图片会居中显示，引号里的图注浮在图片左下角的毛玻璃上。点一下图片可以全屏查看：</p>
<figure class="media is-image" style="--ar:1.6000;--w:1600px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-480.avif 480w, https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-800.avif 800w, https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-1200.avif 1200w, https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-1200.webp" srcset="https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-480.webp 480w, https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-800.webp 800w, https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-1200.webp 1200w, https://demo1.661366.xyz/assets/img/demo-1-ce40f2dfc1-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="1000" alt="一张示例图片" loading="lazy" decoding="async"></picture><figcaption class="media-cap">图注写在图片路径后面的引号里</figcaption></figure>
<p>构建时会自动把图片缩成几种尺寸、转成 AVIF 和 WebP，手机只下载手机大小的那一张。</p>
<h3 id="流程图和示意图">流程图和示意图</h3>
<p>流程图、架构图用 <a href="https://app.diagrams.net" target="_blank" rel="noopener">draw.io</a>、<a href="https://excalidraw.com" target="_blank" rel="noopener">Excalidraw</a> 或 <a href="https://mermaid.live" target="_blank" rel="noopener">Mermaid Live</a> 画好，导出成 SVG，像图片一样引用。SVG 是矢量图，放多大都清楚，文件也小：</p>
<figure class="media is-raw is-svg has-dark" style="--w:640px"><img class="svg-light" src="https://demo1.661366.xyz/images/demo-flow.svg" alt="构建流程示意图" width="640" height="150" loading="lazy" decoding="async"><img class="svg-dark" src="https://demo1.661366.xyz/images/demo-flow.dark.svg" alt="构建流程示意图" width="640" height="150" loading="lazy" decoding="async"><figcaption class="media-cap">一张 SVG 示意图：在深色模式下会换成为深色画的那一版</figcaption></figure>
<ul>
<li>导出时背景选透明。深色模式下这张图会自动垫一块白底，深色的线条照样看得清。</li>
<li>想让深色模式显示专门的一版，再导出一份放在旁边，文件名加 <code>.dark</code>：<code>流程图.svg</code> 和 <code>流程图.dark.svg</code>。上面这张就是这样。</li>
<li>后台编辑器里也能直接上传 SVG。上传时会去掉里面能运行的代码和指向别的网站的链接，只留下图形和文字。</li>
</ul>
<h3 id="图集">图集</h3>
<p>几张图片挨着写（中间不夹文字），就会叠成一摞卡片：拖动或者左右滑动最上面那张翻页，也可以点右下角的箭头。形状和第一张不一样的图片，会放在它自己模糊后的颜色上：</p>
<div class="stack" style="--ar:1.6000" data-count="3">
<figure class="media is-image stack-card" data-depth="0"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-480.avif 480w, https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-800.avif 800w, https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-1200.avif 1200w, https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-1200.webp" srcset="https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-480.webp 480w, https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-800.webp 800w, https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-1200.webp 1200w, https://demo1.661366.xyz/assets/img/demo-2-3ca78d5adb-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="1000" alt="第二张示例图片" loading="lazy" decoding="async"></picture><figcaption class="media-cap">第一张：拖动或滑动卡片翻页</figcaption></figure>
<figure class="media is-image stack-card" data-depth="1"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-480.avif 480w, https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-800.avif 800w, https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-1200.avif 1200w, https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-1200.webp" srcset="https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-480.webp 480w, https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-800.webp 800w, https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-1200.webp 1200w, https://demo1.661366.xyz/assets/img/demo-3-c2f6aa4dd4-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="1000" alt="第三张示例图片" loading="lazy" decoding="async"></picture><figcaption class="media-cap">第二张</figcaption></figure>
<figure class="media is-image stack-card" data-depth="2"><img class="media-fill" src="https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-480.webp" alt="" aria-hidden="true" loading="lazy" decoding="async"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-480.avif 480w, https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-800.avif 800w, https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-1200.avif 1200w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-1200.webp" srcset="https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-480.webp 480w, https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-800.webp 800w, https://demo1.661366.xyz/assets/img/demo-4-3cd5697375-1200.webp 1200w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1200" height="1500" alt="第四张示例图片" loading="lazy" decoding="async"></picture><figcaption class="media-cap">第三张是竖图，衬着它自己模糊的颜色</figcaption></figure>
</div>
<h3 id="视频">视频</h3>
<p>先用 <code>npm run video -- 视频文件 名字</code> 把视频转成网页用的格式（需要装 ffmpeg），它会生成 <code>src/public/videos/名字.mp4</code>
和一张同名封面，然后像图片一样引用：</p>
<figure class="media is-video" style="--ar:1.7778;--w:1280px"><video controls playsinline preload="none" poster="https://demo1.661366.xyz/assets/img/demo-dd901b4a7b-1200.webp" width="1280" height="720" src="https://demo1.661366.xyz/videos/demo.mp4" aria-label="一段示例视频"></video><figcaption class="media-cap">播放按钮和控制条都是毛玻璃</figcaption></figure>
<p>有封面的视频，访客按播放之前一点都不下载。</p>
<h2 id="文章目录">文章目录</h2>
<p>文章里的二级标题（<code>##</code>）和三级标题（<code>###</code>）会自动出现在屏幕边上的刻度里，三角形跟着你读到的位置移动。鼠标移上去（手机上点一下）会展开目录，目录分三级：</p>
<ul>
<li>最上面是文章标题，点它回到开头</li>
<li>二级标题带编号 01、02……</li>
<li>三级标题缩进在所属的二级标题下面，字小一号</li>
</ul>
<p>四级及以下的标题不进目录，所以正文的标题从 <code>##</code> 开始写就好（<code>#</code> 是文章标题本身）。目录很长时可以在上面单独滚动，文章不会跟着动。一篇文章至少有两个这样的标题，才会显示目录。</p>
<section class="footnotes" aria-label="注释"><p class="fn-title">注释</p><ol><li id="fn-1"><p>脚注适合放出处、补充说明这类不想打断正文的内容。 <a class="fn-back" href="#fnref-1" aria-label="回到正文第 1 处">↩</a></p></li><li id="fn-2"><p>比如 Spring Boot 3 要求 Java 17 或更新的版本。 <a class="fn-back" href="#fnref-2" aria-label="回到正文第 1 处">↩</a></p></li></ol></section>]]></content:encoded><category>写作</category><category>博客</category></item>
<item><title>代码块：仿 Xcode 的语法高亮</title><link>https://demo1.661366.xyz/blog/code-blocks/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/code-blocks/</guid><pubDate>Fri, 23 Oct 2026 16:00:00 GMT</pubDate><description>博客里代码块的样子：Xcode 配色、文件名、高亮行、行号、折叠和复制按钮，以及 Java、Kotlin、SQL、YAML 这些常用语言的效果。</description><content:encoded><![CDATA[<p>用三个反引号包住代码，并在后面写上语言名。颜色参照 Xcode 的默认主题，跟着网站的明暗模式切换；高亮在构建时就做好了，访客不用多下载任何脚本。</p>
<h2 id="写法">写法</h2>
<p>代码块自动带行号（复制时不会带上行号），超过 30 行的会先折叠，只显示前 18 行，点底部的「展开全部代码」再看全部。在语言名后面还可以加这些：</p>
<ul>
<li><code>title=&quot;文件名&quot;</code>：代码块顶上显示文件名</li>
<li><code>{行号}</code>：高亮某几行，比如 <code>{3}</code>、<code>{2,5-7}</code></li>
<li><code>nolines</code>：不要行号</li>
<li><code>nofold</code>：再长也不折叠</li>
</ul>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:41"><div class="code-head"><span class="code-lang">Markdown</span><span class="code-side"></span></div><pre><code class="hljs language-markdown"><span class="line"><span class="hljs-code">```java title=&quot;UserController.java&quot; {8-9}</span></span>
<span class="line"><span class="hljs-code">……</span></span>
<span class="line"><span class="hljs-code">```</span></span></code></pre></div>
<h2 id="java">Java</h2>
<div class="codeblock has-lines" style="--ln:2;--rows:24;--cols:81"><div class="code-head"><span class="code-title">UserController.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-meta">@RestController</span></span>
<span class="line"><span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">&quot;/api/users&quot;</span>)</span>
<span class="line"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_ decl__">UserController</span> {</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> <span class="hljs-variable">PAGE_SIZE</span> <span class="hljs-operator">=</span> <span class="hljs-number">20</span>;</span>
<span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="x-t">UserService</span> userService;</span>
<span class="line"></span>
<span class="line is-hl">    <span class="hljs-keyword">public</span> <span class="hljs-title function_">UserController</span><span class="hljs-params">(<span class="x-t">UserService</span> userService)</span> {</span>
<span class="line is-hl">        <span class="hljs-built_in">this</span>.<span class="x-p">userService</span> = userService;</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-comment">/** 根据 ID 查询用户，找不到时返回 404 */</span></span>
<span class="line">    <span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">&quot;/{id}&quot;</span>)</span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-type">ResponseEntity</span>&lt;<span class="x-t">User</span>&gt; <span class="hljs-title function_">get</span><span class="hljs-params">(<span class="hljs-meta">@PathVariable</span> <span class="x-t">Long</span> id)</span> {</span>
<span class="line">        <span class="hljs-keyword">return</span> userService.<span class="x-f">findById</span>(id)</span>
<span class="line">                .<span class="x-f">map</span>(<span class="x-t">ResponseEntity</span>::<span class="x-f">ok</span>)</span>
<span class="line">                .<span class="x-f">orElse</span>(<span class="x-t">ResponseEntity</span>.<span class="x-f">notFound</span>().<span class="x-f">build</span>());</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-meta">@GetMapping</span></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-type">List</span>&lt;<span class="x-t">User</span>&gt; <span class="hljs-title function_">list</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam</span>(defaultValue = <span class="hljs-string">&quot;0&quot;</span>) <span class="hljs-keyword">int</span> page)</span> {</span>
<span class="line">        <span class="hljs-keyword">return</span> userService.<span class="x-f">findAll</span>(<span class="x-t">PageRequest</span>.<span class="x-f">of</span>(page, <span class="x-c">PAGE_SIZE</span>)).<span class="x-f">getContent</span>();</span>
<span class="line">    }</span>
<span class="line">}</span></code></pre></div>
<h2 id="长代码会折叠">长代码会折叠</h2>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:55;--cols:115;--fold:18" data-lines="55"><div class="code-head"><span class="code-title">UserService.java</span><span class="code-side"><span class="code-lang">Java</span></span></div><pre><code class="hljs language-java"><span class="line"><span class="hljs-meta">@Service</span></span>
<span class="line"><span class="hljs-meta">@Transactional</span>(readOnly = <span class="hljs-literal">true</span>)</span>
<span class="line"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_ decl__">UserService</span> {</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Duration</span> <span class="hljs-variable">CACHE_TTL</span> <span class="hljs-operator">=</span> <span class="x-t">Duration</span>.<span class="x-f">ofMinutes</span>(<span class="hljs-number">10</span>);</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="x-t">UserRepository</span> userRepository;</span>
<span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="x-t">PasswordEncoder</span> passwordEncoder;</span>
<span class="line">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="x-t">StringRedisTemplate</span> redis;</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-title function_">UserService</span><span class="hljs-params">(<span class="x-t">UserRepository</span> userRepository, <span class="x-t">PasswordEncoder</span> passwordEncoder, <span class="x-t">StringRedisTemplate</span> redis)</span> {</span>
<span class="line">        <span class="hljs-built_in">this</span>.<span class="x-p">userRepository</span> = userRepository;</span>
<span class="line">        <span class="hljs-built_in">this</span>.<span class="x-p">passwordEncoder</span> = passwordEncoder;</span>
<span class="line">        <span class="hljs-built_in">this</span>.<span class="x-p">redis</span> = redis;</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-type">Optional</span>&lt;<span class="x-t">User</span>&gt; <span class="hljs-title function_">findById</span><span class="hljs-params">(<span class="x-t">Long</span> id)</span> {</span>
<span class="line">        <span class="hljs-keyword">return</span> userRepository.<span class="x-f">findById</span>(id);</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-type">Page</span>&lt;<span class="x-t">User</span>&gt; <span class="hljs-title function_">findAll</span><span class="hljs-params">(<span class="x-t">Pageable</span> pageable)</span> {</span>
<span class="line">        <span class="hljs-keyword">return</span> userRepository.<span class="x-f">findAll</span>(pageable);</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-meta">@Transactional</span></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-type">User</span> <span class="hljs-title function_">register</span><span class="hljs-params">(<span class="x-t">String</span> email, <span class="x-t">String</span> rawPassword)</span> {</span>
<span class="line">        <span class="hljs-keyword">if</span> (userRepository.<span class="x-f">existsByEmail</span>(email)) {</span>
<span class="line">            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalArgumentException</span>(<span class="hljs-string">&quot;邮箱已被注册：&quot;</span> + email);</span>
<span class="line">        }</span>
<span class="line">        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();</span>
<span class="line">        user.<span class="x-f">setEmail</span>(email.<span class="x-f">trim</span>().<span class="x-f">toLowerCase</span>());</span>
<span class="line">        user.<span class="x-f">setPassword</span>(passwordEncoder.<span class="x-f">encode</span>(rawPassword));</span>
<span class="line">        user.<span class="x-f">setCreatedAt</span>(<span class="x-t">Instant</span>.<span class="x-f">now</span>());</span>
<span class="line">        <span class="hljs-keyword">return</span> userRepository.<span class="x-f">save</span>(user);</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-meta">@Transactional</span></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">changePassword</span><span class="hljs-params">(<span class="x-t">Long</span> id, <span class="x-t">String</span> oldPassword, <span class="x-t">String</span> newPassword)</span> {</span>
<span class="line">        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> userRepository.<span class="x-f">findById</span>(id).<span class="x-f">orElseThrow</span>(() -&gt; <span class="hljs-keyword">new</span> <span class="hljs-title class_">NoSuchElementException</span>(<span class="hljs-string">&quot;用户不存在&quot;</span>));</span>
<span class="line">        <span class="hljs-keyword">if</span> (!passwordEncoder.<span class="x-f">matches</span>(oldPassword, user.<span class="x-f">getPassword</span>())) {</span>
<span class="line">            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalArgumentException</span>(<span class="hljs-string">&quot;原密码不正确&quot;</span>);</span>
<span class="line">        }</span>
<span class="line">        user.<span class="x-f">setPassword</span>(passwordEncoder.<span class="x-f">encode</span>(newPassword));</span>
<span class="line">        redis.<span class="x-f">delete</span>(<span class="hljs-string">&quot;user:session:&quot;</span> + id); <span class="hljs-comment">// 改密码后让旧登录失效</span></span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">long</span> <span class="hljs-title function_">countActiveSince</span><span class="hljs-params">(<span class="x-t">Instant</span> since)</span> {</span>
<span class="line">        <span class="hljs-type">String</span> <span class="hljs-variable">key</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;user:active:&quot;</span> + since.<span class="x-f">getEpochSecond</span>();</span>
<span class="line">        <span class="hljs-type">String</span> <span class="hljs-variable">cached</span> <span class="hljs-operator">=</span> redis.<span class="x-f">opsForValue</span>().<span class="x-f">get</span>(key);</span>
<span class="line">        <span class="hljs-keyword">if</span> (cached != <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> <span class="x-t">Long</span>.<span class="x-f">parseLong</span>(cached);</span>
<span class="line">        <span class="hljs-keyword">long</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> userRepository.<span class="x-f">countByLastLoginAfter</span>(since);</span>
<span class="line">        redis.<span class="x-f">opsForValue</span>().<span class="x-f">set</span>(key, <span class="x-t">String</span>.<span class="x-f">valueOf</span>(count), <span class="x-c">CACHE_TTL</span>);</span>
<span class="line">        <span class="hljs-keyword">return</span> count;</span>
<span class="line">    }</span>
<span class="line">}</span></code></pre></div>
<h2 id="配置文件">配置文件</h2>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:53"><div class="code-head"><span class="code-title">application.yml</span><span class="code-side"><span class="code-lang">YAML</span></span></div><pre><code class="hljs language-yaml"><span class="line"><span class="hljs-attr">server:</span></span>
<span class="line">  <span class="hljs-attr">port:</span> <span class="hljs-number">8080</span></span>
<span class="line"><span class="hljs-attr">spring:</span></span>
<span class="line">  <span class="hljs-attr">datasource:</span></span>
<span class="line">    <span class="hljs-attr">url:</span> <span class="hljs-string">jdbc:mysql://localhost:3306/app?useSSL=false</span></span>
<span class="line">    <span class="hljs-attr">username:</span> <span class="hljs-string">app</span></span>
<span class="line">  <span class="hljs-attr">jpa:</span></span>
<span class="line">    <span class="hljs-attr">open-in-view:</span> <span class="hljs-literal">false</span>   <span class="hljs-comment"># 别在视图层懒加载</span></span></code></pre></div>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:52"><div class="code-head"><span class="code-title">pom.xml</span><span class="code-side"><span class="code-lang">XML</span></span></div><pre><code class="hljs language-xml"><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span></span>
<span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span></span></code></pre></div>
<h2 id="sql">SQL</h2>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:42"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">SELECT</span> u.id, u.name, <span class="hljs-built_in">COUNT</span>(o.id) <span class="hljs-keyword">AS</span> orders</span>
<span class="line"><span class="hljs-keyword">FROM</span> users u</span>
<span class="line"><span class="hljs-keyword">LEFT</span> <span class="hljs-keyword">JOIN</span> orders o <span class="hljs-keyword">ON</span> o.user_id <span class="hljs-operator">=</span> u.id</span>
<span class="line"><span class="hljs-keyword">WHERE</span> u.created_at <span class="hljs-operator">&gt;=</span> <span class="hljs-string">&#x27;2026-01-01&#x27;</span></span>
<span class="line"><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> u.id</span>
<span class="line"><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> orders <span class="hljs-keyword">DESC</span></span>
<span class="line">LIMIT <span class="hljs-number">10</span>;</span></code></pre></div>
<h2 id="命令行">命令行</h2>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:62"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">npm</span> run build</span>
<span class="line"><span class="x-cmd">rsync</span> -az --delete-after dist/ user@your-server:/var/www/site/</span>
<span class="line"><span class="hljs-built_in">export</span> JAVA_HOME=/usr/lib/jvm/java-21   <span class="hljs-comment"># 切换 JDK</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;部署完成：<span class="hljs-subst">$(date)</span>&quot;</span></span></code></pre></div>
<h2 id="typescript">TypeScript</h2>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:60"><div class="code-head"><span class="code-lang">TypeScript</span><span class="code-side"></span></div><pre><code class="hljs language-typescript"><span class="line"><span class="hljs-keyword">interface</span> <span class="hljs-title class_ decl__">User</span> {</span>
<span class="line">  <span class="hljs-attr">id</span>: <span class="hljs-built_in">number</span>;</span>
<span class="line">  <span class="hljs-attr">name</span>: <span class="hljs-built_in">string</span>;</span>
<span class="line">}</span>
<span class="line"></span>
<span class="line"><span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">fetchUser</span>(<span class="hljs-params"><span class="hljs-attr">id</span>: <span class="hljs-built_in">number</span></span>): <span class="hljs-title class_">Promise</span>&lt;<span class="hljs-title class_">User</span>&gt; {</span>
<span class="line">  <span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> <span class="hljs-title function_ invoke__">fetch</span>(<span class="hljs-string">`/api/users/<span class="hljs-subst">${id}</span>`</span>);</span>
<span class="line">  <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">ok</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`请求失败：<span class="hljs-subst">${res.<span class="x-p">status</span>}</span>`</span>);</span>
<span class="line">  <span class="hljs-keyword">return</span> res.<span class="hljs-title function_ invoke__">json</span>();</span>
<span class="line">}</span></code></pre></div>
<h2 id="改动对比">改动对比</h2>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:42"><div class="code-head"><span class="code-lang">Diff</span><span class="code-side"></span></div><pre><code class="hljs language-diff"><span class="line is-del"><span class="hljs-deletion">- private static final int PAGE_SIZE = 10;</span></span>
<span class="line is-add"><span class="hljs-addition">+ private static final int PAGE_SIZE = 20;</span></span></code></pre></div>
<p>行内代码用一对反引号，比如 <code>npm run dev</code> 会在本地启动预览。</p>
]]></content:encoded><category>Java</category><category>博客</category></item>
<item><title>标签、摘要与草稿</title><link>https://demo1.661366.xyz/blog/tags-and-drafts/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/tags-and-drafts/</guid><pubDate>Sat, 17 Oct 2026 16:00:00 GMT</pubDate><description>文章开头的几行信息各管什么：标签用来筛选，摘要出现在列表和分享卡片上，草稿先写着不发布。</description><content:encoded><![CDATA[<h2 id="标签">标签</h2>
<p>标签写在文章开头的 <code>tags</code> 里。<a href="https://demo1.661366.xyz/blog/">全部文章</a>页会自动统计所有标签，点一下就能筛选；主页的博客列表在每篇文章后面显示它的第一个标签。</p>
<h2 id="摘要">摘要</h2>
<p><code>description</code> 会出现在三个地方：全部文章页的搜索范围、RSS 订阅，以及把链接分享到微信或 X 时的预览卡片。没写的话，会自动取正文第一段。</p>
<h2 id="草稿">草稿</h2>
<p>在文章开头加上 <code>draft: true</code>，本地预览时能看到它，正式构建时会自动跳过。写完把这一行删掉，重新构建就发布了。在后台写的话，保存和发布本来就是分开的，不用管这一行。</p>
]]></content:encoded><category>写作</category><category>博客</category></item>
<item><title>Linux</title><link>https://demo1.661366.xyz/blog/linux-basics/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/linux-basics/</guid><pubDate>Thu, 09 Apr 2026 16:00:00 GMT</pubDate><description>🚀概述</description><content:encoded><![CDATA[<h2 id="概述">🚀概述</h2>
<p>Linux是一个操作系统(OS)</p>
<p>20世纪80年代，由于Unix 版权的限制，荷兰阿姆斯特丹Vrije大学的Andrew S. Tanenbaum教授决定写一个系统，名为MINIX(即小型的UNIX)，并开放全部源代码给大学教学和研究工作。李纳斯·托瓦兹在赫尔辛基大学读书时也使用了Andrew教授的Minix系统，但他觉得实在太难用，于是也决定自己开发一个操作系统。</p>
<p>李纳斯·托瓦兹(<a href="https://so.csdn.net/so/search?q=Linus&amp;spm=1001.2101.3001.7020" target="_blank" rel="noopener">Linus</a> Torvalds)在1991年初，开始基于Minix(Unix变种)进行开发，同年九月，开发完成内核的0.01版本，命名为Linux。</p>
<p>1960年代末，肯·汤普逊和丹尼斯·里奇一起参与了一个项目，由贝尔实验室、麻省理工学院、以及通用电气公司联合开发了一套多使用者分时作业系统，名叫Multics(MULTiplexed Information and Computing System)，后来由于种种原因Multics计划失败。之后，他和丹尼斯·里奇花了一个月的时间用之前他们设计的B语言开发了一套全新的操作系统，并起名叫做 Unics(UNiplexed Information and Computing System)，后来，Unics慢慢就变成了Unix。</p>
<p>第一版的Unix是基于B语言来开发的。B语言在进行系统编程时不够强大。汤普逊和丹尼斯·里奇对其进行了改造，并于1971年发明了C语言。1973年他们用C语言对 Unix系统进行了重新编写。</p>
<h2 id="安装与下载">📥安装与下载</h2>
<p>详细下载<a href="https://so.csdn.net/so/search?q=%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B&amp;spm=1001.2101.3001.7020" target="_blank" rel="noopener">安装教程</a>： 👉 <a href="https://blog.csdn.net/da_ge_de_nv_ren/article/details/128408962" target="_blank" rel="noopener">点击跳转</a></p>
<figure class="media is-image" style="--ar:8.2849;--w:1425px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-480.avif 480w, https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-800.avif 800w, https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-1200.avif 1200w, https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-1425.avif 1425w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-1200.webp" srcset="https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-480.webp 480w, https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-800.webp 800w, https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-1200.webp 1200w, https://demo1.661366.xyz/assets/img/fc444071bc38aa46-fc444071bc-1425.webp 1425w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1425" height="172" alt="" fetchpriority="high" decoding="async"></picture></figure>
<h2 id="目录结构">🧾目录结构</h2>
<p>Linux 的一切资源都挂载在 / 节点下。</p>
<p><strong>👉/bin：</strong> Binary的缩写。存放系统命令，普通用户和 root 都可以执行。放在 /bin 下的命令在单用户模式下也可以执行。</p>
<p><strong>👉/boot：</strong> 这里存放的是启动 Linux 时使用的一些核心文件，包括一些连接文件以及镜像文件。</p>
<p><strong>👉/dev：</strong> Device的缩写。该目录下存放的是 Linux 的外部设备，在 Linux 中访问设备的方式和访问文件的方式是相同的。</p>
<p><strong>👉/etc：</strong> Etcetera的缩写。这个目录用来存放所有的系统管理所需要的配置文件和子目录。</p>
<p><strong>👉/home：</strong> 用户的主目录，在 Linux 中，每个用户都有一个自己的目录，一般该目录名是以用户的账号命名的。</p>
<p><strong>👉/lib：</strong> Library的缩写。这个目录里存放着系统最基本的动态连接共享库，其作用类似于 Windows 里的 DLL 文件。几乎所有的应用程序都需要用到这些共享库。</p>
<p><strong>👉/lib64：</strong> 64位相关的库会放在这。</p>
<p><strong>👉/media：</strong> linux 系统会自动识别一些设备，例如U盘、光驱等等，当识别后，Linux 会把识别的设备挂载到这个目录下。</p>
<p><strong>👉/mnt：</strong> 系统提供该目录是为了让用户临时挂载别的文件系统的，我们可以将光驱挂载在 /mnt/ 上，然后进入该目录就可以查看光驱里的内容了。</p>
<p><strong>👉/opt：</strong> optional的缩写。这是给主机额外安装软件所摆放的目录。</p>
<p><strong>👉/proc：</strong> Processes的缩写。/proc 是一种伪文件系统（也即虚拟文件系统），存储的是当前内核运行状态的一系列特殊文件，这个目录是一个虚拟的目录，它是系统内存的映射，我们可以通过直接访问这个目录来获取系统信息。这个目录的内容不在硬盘上而是在内存里</p>
<p><strong>👉/root：</strong> 该目录为系统管理员，也称作超级权限者的用户主目录。</p>
<p><strong>👉/run：</strong> 运行目录</p>
<p><strong>👉/sbin：</strong> s 就是 Super User 的意思，是 Superuser Binaries (超级用户的二进制文件) 的缩写，这里存放的是系统管理员使用的系统管理程序。</p>
<p><strong>👉/srv：</strong> 该目录存放一些服务启动之后需要提取的数据。</p>
<p><strong>👉/sys：</strong> 虚拟文件系统。和 /proc/ 目录相似，该目录中的数据都保存在内存中，主要保存与内核相关的信息</p>
<p><strong>👉/tmp：</strong> temporary的缩写这个目录是用来存放一些临时文件的。</p>
<p><strong>👉/usr：</strong> unix system resources缩写。用于存储系统软件资源。</p>
<p><strong>👉/var：</strong> 用于存储动态数据，例如缓存、日志文件、软件运行过程中产生的文件等</p>
<h2 id="vi-vim">🧾VI/VIM</h2>
<p>VI是<a href="https://so.csdn.net/so/search?q=Unix%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F&amp;spm=1001.2101.3001.7020" target="_blank" rel="noopener">Unix操作系统</a>和类Unix操作系统中最通用的文本编辑器。</p>
<p>VIM编辑器是从VI发展出来的一个性能更强大的文本编辑器。可以主动的以字体颜色辨别语法的正确性，方便程序设计。VIM 与VI编辑器完全兼容。</p>
<figure class="media is-image" style="--ar:1.5501;--w:1223px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-480.avif 480w, https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-800.avif 800w, https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-1200.avif 1200w, https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-1223.avif 1223w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-1200.webp" srcset="https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-480.webp 480w, https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-800.webp 800w, https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-1200.webp 1200w, https://demo1.661366.xyz/assets/img/4db615afa4295484-4db615afa4-1223.webp 1223w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1223" height="789" alt="" loading="lazy" decoding="async"></picture></figure>
<p>基本上 vi/vim 共分为三种模式，分别是一般模式，编辑模式和命令模式。</p>
<figure class="media is-image" style="--ar:2.3876;--w:807px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-480.avif 480w, https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-800.avif 800w, https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-807.avif 807w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-807.webp" srcset="https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-480.webp 480w, https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-800.webp 800w, https://demo1.661366.xyz/assets/img/d8f43988276f76cf-d8f4398827-807.webp 807w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="807" height="338" alt="" loading="lazy" decoding="async"></picture></figure>
<h3 id="一般模式">一般模式</h3>
<p>刚刚启动 vi/vim，便进入了一般模式(默认模式)。此状态下敲击键盘动作会被Vim识别为命令，而非输入字符。在此模式可以复制黏贴，删除等操作。若想要编辑文本，进入一般模式，按下i，切换到编辑模式。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>按键</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>x</td>
<td>向后删除一个字符，相当于 del</td>
</tr>
<tr>
<td>X</td>
<td>向前删除一个字符，相当于backspace</td>
</tr>
<tr>
<td>nx</td>
<td>n 为数字，连续向后删除 n 个字符</td>
</tr>
<tr>
<td>dd</td>
<td>删除游标所在的那一整行(常用)</td>
</tr>
<tr>
<td>ndd</td>
<td>n 为数字。删除光标所在的向下 n 行</td>
</tr>
<tr>
<td>d1G</td>
<td>删除光标所在到第一行的所有数据</td>
</tr>
<tr>
<td>dG</td>
<td>删除光标所在到最后一行的所有数据</td>
</tr>
<tr>
<td>d$</td>
<td>删除游标所在处，到该行的最后一个字符</td>
</tr>
<tr>
<td>d0</td>
<td>删除游标所在处，到该行的最前面一个字符</td>
</tr>
<tr>
<td>yy</td>
<td>复制游标所在的那一行(常用)</td>
</tr>
<tr>
<td>nyy</td>
<td>n 为数字。复制光标所在的向下 n 行</td>
</tr>
<tr>
<td>y1G</td>
<td>复制游标所在行到第一行的所有数据</td>
</tr>
<tr>
<td>yG</td>
<td>复制游标所在行到最后一行的所有数据</td>
</tr>
<tr>
<td>y0</td>
<td>复制光标所在的那个字符到该行行首的所有数据</td>
</tr>
<tr>
<td>y$</td>
<td>复制光标所在的那个字符到该行行尾的所有数据</td>
</tr>
<tr>
<td>p</td>
<td>将已复制的数据在光标下一行贴上</td>
</tr>
<tr>
<td>P</td>
<td>将已复制的数据在光标上一行贴上</td>
</tr>
<tr>
<td>J</td>
<td>将光标所在行与下一行的数据结合成同一行</td>
</tr>
<tr>
<td>c</td>
<td>重复删除多个数据，例：向下删除 10 行：10cj</td>
</tr>
<tr>
<td>u</td>
<td>复原前一个动作</td>
</tr>
<tr>
<td>Ctrl+r</td>
<td>重做上一个动作</td>
</tr>
<tr>
<td>+</td>
<td>光标移动到非空格符的下一行</td>
</tr>
<tr>
<td>-</td>
<td>光标移动到非空格符的上一行</td>
</tr>
<tr>
<td>n&lt; space&gt;</td>
<td>n 表示数字，光标会向右移动这一行的 n 个字符</td>
</tr>
<tr>
<td>0 或Home</td>
<td>移动到这一行的最前面字符处</td>
</tr>
<tr>
<td>$ 或End</td>
<td>移动到这一行的最后面字符处</td>
</tr>
<tr>
<td>H</td>
<td>光标移动到这个屏幕的最上方那一行的第一个字符</td>
</tr>
<tr>
<td>M</td>
<td>光标移动到这个屏幕的中央那一行的第一个字符</td>
</tr>
<tr>
<td>L</td>
<td>光标移动到这个屏幕的最下方那一行的第一个字符</td>
</tr>
<tr>
<td>G</td>
<td>移动到这个档案的最后一行</td>
</tr>
<tr>
<td>nG</td>
<td>n 为数字。移动到这个档案的第 n 行</td>
</tr>
<tr>
<td>gg</td>
<td>移动到这个档案的第一行</td>
</tr>
<tr>
<td>n&lt; Enter&gt;</td>
<td>n 为数字。光标向下移动 n 行</td>
</tr>
<tr>
<td>/n</td>
<td>n为要搜索的内容。向光标之下寻找一个名称为n的字符串</td>
</tr>
<tr>
<td>?n</td>
<td>向光标之上寻找一个字符串名称为n的字符串</td>
</tr>
<tr>
<td>n</td>
<td>重复前一个搜寻的动作</td>
</tr>
<tr>
<td>N</td>
<td>与 n 相反，反向进行前一个搜寻动作</td>
</tr>
<tr>
<td>h 或 ←</td>
<td>光标向左移动一个字符</td>
</tr>
<tr>
<td>j 或 ↓</td>
<td>光标向下移动一个字符</td>
</tr>
<tr>
<td>k 或 ↑</td>
<td>光标向上移动一个字符</td>
</tr>
<tr>
<td>l 或 →</td>
<td>光标向右移动一个字符</td>
</tr>
<tr>
<td>Ctrl+ f</td>
<td>屏幕向下移动一页，相当于Page Down按键</td>
</tr>
<tr>
<td>Ctrl + b]</td>
<td>屏幕向上移动一页，相当于Page Up按键</td>
</tr>
<tr>
<td>Ctrl+ d</td>
<td>屏幕向下移动半页</td>
</tr>
<tr>
<td>Ctrl + u</td>
<td>屏幕向上移动半页</td>
</tr>
</tbody></table></div>
<h3 id="编辑模式">编辑模式</h3>
<p>在一般模式之中，只要按下 i, o, a 等字符就可以进入输入模式。在编辑模式当中，左下角状态栏中会出现 –INSERT- 的字样，那就是可以输入任意字符的提示。这个时候，键盘上除了 Esc 这个按键之外，其他的按键都可以视作为一般的输入按钮。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>按键</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>i, I</td>
<td>i 为从目前光标所在处输入， I 在目前所在行的第一个非空格符处开始输入</td>
</tr>
<tr>
<td>a, A</td>
<td>a 为从目前光标所在的下一个字符处开始输入， A 为从光标所在行的最后一个字符处开始输入</td>
</tr>
<tr>
<td>o, O</td>
<td>o 为在目前光标所在的下一行处输入新的一行； O 为在目前光标所在的上一行处输入新的一行</td>
</tr>
<tr>
<td>r, R</td>
<td>r 只会取代光标所在的那一个字符一次；R会一直取代光标所在的文字，直到按下 ESC 为止</td>
</tr>
</tbody></table></div>
<h3 id="命令模式">命令模式</h3>
<p>在一般模式之中，只要按下 ：或 / 就可以进入命令模式。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>按键</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>:w</td>
<td>保存</td>
</tr>
<tr>
<td>:w!</td>
<td>若文件属性为只读时，强制写入该档案。不过，能不能写入， 还跟你对该档案的档案权限有关</td>
</tr>
<tr>
<td>:q</td>
<td>退出</td>
</tr>
<tr>
<td>:q!</td>
<td>强制退出</td>
</tr>
<tr>
<td>:wq</td>
<td>保存并退出</td>
</tr>
<tr>
<td>ZZ</td>
<td>效果等同于保存并退出</td>
</tr>
<tr>
<td>ZQ</td>
<td>效果等同于 :q!</td>
</tr>
<tr>
<td>:w [filename]</td>
<td>将编辑的数据储存成另一个文件</td>
</tr>
<tr>
<td>:r [filename]</td>
<td>在编辑的数据中，读入另一个文件的数据。将filename这个档案内容加到游标所在行后面</td>
</tr>
<tr>
<td>:n1,n2 w [filename]</td>
<td>将 n1 到 n2 的内容储存成 filename 这个档案</td>
</tr>
<tr>
<td>:! command</td>
<td>暂时离开 vi 到指令行模式下执行 command 的显示结果！例：:! ls /home 即可在 vi 当中察看 /home 底下以 ls 输出的档案信息</td>
</tr>
<tr>
<td>:set nu</td>
<td>显示行号</td>
</tr>
<tr>
<td>:set nonu</td>
<td>取消行号</td>
</tr>
<tr>
<td>:noh</td>
<td>取消高亮显示</td>
</tr>
<tr>
<td>: s/old/new</td>
<td>替换当前匹配到的第一个 old 为 new</td>
</tr>
<tr>
<td>: s/old/new/g</td>
<td>替换当前匹配到所有的 old 为 new</td>
</tr>
<tr>
<td>:%s/old/new</td>
<td>替换文档中每一行匹配到的第一个 old 为 new</td>
</tr>
<tr>
<td>:%s/old/new/g</td>
<td>替换文档中匹配到所有的 old 为 new</td>
</tr>
</tbody></table></div>
<h2 id="网络配置">🌐网络配置</h2>
<p>VMware安装完成后会在宿主机上生成两块虚拟网卡，这两块网卡与虚拟机管理的两个虚拟交换机相连。</p>
<figure class="media is-image" style="--ar:2.5930;--w:892px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-480.avif 480w, https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-800.avif 800w, https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-892.avif 892w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-892.webp" srcset="https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-480.webp 480w, https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-800.webp 800w, https://demo1.661366.xyz/assets/img/c8fd34ce740e077a-c8fd34ce74-892.webp 892w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="892" height="344" alt="" loading="lazy" decoding="async"></picture></figure>
<p>VMware管理了3个特殊的虚拟交换机：VMnet0（用于桥接模式），VMnet8（用于NAT模式），VMnet1（用于仅主机模式）。</p>
<figure class="media is-image" style="--ar:5.0213;--w:708px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/5b0a2859229c30fb-5b0a285922-480.avif 480w, https://demo1.661366.xyz/assets/img/5b0a2859229c30fb-5b0a285922-708.avif 708w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/5b0a2859229c30fb-5b0a285922-708.webp" srcset="https://demo1.661366.xyz/assets/img/5b0a2859229c30fb-5b0a285922-480.webp 480w, https://demo1.661366.xyz/assets/img/5b0a2859229c30fb-5b0a285922-708.webp 708w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="708" height="141" alt="" loading="lazy" decoding="async"></picture></figure>
<p>VMware提供了3种网络工作模式：Bridged(桥接模式)，NAT（网络地址转换模式），Host-Only（仅主机模式）。</p>
<p><strong>🌐桥接模式：</strong></p>
<p>📎 虚拟机直接连接外部物理网络的模式，主机起到了网桥的作用。这种模式下，虚拟机可以直接访问外部网络，并且对外部网络是可见的。</p>
<p>👉将宿主机的网卡通过虚拟网桥与VMware管理的虚拟交换机VMnet0相连，所有网络连接设置为桥接模式的虚拟机都会连接到虚拟交换机VMnet0上。</p>
<p>👉这样所有的虚拟机与宿主机以及与宿主机在同一个局域网下的主机之间都是可以相互访问的。</p>
<p>👉在桥接模式下，虚拟机IP地址需要与宿主机在同一个网段，如果需要联网，则虚拟机的网关和DNS也需要与宿主机被桥接的网卡一致。</p>
<figure class="media is-image" style="--ar:3.0476;--w:1600px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-480.avif 480w, https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-800.avif 800w, https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-1200.avif 1200w, https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-1200.webp" srcset="https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-480.webp 480w, https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-800.webp 800w, https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-1200.webp 1200w, https://demo1.661366.xyz/assets/img/0b7650e7963f9053-0b7650e796-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="525" alt="" loading="lazy" decoding="async"></picture></figure>
<p>📍 虚拟网桥会转发宿主机网卡接收到的广播和组播信息，以及目标为虚拟交换机网段的单播。所以，与虚拟交换机机连接的虚拟网卡，如: eth0、ens33等能接收到路由器发出的DHCP信息及路由更新。</p>
<p>📍桥接模式是通过虚拟网桥将主机上的网卡与虚拟交换机VMnet0连接在一起，虚拟机上的虚拟网卡都连接在虚拟交换机VMnet0上，在桥接模式下，虚拟机IP必须与宿主机的IP在同一网段且子网掩码、网关与DNS也要与宿主机网卡的一致。</p>
<p>📍桥接模式的虚拟机会占用宿主机所在局域网内的IP地址资源。如果宿主机所在局域网中做了IP地址管理，比如IP地址需要公司网络部门审批通过后才能使用，那么虚拟机的IP地址也会受此管理规则的约束。</p>
<p><strong>🌐NAT模式：</strong></p>
<p>📎虚拟机和主机构建一个专用网络，并通过虚拟网络地址转换(NAT)设备对IP进行转换。虚拟机通过共享主机IP可以访问外部网络，但外部网络无法访问虚拟机。</p>
<p>👉如果你的网络ip资源紧缺，但又希望虚拟机能够联网，NAT模式是最好的选择。</p>
<p>👉NAT模式借助虚拟NAT设备和虚拟DHCP服务器，使得虚拟机可以联网</p>
<figure class="media is-image" style="--ar:2.4660;--w:1524px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-480.avif 480w, https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-800.avif 800w, https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-1200.avif 1200w, https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-1524.avif 1524w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-1200.webp" srcset="https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-480.webp 480w, https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-800.webp 800w, https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-1200.webp 1200w, https://demo1.661366.xyz/assets/img/804419af28213da2-804419af28-1524.webp 1524w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1524" height="618" alt="" loading="lazy" decoding="async"></picture></figure>
<p>📍使用NAT模式，就是让虚拟机借助NAT（网络地址转换功能），通过宿主机所在的网络来访问互联网。</p>
<p>📍在虚拟机连接Vmnet8虚拟交换机时，虚拟机会将虚拟NAT设备及虚拟DHCP服务器连接到Vmnet8虚拟交换机上。依靠虚拟的NAT设备及虚拟的DHCP设备，如果你的宿主机已经联网了，那么你的虚拟机也就可以联网。</p>
<p>📍同时宿主机上的虚拟网卡VMwareNetwork Adapter VMnet8也会连接到Vmnet8虚拟交换机上，用于宿主机和虚拟机的通信。如果禁用掉宿主机上的虚拟网卡VMwareNetwork Adapter VMnet8，虚拟机仍然是可以上网的，只是宿主无法再访问VMnet8网段下的虚拟机了，但是虚拟机仍然可以访问宿主机。</p>
<p>📍虚拟网卡VMwareNetwork Adapter VMnet8只是作为主机与虚拟机通信的接口，虚拟机并不是依靠虚拟网卡VMware Network Adapter VMnet8来联网的。</p>
<p>📍由于NAT服务协议对外部网络隐蔽内部网络，因此虚拟机可以通过NAT服务器访问宿主机所在局域网内的其他真实主机，但这些真实主机且不能反过来访问虚拟机。</p>
<p><strong>🌐仅主机模式</strong></p>
<p>📎虚拟机只与主机共享一个专用网络，与外部网络无法通信。</p>
<p>👉仅主机模式就是NAT模式去掉了虚拟NAT设备，使虚拟机无法与公网进行通信，而宿主机使用虚拟网卡VMware Network Adapter VMnet1连接VMnet1虚拟交换机来与虚拟机进行通信。</p>
<p>👉仅主机模式将虚拟机与公网隔开，使虚拟机成为一个独立的系统，只能与宿主机相互通讯。</p>
<figure class="media is-image" style="--ar:2.6578;--w:1600px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-480.avif 480w, https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-800.avif 800w, https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-1200.avif 1200w, https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-1200.webp" srcset="https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-480.webp 480w, https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-800.webp 800w, https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-1200.webp 1200w, https://demo1.661366.xyz/assets/img/6af8dc5e1aa25d72-6af8dc5e1a-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="602" alt="" loading="lazy" decoding="async"></picture></figure>
<p>📍仅主机模式通过宿主机的虚拟网卡VMwareNetwork Adapter VMnet1来连接虚拟交换机VMnet1，来达到宿主机与虚拟机通信的目的，但是虚拟机不能主动与宿主机通信，是单向连通的。</p>
<p>📍如果想要在仅主机模式下联网，可以将能联网的主机网卡共享给VMware Network Adapter VMnet1，这样就可以实现虚拟机联网。</p>
<h3 id="常用命令">常用命令</h3>
<h4 id="ifconfig"><strong>📚 ifconfig</strong></h4>
<p>📔 用于显示或设置网络设备。可设置网络设备的状态，或是显示目前的设置</p>
<p>📃启动关闭指定网卡</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:18"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">ifconfig</span> eth0 down</span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 up</span></code></pre></div>
<p>📃为网卡配置和删除IPv6地址</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:65"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">ifconfig</span> eth0 add 33ffe:3240:800:1005::2/ 64 <span class="hljs-comment"># 为网卡设置IPv6地址</span></span>
<span class="line"></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 del 33ffe:3240:800:1005::2/ 64 <span class="hljs-comment"># 为网卡删除IPv6地址</span></span></code></pre></div>
<p>📃用ifconfig修改MAC地址</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:62"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">ifconfig</span> eth0 down <span class="hljs-comment"># 关闭网卡</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 hw ether 00:AA:BB:CC:DD:EE <span class="hljs-comment"># 修改MAC地址</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 up <span class="hljs-comment"># 启动网卡</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth1 hw ether 00:1D:1C:1D:1E <span class="hljs-comment"># 关闭网卡并修改MAC地址 </span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth1 up <span class="hljs-comment"># 启动网卡</span></span></code></pre></div>
<p>📃配置IP地址</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:72"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 给eth0网卡配置IP地址</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 192.168.1.56 </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 给eth0网卡配置IP地址,并加上子掩码</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 192.168.1.56 netmask 255.255.255.0 </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 给eth0网卡配置IP地址,加上子掩码,加上个广播地址</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255</span></code></pre></div>
<p>📃启用和关闭ARP协议</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">ifconfig</span> eth0 arp   <span class="hljs-comment"># 开启</span></span>
<span class="line"><span class="x-cmd">ifconfig</span> eth0 -arp  <span class="hljs-comment"># 关闭</span></span></code></pre></div>
<p>📃设置最大传输单元</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:41"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 设置能通过的最大数据包大小为 1500 bytes</span></span>
<span class="line"> <span class="x-cmd">ifconfig</span> eth0 mtu 1500 </span></code></pre></div>
<h4 id="ping">📚ping</h4>
<p>📔 命令用于检测主机。执行 ping 指令会使用 ICMP 传输协议，发出要求回应的信息，若远端主机的网络功能没有问题，就会回应该信息，因而得知该主机运作正常。</p>
<h3 id="修改静态ip">修改静态IP</h3>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:47"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">//用Vim进入ifcfg- ens33配置文件，将动态改成静态</span>
<span class="line"><span class="x-cmd">vim</span> /etc/sysconfig/network-scripts/ifcfg- ens33</span></code></pre></div>
<figure class="media is-image" style="--ar:3.0864;--w:1500px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-480.avif 480w, https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-800.avif 800w, https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-1200.avif 1200w, https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-1500.avif 1500w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-1200.webp" srcset="https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-480.webp 480w, https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-800.webp 800w, https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-1200.webp 1200w, https://demo1.661366.xyz/assets/img/4db683fec79f1e5c-4db683fec7-1500.webp 1500w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1500" height="486" alt="" loading="lazy" decoding="async"></picture></figure>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:23"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 重启网路</span></span>
<span class="line"><span class="x-cmd">service</span> network restart</span></code></pre></div>
<p><strong>❓可能会遇到的问题：</strong></p>
<p>❗ 物理机能ping通虚拟机，但是虚拟机ping不通物理机，一般都是因为物理机的防火墙问题,把防火墙关闭就行</p>
<p>❗虚拟机能Ping通物理机，但是虚拟机Ping不通外网，一般都是因为DNS的设置有问题</p>
<p>❗虚拟机Ping <a href="http://www.baidu.com%E6%98%BE%E7%A4%BA%E5%9F%9F%E5%90%8D%E6%9C%AA%E7%9F%A5%E7%AD%89%E4%BF%A1%E6%81%AF%EF%BC%8C%E4%B8%80%E8%88%AC%E6%9F%A5%E7%9C%8BGATEWAY%E5%92%8CDNS%E8%AE%BE%E7%BD%AE%E6%98%AF%E5%90%A6%E6%AD%A3%E7%A1%AE" target="_blank" rel="noopener">www.baidu.com显示域名未知等信息，一般查看GATEWAY和DNS设置是否正确</a></p>
<p>❗如果以上全部设置完还是不行，需要关闭NetworkManager服务</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:39"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> stop NetworkManager  <span class="hljs-comment"># 关闭</span></span>
<span class="line"><span class="x-cmd">systemctl</span> <span class="hljs-built_in">disable</span> NetworkManager <span class="hljs-comment"># 禁用</span></span></code></pre></div>
<p>❗如果检查发现 systemctl status network有问题需要检查ifcfg-ens33</p>
<h3 id="修改主机名">修改主机名</h3>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:66"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">hostname</span> //查看主机名</span>
<span class="line">1</span>
<span class="line"><span class="x-cmd">vim</span> /etc/hostname //进入配置文件修改主机名，修改完成后需要重启</span>
<span class="line">1</span>
<span class="line"><span class="x-cmd">hostnamectl</span> set-hostname 修改的主机名  // 更好的修改方式，不用重启</span>
<span class="line">1</span></code></pre></div>
<p><strong>在hosts文件中添加</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:44"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">vim</span> /etc/hosts //进入hosts配置文件，进行添加</span></code></pre></div>
<figure class="media is-image" style="--ar:7.3830;--w:1041px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-480.avif 480w, https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-800.avif 800w, https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-1041.avif 1041w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-1041.webp" srcset="https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-480.webp 480w, https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-800.webp 800w, https://demo1.661366.xyz/assets/img/9f062155e955ef7e-9f062155e9-1041.webp 1041w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1041" height="141" alt="" loading="lazy" decoding="async"></picture></figure>
<p>找到电脑目录 C:WindowsSystem32driversetc下的hosts文件将192.168.111.100 hhh 添加进去</p>
<p>打开DOS窗口输入 <code>Ping hhh</code> 测试是否可以Ping通</p>
<h3 id="远程登录">远程登录</h3>
<p>Dos窗口登录</p>
<figure class="media is-image" style="--ar:2.0894;--w:1356px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-480.avif 480w, https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-800.avif 800w, https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-1200.avif 1200w, https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-1356.avif 1356w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-1200.webp" srcset="https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-480.webp 480w, https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-800.webp 800w, https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-1200.webp 1200w, https://demo1.661366.xyz/assets/img/0390bad480b99203-0390bad480-1356.webp 1356w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1356" height="649" alt="" loading="lazy" decoding="async"></picture></figure>
<p>通常在工作过程中，公司中使用的真实服务器或者是云服务器，都不允许除运维人员之外的员工直接接触，因此就需要通过远程登录的方式来操作。目前，比较主流的有Xshell, SSH Secure Shell, SecureCRT,FinalShell。</p>
<h2 id="系统管理">💻系统管理</h2>
<p>计算机中，一个正在执行的程序或命令，被叫做“进程”，启动一次之后一直存在、常驻内存的进程，一般被称作“服务”。</p>
<h3 id="常用命令-2">常用命令</h3>
<h4 id="service命令-centos6"><strong>📚 service命令</strong> (CentOS6)</h4>
<p>📔用来控制系统服务的实用工具，它以启动、停止、重新启动和关闭系统服务，还可以显示所有系统服务的当前状态。</p>
<p>🔦 查看服务的方法：<code>/etc/init.d/服务名</code></p>
<p>📝 service 服务名 start | stop | restart | status</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>语法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>–status-all</td>
<td>显示所服务的状态</td>
</tr>
<tr>
<td>-h</td>
<td>显示帮助信息</td>
</tr>
</tbody></table></div>
<p>📃显示所有服务的状态：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:20"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">service</span> --status-all</span></code></pre></div>
<h4 id="systemctl命令">📚 systemctl命令</h4>
<p>📔systemctl命令来自于英文词组”system control“的缩写，其功能是用于管理系统服务。从RHEL/CentOS7版本之后初始化进程服务init被替代成了systemd服务，systemd初始化进程服务的管理是通过systemctl命令完成的，从功能上涵盖了之前service、chkconfig、init、setup等多条命令的大部分功能。</p>
<p>🔦 查看服务的方法：<code>/usr/lib/systemd/system</code></p>
<p>📝 service start | stop | restart | status 服务名</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>start</td>
<td>启动服务</td>
</tr>
<tr>
<td>stop</td>
<td>停止服务</td>
</tr>
<tr>
<td>restart</td>
<td>重启服务</td>
</tr>
<tr>
<td>enable</td>
<td>使某服务开机自启</td>
</tr>
<tr>
<td>disable</td>
<td>关闭某服务开机自启</td>
</tr>
<tr>
<td>status</td>
<td>查看服务状态</td>
</tr>
<tr>
<td>list -units --type=service</td>
<td>列举所有已启动服务</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:36"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 启动指定的服务</span></span>
<span class="line"><span class="x-cmd">systemctl</span> start sshd</span>
<span class="line"><span class="hljs-comment"># 停止指定的服务</span></span>
<span class="line"><span class="x-cmd">systemctl</span> stop sshd</span>
<span class="line"><span class="hljs-comment"># 重启指定的服务</span></span>
<span class="line"><span class="x-cmd">systemctl</span> restart sshd</span>
<span class="line"><span class="hljs-comment"># 查看指定服务的运行状态</span></span>
<span class="line"><span class="x-cmd">systemctl</span> status sshd</span>
<span class="line"><span class="hljs-comment"># 将指定的服务加入到开机启动项中</span></span>
<span class="line"><span class="x-cmd">systemctl</span> <span class="hljs-built_in">enable</span> sshd</span>
<span class="line"><span class="hljs-comment"># 将指定的服务从开机启动项中取消</span></span>
<span class="line"><span class="x-cmd">systemctl</span> <span class="hljs-built_in">disable</span> sshd</span>
<span class="line"><span class="hljs-comment"># 显示系统中所有已启动的服务列表信息</span></span>
<span class="line"><span class="x-cmd">systemctl</span> list-units --<span class="hljs-built_in">type</span>=service</span></code></pre></div>
<h4 id="setup命令">📚setup命令</h4>
<p>📔setup命令是一个基于文本界面的系统管理工具，集成了用户认证管理、防火墙管理、网络管理和系统服务管理。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>setup面板</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>Authentication configuration</td>
<td>用户管理认证</td>
</tr>
<tr>
<td>Firewall configuration</td>
<td>防火墙管理</td>
</tr>
<tr>
<td>Network configuration</td>
<td>网络管理</td>
</tr>
<tr>
<td>System services</td>
<td>系统服务自动启动管理</td>
</tr>
</tbody></table></div>
<p>📃进入setup管理界面</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:5"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">setup</span></span></code></pre></div>
<h4 id="chkconfig命令">📚chkconfig命令</h4>
<p>📔用于检查，设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序，它可查询操作系统在每一个执行等级中会执行哪些系统服务，其中包括各类常驻服务。</p>
<p>📝<code>chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level &lt;等级代号&gt;][系统服务][on/off/reset]</code></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>–add</td>
<td>增加所指定的系统服务，让 chkconfig 指令得以管理它，并同时在系统启动的叙述文件内增加相关数据。</td>
</tr>
<tr>
<td>–del</td>
<td>删除所指定的系统服务，不再由 chkconfig 指令管理，并同时在系统启动的叙述文件内删除相关数据。</td>
</tr>
<tr>
<td>–level&lt;等级代号&gt;</td>
<td>指定读系统服务要在哪一个执行等级中开启或关闭。</td>
</tr>
</tbody></table></div>
<p>🔖chkconfig 没有参数运行时，显示用法。如果在服务名后面指定了on，off或者reset，那么chkconfig 会改变指定服务的启动信息。on和off分别指服务被启动和停止，reset指重置服务的启动信息，无论有问题的初始化脚本指定了什么。on和off开关，系统默认只对运行级2，3，4，5有效，但是reset可以对所有运行级有效。</p>
<p>🔖level选项可以指定要查看的运行级而不一定是当前运行级。对于每个运行级，只能有一个启动脚本或者停止脚本。当切换运行级时，init不会重新启动已经启动的服务，也不会再次去停止已经停止的服务。</p>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:13;--cols:85"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">chkconfig</span> --list  <span class="hljs-comment"># 列出所有的系统服务</span></span>
<span class="line"></span>
<span class="line"><span class="x-cmd">chkconfig</span> --add httpd <span class="hljs-comment"># 将httpd添加为受chkconfi管理的服务：</span></span>
<span class="line"></span>
<span class="line"><span class="x-cmd">chkconfig</span> --del httpd <span class="hljs-comment"># 删除httpd服务</span></span>
<span class="line"></span>
<span class="line"><span class="x-cmd">chkconfig</span> --level 35 httpd on  <span class="hljs-comment"># 在级别3和5上设定服务为“on”，开机自动启动也是配置这个</span></span>
<span class="line"></span>
<span class="line"><span class="x-cmd">chkconfig</span> --level 01246 httpd off  <span class="hljs-comment"># 在其级别上设为off</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 为了确认你的配置被正确的修改了，我们可以列出服务运行级别，如下所示：</span></span>
<span class="line"><span class="hljs-comment"># chkconfig --list httpd</span></span>
<span class="line"><span class="x-cmd">httpd</span>   0:off   1:off    2:off    3:on    4:off    5:on    6:off</span></code></pre></div>
<h4 id="firewall-cmd命令">📚firewall-cmd命令</h4>
<p>📔用于防火墙策略管理，是firewalld服务的配置工具。使用firewall-cmd命令修改的防火墙策略会立即生效，但重启后失效，因此推荐要加上permanent参数。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>–state</td>
<td>显示当前服务运行状态</td>
</tr>
<tr>
<td>–zone=public --list-ports</td>
<td>查看所有打开运行的端口</td>
</tr>
<tr>
<td>–permanent</td>
<td>策略写入到永久生效表中</td>
</tr>
<tr>
<td>–reload</td>
<td>不重启立即加载</td>
</tr>
<tr>
<td>–list-all-zones</td>
<td>查看区域信息情况</td>
</tr>
<tr>
<td>–panic-on</td>
<td>拒绝所有包</td>
</tr>
<tr>
<td>–panic-off</td>
<td>取消拒绝状态</td>
</tr>
<tr>
<td>–query-panic</td>
<td>查看是否拒绝</td>
</tr>
</tbody></table></div>
<p>📃查看当前防火墙状态</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:20"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">firewall-cmd</span> --state</span></code></pre></div>
<p>📃查看防火墙当前放行端口号列表</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:39"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">firewall-cmd</span> --zone=public --list-ports</span></code></pre></div>
<p>📃重新加载防火墙策略，立即生效</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">firewall-cmd</span> --reload</span></code></pre></div>
<p>📃查看当前防火墙默认使用区域名称</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">firewall-cmd</span> --get-default-zone </span></code></pre></div>
<p>📃参考实例</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:31;--cols:126;--fold:18" data-lines="31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 开放某个端口，如8080端口</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --zone=public --add-port=8080/tcp --permanent</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 重新加载配置</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --reload</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看防火墙锁开放的端口</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --zone=public --list-ports</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看某个端口的访问权限，如8080</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --zone=public --query-port=8080/tcp</span>
<span class="line"><span class="hljs-comment"># 关闭某个端口的防火墙，如8080端口</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --zone=public --remove-port=8080/tcp --permanent</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 放通某个端口段</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --permanent --zone=public --add-port=1000-2000/tcp</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 放通某个IP访问，默认允许</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --permanent --add-rich-rule=<span class="hljs-string">&#x27;rule family=ipv4 source address=192.168.1.169 accept&#x27;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 禁止某个IP访问</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --permanent --add-rich-rule=<span class="hljs-string">&#x27;rule family=ipv4 source address=10.0.0.42 drop&#x27;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 放通某个IP访问某个端口</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --permanent --add-rich-rule=<span class="hljs-string">&#x27;rule family=ipv4 source address=192.168.1.169 port protocol=tcp port=6379 accept&#x27;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 移除以上规则</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --permanent --remove-rich-rule=<span class="hljs-string">&#x27;rule family=ipv4 source address=192.168.1.169 port protocol=tcp port=6379 accept&#x27;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 放通某个IP段访问</span></span>
<span class="line"><span class="x-cmd">firewall-cmd</span> --permanent --add-rich-rule=<span class="hljs-string">&#x27;rule family=ipv4 source address=10.0.0.0/24 accept&#x27;</span></span></code></pre></div>
<h3 id="运行级别">运行级别</h3>
<p>CentOS6</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:67"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">开机`➡`BIOS `➡`/boot `➡ `init进程`➡`运行级别` ➡ `运行级别对应的服务</span></code></pre></div>
<p>🔦查看默认级别: <code>vi /etc/inittab</code></p>
<p><strong>❄️Linux系统有<code>7</code>种运行级别(runlevel)：<code>常用的级别是3和5</code></strong></p>
<p>💨<strong>运行级别0：</strong> 系统停机状态，系统默认运行级别不能设为0，否则不能正常启动。</p>
<p>💨<strong>运行级别1：</strong> 单用户工作状态，root权限，用于系统维护，禁止远程登陆。</p>
<p>💨<strong>运行级别2：</strong> 多用户状态(没有NFS：网络文件系统)，不支持网络。</p>
<p>💨<strong>运行级别3：</strong> 完全的多用户状态(有NFS：网络文件系统)，登陆后进入控制台命令行模式。</p>
<p>💨<strong>运行级别4：</strong> 系统未使用，保留。</p>
<p>💨<strong>运行级别5：</strong> X11控制台，登陆后进入图形GUI模式。</p>
<p>💨<strong>运行级别6：</strong> 系统正常关闭并重启，默认运行级别不能设为6，否则不能正常启动。</p>
<p>❄️<strong>CentOS7 的运行级别简化为：</strong></p>
<p>☃️<strong>multi-user.target</strong> 等价于原运行级别3，多用户有网，无图形界面）</p>
<p>⛄️<strong>graphical.target</strong> 等价于原运行级别5，多用户有网，有图形界面</p>
<p><strong>📃查看当前运行级别</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> get-default</span></code></pre></div>
<p><strong>📝修改当前运行级别</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> set-default xxx.target</span></code></pre></div>
<h3 id="防火墙开闭">防火墙开闭</h3>
<p>CentOS 7版本后防火墙默认使用的是firewalld**📃查看防火墙状态**</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:34"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> status firewalld.service</span></code></pre></div>
<p>🖇运行上述命令后，如果看到有绿色字样标注的“active（running）”，说明防火墙是开启状态。</p>
<p><strong>📃关闭运行的防火墙</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> stop firewalld.service</span></code></pre></div>
<p>🖇关闭后，可查看防火墙状态，当显示disavtive（dead）的字样，说明CentOS 7防火墙已经关闭。</p>
<p>🖇上面的命令只是临时关闭了CentOS 7防火墙，当重启操作系统后，防火墙服务还是会再次启动。如果想要永久关闭防火墙则还需要开机禁用防火墙服务。</p>
<p><strong>📃开机禁用防火墙服务</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:35"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> <span class="hljs-built_in">disable</span> firewalld.service</span></code></pre></div>
<p><strong>📃启动防火墙</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:25"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> start firewalld</span></code></pre></div>
<p><strong>📃开机启用防火墙</strong></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">systemctl</span> <span class="hljs-built_in">enable</span> firewalld</span></code></pre></div>
<h3 id="关机重启命令">关机重启命令</h3>
<p>在linux领域内大多用在服务器上，很少遇到关机的操作。毕竟服务器上跑一个服务是不会停止的，除非特殊情况下，不得已才会关机。</p>
<h4 id="shutdown命令">📚shutdown命令</h4>
<p>📔可以用来进行关机程序，并且在关机以前传送讯息给所有使用者正在执行的程序，shutdown 也可以用来重开机。shutdown命令作用同于poweroff命令。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-f</td>
<td>重新启动时不执行fsck</td>
</tr>
<tr>
<td>-F</td>
<td>重新启动时执行fsck</td>
</tr>
<tr>
<td>-h</td>
<td>将系统关机</td>
</tr>
<tr>
<td>-c</td>
<td>取消关机任务</td>
</tr>
<tr>
<td>-k</td>
<td>发送信息给所有用户</td>
</tr>
<tr>
<td>-n</td>
<td>不调用init程序进行关机</td>
</tr>
<tr>
<td>-r</td>
<td>将系统重启</td>
</tr>
<tr>
<td>-t</td>
<td>送出警告信息和删除信息之间要延迟多少秒</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:17;--cols:67"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 立即关机</span></span>
<span class="line"><span class="x-cmd">shutdown</span> -h now</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 指定 10 分钟后关机</span></span>
<span class="line"><span class="x-cmd">shutdown</span> -h 10</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 立即重新启动计算机</span></span>
<span class="line"><span class="x-cmd">shutdown</span> -r now</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定当前的服务器指定时间自动关机，格式为”小时:分钟“：</span></span>
<span class="line"><span class="x-cmd">shutdown</span> -h 21:00</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定当前的服务器5分钟后关机，同时发送警告信息给所有已登录的用户：</span></span>
<span class="line"><span class="x-cmd">shutdown</span> +5 <span class="hljs-string">&quot;System will shutdown after 5 minutes&quot;</span> </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 取消当前服务器上已有的关机任务：</span></span>
<span class="line"><span class="x-cmd">shutdown</span> -c </span></code></pre></div>
<h4 id="sync命令">📚sync命令</h4>
<p>📔用于数据同步,sync命令是在关闭Linux系统时使用的。</p>
<p>🔖sync命令用于强制被改变的内容立刻写入磁盘，更新信息速度非常快， 在Linux/Unix系统中，在文件或数据处理过程中一般先放到内存缓冲区中，等到适当的时候再写入磁盘，以提高系统的运行效率。</p>
<p>🔖sync命令则可用来强制将内存缓冲区中的数据立即写入磁盘中。用户通常不需执行sync命令，系统会自动执行update或bdflush操作，将缓冲区的数据写入磁盘。只有在update或bdflush无法执行或用户需要非正常关机时，才需手动执行sync命令。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>–help</td>
<td>显示命定详细帮助信息</td>
</tr>
<tr>
<td>–version</td>
<td>显示版本号</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:14"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示帮助信息</span></span>
<span class="line"><span class="hljs-built_in">sync</span> --<span class="hljs-built_in">help</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示版本</span></span>
<span class="line"><span class="hljs-built_in">sync</span> --version</span></code></pre></div>
<h4 id="halt命令">📚halt命令</h4>
<p>📔用于关闭当前服务器系统，类似于poweroff与shutdown命令，没有太大差别。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n : 在关机前不做将记忆体资料写回硬盘的动作</td>
<td></td>
</tr>
<tr>
<td>-w 模拟关机，把过程写入到日志文件</td>
<td></td>
</tr>
<tr>
<td>-d 不写入日志纪录</td>
<td></td>
</tr>
<tr>
<td>-f 强制关机或重启</td>
<td></td>
</tr>
<tr>
<td>-i 关机或重启前关掉所有的网络服务</td>
<td></td>
</tr>
<tr>
<td>-p : 当关机的时候，顺便做关闭电源的动作</td>
<td></td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 关闭系统</span></span>
<span class="line"><span class="x-cmd">halt</span></span>
<span class="line"><span class="hljs-comment"># 关闭系统并关闭电源</span></span>
<span class="line"><span class="x-cmd">halt</span> -p</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 关闭系统，但不留下纪录</span></span>
<span class="line"><span class="x-cmd">halt</span> -d</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 强制关闭当前服务器系统：</span></span>
<span class="line"><span class="x-cmd">halt</span> -f</span></code></pre></div>
<h4 id="poweroff命令">📚poweroff命令</h4>
<p>📔用来关闭计算机操作系统并且切断系统电源。如果确认系统中已经没有用户存在且所有数据都已保存，需要立即关闭系统，可以使用poweroff命令。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n</td>
<td>关闭操作系统时不执行sync操作</td>
</tr>
<tr>
<td>-w</td>
<td>不真正关闭操作系统，仅在日志文件“/var/log/wtmp”中</td>
</tr>
<tr>
<td>-d</td>
<td>关闭操作系统时，不将操作写入日志文件“/var/log/wtmp”中添加相应的记录</td>
</tr>
<tr>
<td>-f</td>
<td>强制关闭操作系统</td>
</tr>
<tr>
<td>-i</td>
<td>关闭操作系统之前关闭所有的网络接口</td>
</tr>
<tr>
<td>-h</td>
<td>关闭操作系统之前将系统中所有的硬件设置为备用模式</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:60"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 关闭计算机操作系统并且切断系统电源：</span></span>
<span class="line"><span class="x-cmd">poweroff</span></span>
<span class="line"><span class="hljs-comment"># 使用-h参数关闭操作系统之前将系统中所有的硬件设置为备用模式</span></span>
<span class="line"><span class="x-cmd">poweroff</span> -h</span></code></pre></div>
<h4 id="reboot命令">📚reboot命令</h4>
<p>📔用于重新启动计算机。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n</td>
<td>直接重启系统，不保存当前资料</td>
</tr>
<tr>
<td>-w</td>
<td>模拟重启过程，将过程写入到日志中</td>
</tr>
<tr>
<td>-d</td>
<td>重启系统但不将过程写入到日志中</td>
</tr>
<tr>
<td>-f</td>
<td>强制重启系统</td>
</tr>
<tr>
<td>-i</td>
<td>关闭所有的网络服务，然后重启系统</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:20"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 重启当前系统</span></span>
<span class="line"><span class="x-cmd">reboot</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 模拟重启当前系统：</span></span>
<span class="line"><span class="x-cmd">reboot</span> -w</span></code></pre></div>
<h2 id="常用命令-3">💼常用命令</h2>
<p>Shell可以看作是一个命令解释器，为我们提供了交互式的文本控制的界面。我们可以通过终端控制台来输入命令，由 shell进行解释并最终交给内核执行。</p>
<p>SH(Bourne Shell)是UNIX最初使用的shell，BASH(Bourne Again Shell)Linux默认的，是Bourne Shell的扩展</p>
<h3 id="输出重定向和-追加">&gt; 输出重定向和 &gt;&gt; 追加</h3>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>ls -l &gt; 文件</td>
<td>将列表的内容写入文件a.txt中</td>
</tr>
<tr>
<td>ls -al &gt;&gt; 文件</td>
<td>列表的内容追加到文件的末尾</td>
</tr>
<tr>
<td>cat 文件1 &gt; 文件2</td>
<td>将文件1的内容覆盖到文件2</td>
</tr>
<tr>
<td>echo “内容” &gt;&gt; 文件</td>
<td>将内容写入到文件</td>
</tr>
</tbody></table></div>
<h3 id="帮助常用命令">帮助常用命令</h3>
<h4 id="man命令">📚man命令</h4>
<p>📔man命令来自于英文单词manual的缩写，中文译为帮助手册，其功能是用于查看命令、配置文件及服务的帮助信息。一份完整的帮助信息包含以下信息：</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>名称</th>
<th>意义</th>
</tr>
</thead>
<tbody><tr>
<td>NAME</td>
<td>命令的名称</td>
</tr>
<tr>
<td>SYNOPSIS</td>
<td>参数的大致使用方法</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>介绍说明</td>
</tr>
<tr>
<td>EXAMPLES</td>
<td>演示（附带简单说明）</td>
</tr>
<tr>
<td>OVERVIEW</td>
<td>概述</td>
</tr>
<tr>
<td>DEFAULTS</td>
<td>默认的功能</td>
</tr>
<tr>
<td>OPTIONS</td>
<td>具体的可用选项（带介绍）</td>
</tr>
<tr>
<td>ENVIRONMENT</td>
<td>环境变量</td>
</tr>
<tr>
<td>FILES</td>
<td>用到的文件</td>
</tr>
<tr>
<td>SEE ALSO</td>
<td>相关的资料</td>
</tr>
<tr>
<td>HISTORY</td>
<td>维护历史与联系方式</td>
</tr>
</tbody></table></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>在所有的man帮助手册中搜索</td>
</tr>
<tr>
<td>-d</td>
<td>检查新加入的文件是否有错误</td>
</tr>
<tr>
<td>-f</td>
<td>显示给定关键字的简短描述信息</td>
</tr>
<tr>
<td>-p</td>
<td>指定内容时使用分页程序</td>
</tr>
<tr>
<td>-M</td>
<td>指定man手册搜索的路径</td>
</tr>
<tr>
<td>-w</td>
<td>显示文件所在位置</td>
</tr>
</tbody></table></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>快捷键</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>q</td>
<td>退出</td>
</tr>
<tr>
<td>Enter</td>
<td>按行下翻</td>
</tr>
<tr>
<td>Space</td>
<td>按页下翻</td>
</tr>
<tr>
<td>b</td>
<td>上翻一页</td>
</tr>
<tr>
<td>/字符串</td>
<td>在手册页中查找字符串</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:196"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看指定命令的帮助信息：</span></span>
<span class="line"><span class="x-cmd">man</span> <span class="hljs-built_in">ls</span></span>
<span class="line"></span>
<span class="line">//查看指定配置文件的帮助信息</span>
<span class="line">//如果即有passwd命令，又有/etc/passwd文件，则需要手动指定帮助信息的编号。编号规则：普通命令为1，函数为2，库文件为3，设备为4，配置文件为5，游戏为6，宏文件为7，系统命令为8，内核程序为9，TK指令为10。</span>
<span class="line"><span class="x-cmd">man</span> 5 passwd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 找到某个命令的帮助信息的存储位置</span></span>
<span class="line"><span class="x-cmd">man</span> -w <span class="hljs-built_in">ls</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 找到某个配置文件的帮助信息的存储位置</span></span>
<span class="line"><span class="x-cmd">man</span> -w 5 passwd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># man查看内置命令</span></span>
<span class="line">[pinkpig@Demo100 ~]$ man -f <span class="hljs-built_in">cd</span></span>
<span class="line"><span class="hljs-built_in">cd</span> (1)               - GNU Bourne-Again SHell (GNU 命令解释程序 “Bourne二...</span>
<span class="line"><span class="hljs-built_in">cd</span> (3tcl)            - 改变工作目录</span>
<span class="line"><span class="hljs-built_in">cd</span> (1p)              - change the working directory</span>
<span class="line">[pinkpig@Demo100 ~]$ man 1p <span class="hljs-built_in">cd</span></span>
<span class="line">[pinkpig@Demo100 ~]$ man 3 <span class="hljs-built_in">cd</span></span></code></pre></div>
<p><strong>📍 一部分基础功能的系统命令是直接内嵌在shell 中的，系统加载启动之后会随着shell一起加载，常驻系统内存中。这部分命令被称为“内置(built-in)命令”﹔相应的其它命令被称为“外部命令”。</strong></p>
<h4 id="help命令">📚help命令</h4>
<p>📔用于显示帮助信息，能够输出Shell内部命令的帮助内容，但对于外部命令则无法使用，需要用man或info命令进行查看。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-d</td>
<td>输出每个命令的简短描述</td>
</tr>
<tr>
<td>-s</td>
<td>输出短格式的帮助信息</td>
</tr>
<tr>
<td>-m</td>
<td>以man手册的格式显示帮助信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:37"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 以默认格式显示指定命令的帮助信息</span></span>
<span class="line"><span class="hljs-built_in">help</span> <span class="hljs-built_in">cd</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以短格式显示指定命令的帮助信息</span></span>
<span class="line"><span class="hljs-built_in">help</span> -s <span class="hljs-built_in">cd</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以简短格式显示指定命令的帮助信息</span></span>
<span class="line"><span class="hljs-built_in">help</span> -d <span class="hljs-built_in">cd</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以man命令格式显示指定命令的帮助信息</span></span>
<span class="line"><span class="hljs-built_in">help</span> -m <span class="hljs-built_in">cd</span></span></code></pre></div>
<h4 id="type命令">📚type命令</h4>
<p>📔用来显示指定命令的类型，判断给出的指令是内部指令还是外部指令。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>lias</td>
<td>别名</td>
</tr>
<tr>
<td>keyword</td>
<td>关键字，Shell保留字</td>
</tr>
<tr>
<td>function</td>
<td>函数，Shell函数</td>
</tr>
<tr>
<td>builtin</td>
<td>内建命令，Shell内建命令</td>
</tr>
<tr>
<td>file</td>
<td>文件，磁盘文件，外部命令</td>
</tr>
<tr>
<td>unfound</td>
<td>没有找到</td>
</tr>
</tbody></table></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-t</td>
<td>判断给定参数的命令类型。如果输出 file、alias、builtin 则分别表示 外部命令、命令别名、内置命令。</td>
</tr>
<tr>
<td>-p</td>
<td>如果给出的参数是外部命令，则显示其绝对路径。</td>
</tr>
<tr>
<td>-a</td>
<td>显示所有可能的类型，包括别名、内置命令、函数等。</td>
</tr>
<tr>
<td>-f</td>
<td>只返回shell函数的信息。</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:24"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示指定命令的所有信息</span></span>
<span class="line"><span class="hljs-built_in">type</span> -a <span class="hljs-built_in">ls</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示外部命令的绝对路径</span></span>
<span class="line"><span class="hljs-built_in">type</span> -p <span class="hljs-built_in">ls</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看命令类型</span></span>
<span class="line"><span class="hljs-built_in">type</span> -t <span class="hljs-built_in">ls</span></span></code></pre></div>
<h4 id="clear命令">📚clear命令</h4>
<p>📔clear命令用于清除屏幕。这个命令将会刷新屏幕，本质上只是让终端显示页向后翻了一页，如果向上滚动屏幕还可以看到之前的操作信息。</p>
<h4 id="reset命令">📚reset命令</h4>
<p>📔reset命令其实和 tset 是一同个命令，用于重置终端机的状态，将终端机设置为原始状态。</p>
<h4 id="tset命令">📚tset命令</h4>
<p>📔tset命令的作用是可以进行终端初始化。默认终端类型为“unknown”。</p>
<h3 id="文件常用命令">文件常用命令</h3>
<h4 id="pwd命令">📚pwd命令</h4>
<p>📔pwd命令来自于英文词组”print working directory“的缩写，其功能是用于显示当前工作目录的路径，即显示所在位置的绝对路径。</p>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:22"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看当前工作目录路径</span></span>
<span class="line"><span class="hljs-built_in">pwd</span></span></code></pre></div>
<h4 id="cd命令">📚cd命令</h4>
<p>📔cd命令来自于英文词组”change directory“的缩写，其功能是用于更改当前所处的工作目录，路径可以是绝对路径，也可以是相对路径，若省略不写则会跳转至当前使用者的家目录。<code>~</code>也表示为 home 目录 的意思， <code>.</code>则是表示目前所在的目录，<code>..</code>则表示目前目录位置的上一层目录。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-P</td>
<td>如果切换的目标目录是一个符号链接，则直接切换到符号链接指向的目标目录</td>
</tr>
<tr>
<td>-L</td>
<td>如果切换的目标目录是一个符号链接，则直接切换到符号链接名所在的目录</td>
</tr>
<tr>
<td>–</td>
<td>仅使用”-“选项时，当前目录将被切换到环境变量”OLDPWD”对应值的目录</td>
</tr>
<tr>
<td>~</td>
<td>切换至当前用户目录</td>
</tr>
<tr>
<td>…</td>
<td>切换至当前目录位置的上一级目录</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:39"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 切换当前工作目录至/etc</span></span>
<span class="line"><span class="hljs-built_in">cd</span> /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 切换至当前用户的家目录：</span></span>
<span class="line"><span class="hljs-built_in">cd</span> ~</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 进入到上一级所在目录（家目录→根目录）</span></span>
<span class="line"><span class="hljs-built_in">cd</span> ..</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 返回到上一次所在目录（根目录→家目录）</span></span>
<span class="line"><span class="hljs-built_in">cd</span> -</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 跳到目前目录的上上两层</span></span>
<span class="line"><span class="hljs-built_in">cd</span> ../..</span></code></pre></div>
<h4 id="ls命令">📚ls命令</h4>
<p>📔ls命令来自于英文单词list的缩写，其功能是列举出指定目录下的文件名称及其属性。默认不加参数的情况下，ls命令会列出当前工作目录中的文件信息。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示所有文件及目录 (包括以“.”开头的隐藏文件)</td>
</tr>
<tr>
<td>-l</td>
<td>使用长格式列出文件及目录的详细信息</td>
</tr>
<tr>
<td>-r</td>
<td>将文件以相反次序显示(默认依英文字母次序)</td>
</tr>
<tr>
<td>-t</td>
<td>根据最后的修改时间排序</td>
</tr>
<tr>
<td>-A</td>
<td>同 -a ，但不列出 “.” (当前目录) 及 “…” (父目录)</td>
</tr>
<tr>
<td>-S</td>
<td>根据文件大小排序</td>
</tr>
<tr>
<td>-R</td>
<td>递归列出所有子目录</td>
</tr>
<tr>
<td>-d</td>
<td>查看目录的信息，而不是里面子文件的信息</td>
</tr>
<tr>
<td>-i</td>
<td>输出文件的inode节点信息</td>
</tr>
<tr>
<td>-m</td>
<td>水平列出文件，以逗号间隔</td>
</tr>
<tr>
<td>-X</td>
<td>按文件扩展名排序</td>
</tr>
<tr>
<td>–color</td>
<td>输出信息中带有着色效果</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:58"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 输出当前目录中的文件（默认不含隐藏文件）</span></span>
<span class="line"><span class="hljs-built_in">ls</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出当前目录中的文件（含隐藏文件）</span></span>
<span class="line"><span class="hljs-built_in">ls</span> -a</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出文件的长格式，包含属性详情信息</span></span>
<span class="line"><span class="hljs-built_in">ls</span> -l</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出指定目录中的文件列表</span></span>
<span class="line"><span class="hljs-built_in">ls</span> /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出文件名称及inode属性块号码</span></span>
<span class="line"><span class="hljs-built_in">ls</span> -i</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搭配通配符一起使用，输出指定目录中所有以sd开头的文件名称</span></span>
<span class="line"><span class="hljs-built_in">ls</span> /dev/sd*</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 依据文件大小进行排序，输出指定目录中文件属性详情信息</span></span>
<span class="line"><span class="hljs-built_in">ls</span> -Sl /etc</span></code></pre></div>
<h4 id="mkdir命令">📚mkdir命令</h4>
<p>📔来自于英文词组“make directories”的缩写，其功能是用来创建目录文件。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-p</td>
<td>递归创建多级目录</td>
</tr>
<tr>
<td>-m</td>
<td>建立目录的同时设置目录的权限</td>
</tr>
<tr>
<td>-z</td>
<td>设置安全上下文</td>
</tr>
<tr>
<td>-v</td>
<td>显示目录的创建过程</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:87"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 在当前工作目录中，建立一个目录文件</span></span>
<span class="line"><span class="hljs-built_in">mkdir</span> dir1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在当前工作目录中，创建一个目录文件并设置700权限，不让除所有主以外任何人读、写、执行它</span></span>
<span class="line"><span class="hljs-built_in">mkdir</span> -m 700 dir2</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在当前工作目录中，一次性创建多个目录文件</span></span>
<span class="line"><span class="hljs-built_in">mkdir</span> dir3 dir4 dir5</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在系统根目录中，一次性创建多个有嵌套关系的目录文件</span></span>
<span class="line"><span class="hljs-built_in">mkdir</span> -p /dir1/dir2/dir3/dir4/dir5</span></code></pre></div>
<h4 id="rmdir命令">📚rmdir命令</h4>
<p>📔来自于英文词组“remove directory”的缩写，其功能是用于删除空目录文件。</p>
<p>❗ rmdir命令仅能够删除空内容的目录文件，如需删除非空目录时，则需要使用带有-R参数的rm命令进行操作。而rmdir命令的-p递归删除操作亦不意味着能删除目录中已有的文件，而是要求每个子目录都必须是空的。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-p</td>
<td>用递归的方式删除指定的目录路径中的所有父级目录，非空则报错</td>
</tr>
<tr>
<td>-v</td>
<td>显示命令的详细执行过程</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:36"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 删除指定的空目录</span></span>
<span class="line"><span class="hljs-built_in">rmdir</span> Documents</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 删除指定的空目录，及其内的子空目录</span></span>
<span class="line"><span class="hljs-built_in">rmdir</span> -p Documents</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 删除指定的空目录，并显示删除的过程</span></span>
<span class="line"><span class="hljs-built_in">rmdir</span> -v Documents</span></code></pre></div>
<h4 id="rm命令">📚rm命令</h4>
<p>📔来自于英文单词remove的缩写，其功能是用于删除文件或目录，一次可以删除多个文件，或递归删除目录及其内的所有子文件。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-f</td>
<td>强制删除（不二次询问）</td>
</tr>
<tr>
<td>-i</td>
<td>删除前会询问用户是否操作</td>
</tr>
<tr>
<td>-r/R</td>
<td>递归删除</td>
</tr>
<tr>
<td>-v</td>
<td>显示指令的详细执行过程</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:52"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 删除某个文件，默认会进行二次确认，敲击y进行确认</span></span>
<span class="line"><span class="hljs-built_in">rm</span> anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 删除某个文件，强制操作不需要二次确认：</span></span>
<span class="line"><span class="hljs-built_in">rm</span> -f initial-setup-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 删除某个目录及其内的子文件或子目录，一并都强制删除</span></span>
<span class="line"><span class="hljs-built_in">rm</span> -rf Documents</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 强制删除当前工作目录内的所有以.txt为后缀的文件</span></span>
<span class="line"><span class="hljs-built_in">rm</span> -f *.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 强制清空服务器系统内的所有文件</span></span>
<span class="line"><span class="hljs-built_in">rm</span> -rf /*</span></code></pre></div>
<h4 id="cp命令">📚cp命令</h4>
<p>📔来自于英文单词copy的缩写，用于将一个或多个文件或目录复制到指定位置。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-f</td>
<td>若目标文件已存在，则会直接覆盖原文件</td>
</tr>
<tr>
<td>-i</td>
<td>若目标文件已存在，则会询问是否覆盖</td>
</tr>
<tr>
<td>-p</td>
<td>保留源文件或目录的所有属性</td>
</tr>
<tr>
<td>-r</td>
<td>递归复制文件和目录</td>
</tr>
<tr>
<td>-d</td>
<td>当复制符号连接时，把目标文件或目录也建立为符号连接，并指向与源文件或目录连接的原始文件或目录</td>
</tr>
<tr>
<td>-l</td>
<td>对源文件建立硬连接，而非复制文件</td>
</tr>
<tr>
<td>-s</td>
<td>对源文件建立符号连接，而非复制文件</td>
</tr>
<tr>
<td>-b</td>
<td>覆盖已存在的文件目标前将目标文件备份</td>
</tr>
<tr>
<td>-v</td>
<td>详细显示cp命令执行的操作过程</td>
</tr>
<tr>
<td>-a</td>
<td>等价于“pdr”选项</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:70"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 在当前工作目录中，将某个文件复制一份，并定义新文件名称</span></span>
<span class="line"><span class="hljs-built_in">cp</span> anaconda-ks.cfg kickstart.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在当前工作目录中，将某个目录复制一份，并定义新目录名称</span></span>
<span class="line"><span class="hljs-built_in">cp</span> -r Documents Doc</span>
<span class="line"></span>
<span class="line">复制某个文件时，保留其原始权限及用户归属信息</span>
<span class="line"><span class="hljs-built_in">cp</span> -a kickstart.cfg ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将某个文件复制到/etc目录中，并覆盖已有文件，不进行询问</span></span>
<span class="line"><span class="hljs-built_in">cp</span> -f ks.cfg /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将多个文件一同复制到/etc目录中，如已有目标文件名称则默认询问是否覆盖</span></span>
<span class="line"><span class="hljs-built_in">cp</span> anaconda-ks.cfg ks.cfg /etc</span></code></pre></div>
<h4 id="mv命令">📚mv命令</h4>
<p>📔自于英文单词move的缩写，用于对文件进行剪切和重命名。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-i</td>
<td>若存在同名文件，则向用户询问是否覆盖</td>
</tr>
<tr>
<td>-f</td>
<td>覆盖已有文件时，不进行任何提示</td>
</tr>
<tr>
<td>-b</td>
<td>当文件存在时，覆盖前为其创建一个备份</td>
</tr>
<tr>
<td>-n:</td>
<td>不要覆盖任何已存在的文件或目录</td>
</tr>
<tr>
<td>-u</td>
<td>当源文件比目标文件新，或者目标文件不存在时，才执行移动此操作</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:73"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 在当前工作目录中，对某个文件进行剪切后粘贴（重命名）操作</span></span>
<span class="line"><span class="hljs-built_in">mv</span> anaconda-ks.cfg ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将某个文件移动到/etc目录中，保留文件原始名称</span></span>
<span class="line"><span class="hljs-built_in">mv</span> ks.cfg /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将某个目录移动到/etc目录中，并定义新的目录名称</span></span>
<span class="line"><span class="hljs-built_in">mv</span> Documents /etc/docs</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将/home目录中所有的文件都移动到当前工作目录中，遇到已存在文件则直接覆盖</span></span>
<span class="line"><span class="hljs-built_in">mv</span> -f /home/* .</span></code></pre></div>
<h4 id="cat命令">📚cat命令</h4>
<p>📔自于英文单词concatenate的缩写，其功能是用于查看文件内容。cat命令适合查看内容较少的、纯文本的文件。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n</td>
<td>显示行数（空行也编号）</td>
</tr>
<tr>
<td>-s</td>
<td>显示行数（多个空行算一个编号）</td>
</tr>
<tr>
<td>-b</td>
<td>显示行数（空行不编号）</td>
</tr>
<tr>
<td>-E</td>
<td>每行结束处显示$符号</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:18;--cols:136"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看某个文件的内容</span></span>
<span class="line"><span class="hljs-built_in">cat</span> anaconda-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看某个文件的内容，并显示行号</span></span>
<span class="line"><span class="hljs-built_in">cat</span> -n anaconda-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搭配空设备文件和输出重定向操作符，将某个文件内容清空</span></span>
<span class="line"><span class="hljs-built_in">cat</span> /dev/null &gt; anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 持续写入文件内容，直到碰到EOF符后才会结束并保存</span></span>
<span class="line"><span class="hljs-built_in">cat</span> &gt; anaconda-ks.cfg &lt;&lt; <span class="hljs-string">EOF</span></span>
<span class="line"><span class="hljs-string"></span></span>
<span class="line"><span class="hljs-string"># 搭配输出重定向操作符，将光盘设备制作成镜像文件</span></span>
<span class="line"><span class="hljs-string">[root@Demo ~]# cat /dev/cdrom &gt; rhel.iso</span></span>
<span class="line"><span class="hljs-string">[root@Demo ~]# ls rhel.iso  -lh</span></span>
<span class="line"><span class="hljs-string">-rw-r--r--. 1 root root 6.7G May  2 00:43 rhel.iso</span></span>
<span class="line"><span class="hljs-string">[root@Demo ~]# file rhel.iso </span></span>
<span class="line"><span class="hljs-string">rhel.iso: DOS/MBR boot sector; partition 2 : ID=0xef, start-CHS (0x3ff,254,63), end-CHS (0x3ff,254,63), startsector 23128, 19888 sectors</span></span></code></pre></div>
<h4 id="more命令">📚more命令</h4>
<p>📔用于分页显示文本文件内容。如果文本文件中的内容较多较长，使用cat命令读取后则很难看清，这时使用more命令进行分页查看就更加合适，可以把文本内容一页一页的显示在终端界面上，用户每按一次回车即向下一行，每按一次空格即向下一页，直至看完为止。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-num</td>
<td>指定每屏显示的行数</td>
</tr>
<tr>
<td>-l</td>
<td>more在通常情况下把 ^L 当作特殊字符, 遇到这个字符就会暂停,-l选项可以阻止这种特性</td>
</tr>
<tr>
<td>-f</td>
<td>计算实际的行数，而非自动换行的行数</td>
</tr>
<tr>
<td>-p</td>
<td>先清除屏幕再显示文本文件的剩余内容</td>
</tr>
<tr>
<td>-c</td>
<td>与-p相似，不滚屏，先显示内容再清除旧内容</td>
</tr>
<tr>
<td>-s</td>
<td>多个空行压缩成一行显示</td>
</tr>
<tr>
<td>-u</td>
<td>禁止下划线</td>
</tr>
<tr>
<td>+/pattern</td>
<td>在每个文档显示前搜寻该字(pattern)，然后从该字串之后开始显示</td>
</tr>
<tr>
<td>+num</td>
<td>从第 num 行开始显示</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:78"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 分页显示指定的文本文件内容</span></span>
<span class="line"><span class="x-cmd">more</span> anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 先进行清屏操作，随后以每次10行内容的格式显示指定的文本文件内容</span></span>
<span class="line"><span class="x-cmd">more</span> -c -10 anaconda-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 分页显示指定的文本文件内容，遇到连续两行以上空白行的情况，则以一行空白行显示</span></span>
<span class="line"><span class="x-cmd">more</span> -s anaconda-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 从第10行开始，分页显示指定的文本文件内容</span></span>
<span class="line"><span class="x-cmd">more</span> +10 anaconda-ks.cfg </span></code></pre></div>
<h4 id="less命令">📚less命令</h4>
<p>📔less 指令用来分屏查看文件内容，它的功能与more指令类似，但是比more指令更加强大，支持各种显示终端。less指令在显示文件内容时，并不是一次将整个文件加载之后才显示，而是根据显示需要加载内容，对于显示大型文件具有较高的效率。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-b</td>
<td>设置缓冲区的大小</td>
</tr>
<tr>
<td>-e</td>
<td>当文件显示结束后自动退出</td>
</tr>
<tr>
<td>-f</td>
<td>强制打开文件</td>
</tr>
<tr>
<td>-g</td>
<td>只标志最后搜索的关键词</td>
</tr>
<tr>
<td>-i</td>
<td>忽略搜索时的大小写</td>
</tr>
<tr>
<td>-m</td>
<td>显示阅读进度百分比</td>
</tr>
<tr>
<td>-N</td>
<td>显示每行的行号</td>
</tr>
<tr>
<td>-o</td>
<td>将输出的内容在指定文件中保存起来</td>
</tr>
<tr>
<td>-Q</td>
<td>不使用警告音</td>
</tr>
<tr>
<td>-s</td>
<td>显示连续空行为一行</td>
</tr>
<tr>
<td>-S</td>
<td>在单行显示较长的内容，而不换行显示</td>
</tr>
<tr>
<td>-x</td>
<td>将TAB字符显示为指定个数的空格字符</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 分页查看指定的文件内容</span></span>
<span class="line"><span class="x-cmd">less</span> anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 分页查看指定的文件内容及行号</span></span>
<span class="line"><span class="x-cmd">less</span> -N anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 分页显示指定命令的输出结果</span></span>
<span class="line"><span class="hljs-built_in">history</span> | less</span></code></pre></div>
<h4 id="echo命令">📚echo命令</h4>
<p>📔输出字符串或提取后的变量值</p>
<p>🔖echo是用于在终端设备上输出指定字符串或变量提取后值的命令。能够给用户一些简单的提醒信息，也可以将输出的指定字符串内容同管道符一起传递给后续命令作为标准输入信息再来进行二次处理，又或者同输出重定向符一起操作，将信息直接写入到文件中。</p>
<p>🔖如需提取变量值，需在变量名称前加入$符号做提取，变量名称一般均为大写形式。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n</td>
<td>不输出结尾的换行符</td>
</tr>
<tr>
<td>-e “a”</td>
<td>发出警告音</td>
</tr>
<tr>
<td>-e “b”</td>
<td>删除前面的一个字符</td>
</tr>
<tr>
<td>-e “c”</td>
<td>结尾不加换行符</td>
</tr>
<tr>
<td>-e “f”</td>
<td>换行，光标扔停留在原来的坐标位置</td>
</tr>
<tr>
<td>-e “n”</td>
<td>换行，光标移至行首</td>
</tr>
<tr>
<td>-e “r”</td>
<td>光标移至行首，但不换行</td>
</tr>
<tr>
<td>-E</td>
<td>禁止反斜杠转移，与-e参数功能相反</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:55"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 输出指定字符串到终端设备界面</span></span>
<span class="line"><span class="hljs-built_in">echo</span> LinuxCool</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出某个变量值内容</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$PATH</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搭配转义符一起使用，输出纯字符串内容</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$PATH</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搭配输出重定向符一起使用，将字符串内容直接写入文件中</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Hello World&quot;</span> &gt; Document</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搭配反引号执行命令，并将执行结果输出</span></span>
<span class="line"><span class="hljs-built_in">echo</span> `<span class="hljs-built_in">uptime</span>`</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出带有换行符的内容</span></span>
<span class="line"><span class="hljs-built_in">echo</span> -e <span class="hljs-string">&quot;FirstnSecondnThird&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 指定删除字符串中某些字符，随后将内容输出。结果：12456</span></span>
<span class="line"><span class="hljs-built_in">echo</span> -e <span class="hljs-string">&quot;123b456&quot;</span> </span></code></pre></div>
<h4 id="head命令">📚head命令</h4>
<p>📔显示文件开头的内容，默认为前10行。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n &lt;数字&gt;</td>
<td>定义显示行数</td>
</tr>
<tr>
<td>-c &lt;数字&gt;</td>
<td>指定显示头部内容的字符数</td>
</tr>
<tr>
<td>-v</td>
<td>总是显示文件名的头信息</td>
</tr>
<tr>
<td>-q</td>
<td>不显示文件名的头信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:27"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 指定显示文件的前5行内容</span></span>
<span class="line"><span class="hljs-built_in">head</span> -n 5 anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 指定显示文件的前20个字符</span></span>
<span class="line"><span class="hljs-built_in">head</span> -c 20 anaconda-ks.cfg </span></code></pre></div>
<h4 id="tail命令">📚tail命令</h4>
<p>📔用于查看文件尾部内容，默认为后10行。如果指定了多个文件，则会在显示的每个文件内容前面加上文件名来加以区分。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-c</td>
<td>输出文件尾部的N（N为整数）个字节内容</td>
</tr>
<tr>
<td>-f</td>
<td>持续显示文件最新追加的内容</td>
</tr>
<tr>
<td>-n</td>
<td>输出文件的尾部N（N位数字）行内容</td>
</tr>
<tr>
<td>–retry</td>
<td>即是在tail命令启动时，文件不可访问或者文件稍后变得不可访问，都始终尝试打开文件。</td>
</tr>
<tr>
<td>–pid=&lt;进程号&gt;</td>
<td>与“-f”选项连用，当指定的进程号的进程终止后，自动退出tail命令</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 指定显示某个文件尾部的后5行内容</span></span>
<span class="line"><span class="hljs-built_in">tail</span> -n 5 initial-setup-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 指定显示某个文件尾部的后15个字节</span></span>
<span class="line"><span class="hljs-built_in">tail</span> -c 15 initial-setup-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 持续刷新显示某个文件尾部的后10行内容</span></span>
<span class="line"><span class="hljs-built_in">tail</span> -f /var/log/messages</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示文件 notes.log 的内容，从第 20 行至文件末尾</span></span>
<span class="line"><span class="hljs-built_in">tail</span> -n +20 notes.log</span></code></pre></div>
<h4 id="ln命令">📚ln命令</h4>
<p>📔自于英文单词link的缩写，中文译为“链接”，其功能是用于为某个文件在另外一个位置建立同步的链接。</p>
<p>🔖Linux系统中的链接文件有两种形式，一种是硬链接（hard link），另一种是软链接（symbolic link）。</p>
<p>🔖软连接相当于Windows系统中的快捷方式文件，原始文件被移动或删除后，软连接文件也将无法使用，而硬链接则是通过将文件的inode属性块进行了复制 ，因此把原始文件移动或删除后，硬链接文件依然可以使用。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-b</td>
<td>为每个已存在的目标文件创建备份文件</td>
</tr>
<tr>
<td>-d</td>
<td>此选项允许“root”用户建立目录的硬链接</td>
</tr>
<tr>
<td>-f</td>
<td>强制创建链接，即使目标文件已经存在</td>
</tr>
<tr>
<td>-n</td>
<td>把指向目录的符号链接视为一个普通文件</td>
</tr>
<tr>
<td>-i</td>
<td>交互模式，若目标文件已经存在，则提示用户确认进行覆盖</td>
</tr>
<tr>
<td>-s</td>
<td>对源文件建立符号链接，而非硬链接</td>
</tr>
<tr>
<td>-v</td>
<td>详细信息模式，输出指令的详细执行过程</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 为指定的源文件创建快捷方式（默认为硬链接形式）</span></span>
<span class="line"><span class="hljs-built_in">ln</span> anaconda-ks.cfg ana.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 为指定的源文件创建快捷方式（设定为软连接形式）</span></span>
<span class="line"><span class="hljs-built_in">ln</span> -s initial-setup-ks.cfg ini.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在指定的源文件创建快捷方式，并输出制作的过程信息</span></span>
<span class="line"><span class="hljs-built_in">ln</span> -v anaconda-ks.cfg ana.cfg</span></code></pre></div>
<h4 id="history命令">📚history命令</h4>
<p>📔显示与管理历史命令记录。Linux系统默认会记录用户所执行过的所有命令，可以使用history命令查阅它们，也可以对其记录进行修改和删除操作。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>写入命令记录</td>
</tr>
<tr>
<td>-c</td>
<td>清空命令记录</td>
</tr>
<tr>
<td>-d</td>
<td>删除指定序号的命令记录</td>
</tr>
<tr>
<td>-n</td>
<td>读取命令记录</td>
</tr>
<tr>
<td>-r</td>
<td>读取命令记录到缓冲区</td>
</tr>
<tr>
<td>-s</td>
<td>将指定的命令添加到缓冲区</td>
</tr>
<tr>
<td>-w</td>
<td>将缓冲区信息写入到历史文件</td>
</tr>
</tbody></table></div>
<h3 id="时间日期常用命令">时间日期常用命令</h3>
<h4 id="date命令">📚date命令</h4>
<p>📔用来显示或设定系统的日期与时间。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-d</td>
<td>通过字符串显示时间格式，字符串不能是’now’</td>
</tr>
<tr>
<td>-s</td>
<td>根据字符串设置系统时间</td>
</tr>
<tr>
<td>-u</td>
<td>显示目前的格林威治时间</td>
</tr>
<tr>
<td>-r</td>
<td>显示文件的上次修改时间</td>
</tr>
</tbody></table></div>
<p>📍若是不以加号作为开头，则表示要设定时间，而时间格式为 <code>MMDDhhmm[[CC]YY][.ss]</code>，其中<code>MM</code>为月份，<code>DD</code> 为日，<code>hh</code>为小时，<code>mm</code> 为分钟，<code>CC</code>为年份前两位数字，<code>YY</code> 为年份后两位数字，<code>ss</code>为秒数。</p>
<p>📍当您不希望出现无意义的 0 时(比如说 1999/03/07)，则可以在标记中插入 <code>-</code> 符号，比如说 <code>date &#39;+%-H:%-M:%-S&#39;</code> 会把时分秒中无意义的 0 给去掉，像是原本的 08:09:04 会变为 8:9:4。另外，只有取得权限者(比如说 root)才能设定系统时间。</p>
<p>📍当您以 root 身分更改了系统时间之后，请记得以 <code>clock -w</code>来将系统时间写入 CMOS 中，这样下次重新开机时系统时间才会持续保持最新的正确值。</p>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:52"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 以默认格式输出系统当前的日期与时间信息</span></span>
<span class="line"><span class="hljs-built_in">date</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 按照“年-月-日”的指定格式输出系统当前的日期信息</span></span>
<span class="line"><span class="hljs-built_in">date</span> <span class="hljs-string">&quot;+%Y-%m-%d&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 按照“小时:分钟:秒”的指定格式输出系统当前的时间信息</span></span>
<span class="line"><span class="hljs-built_in">date</span> <span class="hljs-string">&quot;%H:%M:%S&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出昨天日期</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;1 day ago&quot;</span> +<span class="hljs-string">&quot;%Y-%m-%d&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出 2 秒后的时间</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;2 second&quot;</span> +<span class="hljs-string">&quot;%Y-%m-%d %H:%M.%S&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 时间格式转换</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;2009-12-12&quot;</span> +<span class="hljs-string">&quot;%Y/%m/%d %H:%M.%S&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 按自己的格式输出</span></span>
<span class="line"><span class="hljs-built_in">date</span> <span class="hljs-string">&#x27;+usr_time: $1:%M %P -hey&#x27;</span></span></code></pre></div>
<p>📃时间加减操作</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">date</span> +%Y%m%d                   <span class="hljs-comment"># 显示年月日</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;+1 day&quot;</span> +%Y%m%d       <span class="hljs-comment"># 显示后一天的日期</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;-1 day&quot;</span> +%Y%m%d       <span class="hljs-comment"># 显示前一天的日期</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;-1 month&quot;</span> +%Y%m%d     <span class="hljs-comment"># 显示上一月的日期</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;+1 month&quot;</span> +%Y%m%d     <span class="hljs-comment"># 显示下一月的日期</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;-1 year&quot;</span> +%Y%m%d      <span class="hljs-comment"># 显示前一年的日期</span></span>
<span class="line"><span class="hljs-built_in">date</span> -d <span class="hljs-string">&quot;+1 year&quot;</span> +%Y%m%d      <span class="hljs-comment"># 显示下一年的日期</span></span></code></pre></div>
<p>📃设定时间</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:82"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">date</span> -s                         <span class="hljs-comment"># 设置当前时间，只有root权限才能设置，其他只能查看</span></span>
<span class="line"><span class="hljs-built_in">date</span> -s 20120523                <span class="hljs-comment"># 设置成20120523，这样会把具体时间设置成00:00:00</span></span>
<span class="line"><span class="hljs-built_in">date</span> -s 01:01:01                <span class="hljs-comment"># 设置具体时间，不会对日期做更改</span></span>
<span class="line"><span class="hljs-built_in">date</span> -s <span class="hljs-string">&quot;01:01:01 2012-05-23&quot;</span>   <span class="hljs-comment"># 这样可以设置全部时间</span></span>
<span class="line"><span class="hljs-built_in">date</span> -s <span class="hljs-string">&quot;01:01:01 20120523&quot;</span>     <span class="hljs-comment"># 这样可以设置全部时间</span></span>
<span class="line"><span class="hljs-built_in">date</span> -s <span class="hljs-string">&quot;2012-05-23 01:01:01&quot;</span>   <span class="hljs-comment"># 这样可以设置全部时间</span></span>
<span class="line"><span class="hljs-built_in">date</span> -s <span class="hljs-string">&quot;20120523 01:01:01&quot;</span>     <span class="hljs-comment"># 这样可以设置全部时间</span></span></code></pre></div>
<h4 id="cal命令">📚cal命令</h4>
<p>📔显示日历。</p>
<p>🔖cal命令的名字来自英语单词“Calendar”。改命令用来显示当前日历，或者指定日期的公历（公历是现在国际通用的历法，又称格列历，通称阳历。）如只有一个参数，则表示年份(1-9999)，如有两个参数，则表示月份和年份 。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-l</td>
<td>单月分输出日历</td>
</tr>
<tr>
<td>-3</td>
<td>显示最近三个月的日历</td>
</tr>
<tr>
<td>-s</td>
<td>将星期天作为月的第一天</td>
</tr>
<tr>
<td>-m</td>
<td>将星期一作为月的第一天</td>
</tr>
<tr>
<td>-j</td>
<td>显示在当年中的第几天（儒略日）</td>
</tr>
<tr>
<td>-y</td>
<td>显示当年的日历</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示当前月份的日历</span></span>
<span class="line"><span class="x-cmd">cal</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示近期三个月的日历(当前月，上一个月和下一个月)</span></span>
<span class="line"><span class="x-cmd">cal</span> -3  </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示指定年月的日历，如显示2020年2月的日历</span></span>
<span class="line"><span class="x-cmd">cal</span> 2 2020</span></code></pre></div>
<h3 id="用户管理常用命令">用户管理常用命令</h3>
<h4 id="useradd命令">📚useradd命令</h4>
<p>📔创建并设置用户信息。</p>
<p>🔖useradd命令来自于英文词组“User add”的全拼，其功能是用于创建并设置用户信息。使用useradd命令可以自动的完成创建用户的信息、基本组、家目录等工作，并在创建的过程中对用户初始信息进行定制。</p>
<p>🔖已创建的用户则需使用chmod命令修改账户信息，passwd命令修改密码信息。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-D</td>
<td>改变新建用户的预设值</td>
</tr>
<tr>
<td>-c</td>
<td>添加备注文字</td>
</tr>
<tr>
<td>-d</td>
<td>新用户每次登陆时所使用的家目录</td>
</tr>
<tr>
<td>-e</td>
<td>用户终止日期，日期的格式为YYYY-MM-DD</td>
</tr>
<tr>
<td>-f</td>
<td>用户过期几日后永久停权。当值为0时用户立即被停权，而值为-1时则关闭此功能，预设值为-1</td>
</tr>
<tr>
<td>-g</td>
<td>指定用户对应的用户组</td>
</tr>
<tr>
<td>-G</td>
<td>定义此用户为多个不同组的成员</td>
</tr>
<tr>
<td>-m</td>
<td>用户目录不存在时则自动创建</td>
</tr>
<tr>
<td>-M</td>
<td>不建立用户家目录，优先于/etc/login.defs文件设定</td>
</tr>
<tr>
<td>-n</td>
<td>取消建立以用户名称为名的群组</td>
</tr>
<tr>
<td>-r</td>
<td>建立系统帐号</td>
</tr>
<tr>
<td>-u</td>
<td>指定用户id</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:17;--cols:52"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 创建指定的用户信息</span></span>
<span class="line"><span class="x-cmd">useradd</span> user1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建指定的用户信息，但不创建家目录，亦不让登录系统</span></span>
<span class="line"><span class="x-cmd">useradd</span> -M -s /sbin/nologin user2</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建指定的用户信息，并自定义UID值</span></span>
<span class="line"><span class="x-cmd">useradd</span> -u 6688 user3</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建指定的用户信息，并追加指定组为该账户的扩展组</span></span>
<span class="line"><span class="x-cmd">useradd</span> -G root user4</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建指定的用户信息，并指定过期时间</span></span>
<span class="line"><span class="x-cmd">useradd</span> -e <span class="hljs-string">&quot;2023/01/01&quot;</span> user5</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 为新添加的用户指定home目录</span></span>
<span class="line"><span class="x-cmd">useradd</span> -d /home/myd user6</span></code></pre></div>
<h4 id="passwd命令">📚passwd命令</h4>
<p>📔 修改用户的密码值。</p>
<p>🔖passwd命令来自于英文单词password的缩写。同时也可以对用户进行锁定等操作，但需要管理员身份才可以执行。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-d</td>
<td>删除已有密码</td>
</tr>
<tr>
<td>-l</td>
<td>锁定用户的密码值，不允许修改</td>
</tr>
<tr>
<td>-u</td>
<td>解锁用户的密码值，允许修改</td>
</tr>
<tr>
<td>-e</td>
<td>下次登陆强制修改密码</td>
</tr>
<tr>
<td>-k</td>
<td>用户在期满后能仍能使用</td>
</tr>
<tr>
<td>-S</td>
<td>查询密码状态</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:42"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 修改当前登录用户的密码值</span></span>
<span class="line"><span class="x-cmd">passwd</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 修改指定用户的密码值</span></span>
<span class="line"><span class="x-cmd">passwd</span> linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 锁定指定用户的密码值，不允许其进行修改</span></span>
<span class="line"><span class="x-cmd">passwd</span> -l linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 解锁指定用户的密码值，允许其进行修改</span></span>
<span class="line"><span class="x-cmd">passwd</span> -u linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 强制指定的用户在下次登陆时必须重置其密码</span></span>
<span class="line"><span class="x-cmd">passwd</span> -e linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 删除指定用户的密码值</span></span>
<span class="line"><span class="x-cmd">passwd</span> -d linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看指定用户的密码状态</span></span>
<span class="line"><span class="x-cmd">passwd</span> -S linuxprobe</span></code></pre></div>
<h4 id="su命令">📚su命令</h4>
<p>📔 切换用户身份。</p>
<p>🔖su命令来自于英文单词“switch user”的缩写。管理员切换至任意用户身份而无需密码验证，而普通用户切换至任意用户身份均需密码验证。另外添加单个减号（-）参数为完全的身份变更，不保留任何之前用户的环境变量信息。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>单个减号（-）</td>
<td>完全身份变更</td>
</tr>
<tr>
<td>-c</td>
<td>执行完指定的指令后，即恢复原来的身份</td>
</tr>
<tr>
<td>-f</td>
<td>适用于csh与tsch，使shell不用去读取启动文件</td>
</tr>
<tr>
<td>-l</td>
<td>改变身份时，也同时变更工作目录</td>
</tr>
<tr>
<td>-m</td>
<td>变更身份时，不要变更环境变量</td>
</tr>
<tr>
<td>-s</td>
<td>指定要执行的shell</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:24"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 变更至指定用户身份</span></span>
<span class="line"><span class="x-cmd">su</span> user1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 完全变更至指定用户身份</span></span>
<span class="line"><span class="x-cmd">su</span> - user1</span></code></pre></div>
<h4 id="id命令">📚id命令</h4>
<p>📔 显示用户与用户组信息。</p>
<p>🔖UID是指用户身份的唯一识别号码，相当于人类社会的身份证号码，而GID则指用户组的唯一识别号码，用户仅有一个基本组，但可以有多个扩展组。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-g</td>
<td>显示用户所属群组的ID</td>
</tr>
<tr>
<td>-G</td>
<td>显示用户扩展群组的ID</td>
</tr>
<tr>
<td>-n</td>
<td>显示用户所属群组或扩展群组的名称</td>
</tr>
<tr>
<td>-r</td>
<td>显示实际ID</td>
</tr>
<tr>
<td>-u</td>
<td>显示用户ID</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:27"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示当前用户的身份信息</span></span>
<span class="line"><span class="hljs-built_in">id</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示当前用户的所属群组GID</span></span>
<span class="line"><span class="hljs-built_in">id</span> -g</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示当前用户的身份码UID</span></span>
<span class="line"><span class="hljs-built_in">id</span> -u</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查询当前用户的身份信息</span></span>
<span class="line"><span class="hljs-built_in">id</span> linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看创建了哪些用户</span></span>
<span class="line"><span class="hljs-built_in">cat</span> /etc/passwd</span></code></pre></div>
<h4 id="who命令">📚who命令</h4>
<p>📔 用于显示当前登录用户信息，包含登录的用户名、终端、日期时间、进程等信息，帮助运维人员了解当前系统的登入用户情况。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>全面信息</td>
</tr>
<tr>
<td>-b</td>
<td>系统最近启动时间</td>
</tr>
<tr>
<td>-d</td>
<td>死掉的进程</td>
</tr>
<tr>
<td>-l</td>
<td>系统登录进程</td>
</tr>
<tr>
<td>-H</td>
<td>带有列标题打印用户名，终端和时间</td>
</tr>
<tr>
<td>-t</td>
<td>系统上次锁定时间</td>
</tr>
<tr>
<td>-u</td>
<td>已登录用户列表</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:34"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看当前登录用户信息</span></span>
<span class="line"><span class="hljs-built_in">who</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看当前登录用户信息，并加上标题</span></span>
<span class="line"><span class="hljs-built_in">who</span> -H</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看当前全部的登录全部用户信息</span></span>
<span class="line"><span class="hljs-built_in">who</span> -H -a</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看系统的最近启动时间</span></span>
<span class="line"><span class="hljs-built_in">who</span> -b</span></code></pre></div>
<h4 id="userdel命令">📚userdel命令</h4>
<p>📔删除用户账户。</p>
<p>🔖userdel命令来自于英文词组“user delete”的缩写。Linux系统中一切都是文件，用户信息被保存到了/etc/passwd、/etc/shadow以及/etc/group文件中，因此使用userdel命令实际就是帮助我们删除了指定用户在上述三个文件中的对应信息。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-f</td>
<td>强制删除用户账号</td>
</tr>
<tr>
<td>-r</td>
<td>删除用户主目录及其中的任何文件</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 删除指定的用户账户信息</span></span>
<span class="line"><span class="x-cmd">userdel</span> linuxcool</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 删除指定的用户账户信息及家目录</span></span>
<span class="line"><span class="x-cmd">userdel</span> -r linuxcool</span></code></pre></div>
<h4 id="sudo命令">📚sudo命令</h4>
<p>📔授权普通用户执行管理员命令。</p>
<p>🔖sudo命令来自于英文词组“super user do”的缩写，中文译为“超级用户才能干的事”。使用sudo服务可以授权某个指定的用户去执行某些指定的命令，在满足工作需求的前提下尽可能少的放权，保证服务器的安全。</p>
<p>🔖配置sudo服务可以直接编辑配置文件/etc/sudoers，亦可以执行visudo命令进行设置，一切妥当后普通用户便能够使用sudo命令进行操作了。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-v</td>
<td>本次需要验证当前用户的密码</td>
</tr>
<tr>
<td>-k</td>
<td>下次强制验证当前用户的密码</td>
</tr>
<tr>
<td>-b</td>
<td>将要执行的指令放在后台执行</td>
</tr>
<tr>
<td>-p</td>
<td>更改需要密码验证时的提示语</td>
</tr>
<tr>
<td>-s</td>
<td>指定默认调用的SHELL解释器</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:56"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看当前用户有哪些被sudo服务授权的命令</span></span>
<span class="line"><span class="hljs-built_in">sudo</span> -l</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用某个被sudo服务允许的用户身份来执行管理员的重启命令</span></span>
<span class="line"><span class="hljs-built_in">sudo</span> -u linuxprobe <span class="hljs-string">&quot;reboot&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用当前用户身份，基于sudo命令来执行管理员的重启命令</span></span>
<span class="line"><span class="hljs-built_in">sudo</span> reboot</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以root权限执行上一条命令</span></span>
<span class="line">$ <span class="hljs-built_in">sudo</span> !!</span></code></pre></div>
<h4 id="usermod命令">📚usermod命令</h4>
<p>📔 修改用户账号信息。</p>
<p>🔖usermod命令来自于英文词组“user modify”的缩写，其功能是用于修改用户账号中的各项参数。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-c&lt;备注&gt;</td>
<td>修改用户账号的备注文字</td>
</tr>
<tr>
<td>-d&lt;登入目录&gt;</td>
<td>修改用户登入时的家目录</td>
</tr>
<tr>
<td>-e&lt;有效期限&gt;</td>
<td>修改账号的有效期限</td>
</tr>
<tr>
<td>-f&lt;缓冲天数&gt;</td>
<td>修改在密码过期后多少天即关闭该账号</td>
</tr>
<tr>
<td>-g&lt;群组&gt;</td>
<td>修改用户所属的群组</td>
</tr>
<tr>
<td>-G&lt;群组&gt;</td>
<td>修改用户所属的附加群组</td>
</tr>
<tr>
<td>-l&lt;账号名称&gt;</td>
<td>修改用户账号名称</td>
</tr>
<tr>
<td>-L</td>
<td>锁定用户密码，使密码无效</td>
</tr>
<tr>
<td>-s</td>
<td>修改用户登入后所使用的shell</td>
</tr>
<tr>
<td>-u</td>
<td>修改用户ID</td>
</tr>
<tr>
<td>-U</td>
<td>解除密码锁定</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">修改指定用户的家目录路径</span>
<span class="line"><span class="x-cmd">usermod</span> -d /home user1</span>
<span class="line"></span>
<span class="line">修改指定用户的UID号码</span>
<span class="line"><span class="x-cmd">usermod</span> -u 6688 user1</span>
<span class="line"></span>
<span class="line">修改指定用户的名称为user2</span>
<span class="line"><span class="x-cmd">usermod</span> -l user2 user1</span>
<span class="line"></span>
<span class="line">锁定指定用户的账户，临时不允许登录系统</span>
<span class="line"><span class="x-cmd">usermod</span> -L user1</span>
<span class="line"></span>
<span class="line">解锁指定用户的账户，再次允许登录系统</span>
<span class="line"><span class="x-cmd">usermod</span> -U user1</span></code></pre></div>
<h3 id="用户组管理常用命令">用户组管理常用命令</h3>
<h4 id="groupadd命令">📚groupadd命令</h4>
<p>📔创建新的用户组。</p>
<p>🔖groupadd命令来自于英文词组“group add”。每个用户在创建时都有一个与其同名的基本组，后期可以使用groupadd命令创建出新的用户组信息，让多个用户加入到指定的扩展组中。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-g</td>
<td>指定新建工作组的id</td>
</tr>
<tr>
<td>-r</td>
<td>创建系统工作组</td>
</tr>
<tr>
<td>-K</td>
<td>覆盖配置文件“/ect/login.defs”</td>
</tr>
<tr>
<td>-o</td>
<td>允许添加组ID号不唯一的工作组</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 创建一个新的用户组</span></span>
<span class="line"><span class="x-cmd">groupadd</span> user1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建一个新的用户组，并指定GID号码</span></span>
<span class="line"><span class="x-cmd">groupadd</span> -g 6688 user1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建一个新的用户组，设定为系统工作组</span></span>
<span class="line"><span class="x-cmd">groupadd</span> -r user1</span></code></pre></div>
<h4 id="groupdel命令">📚groupdel命令</h4>
<p>📔删除用户组。</p>
<p>🔖groupdel命令用于删除指定的工作组，本命令要修改的系统文件包括/ect/group和/ect/gshadow。</p>
<p>🔖userdel修改系统账户文件，删除与 GROUP 相关的所有项目。给出的组名必须存在。若该群组中仍包括某些用户，则必须先删除这些用户后，方能删除群组。</p>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:33"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 使用groupdel命令删除user1工作组</span></span>
<span class="line"><span class="x-cmd">groupdel</span> user1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看user1组是否删除成功</span></span>
<span class="line"><span class="x-cmd">more</span> /etc/group|grep user1 </span></code></pre></div>
<h4 id="groupmod命令">📚groupmod命令</h4>
<p>📔更改群组识别码或名称。</p>
<p>🔖用户名不要随意修改，组名和 GID 也不要随意修改，因为非常容易导致管理员逻辑混乱。如果非要修改用户名或组名，则建议先删除旧的，再建立新的。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-g</td>
<td>设置欲使用的群组识别码</td>
</tr>
<tr>
<td>-o</td>
<td>重复使用群组识别码</td>
</tr>
<tr>
<td>-n</td>
<td>设置欲使用的群组名称</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:23"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 更改user1用户组为root</span></span>
<span class="line"><span class="x-cmd">groupmod</span> -n root user1</span></code></pre></div>
<h3 id="文件基本属性">文件基本属性</h3>
<p>📁 Linux 系统是一种典型的多用户系统，不同的用户处于不同的地位，拥有不同的权限。</p>
<p>📂 为了保护系统的安全性，Linux 系统对不同的用户访问同一文件（包括目录文件）的权限做了不同的规定。</p>
<p>📂 在 Linux 中我们可以使用 <code>ll</code>或者<code>ls –l</code> 命令来显示一个文件的属性以及文件所属的用户和组。</p>
<p>🖐 在 Linux 中第一个字符代表这个文件是目录、文件或链接文件等等。</p>
<p>👉若是 d 则是目录；</p>
<p>👉若是 - 则是文件；</p>
<p>👉若是 l 则表示为链接文档(link file)；</p>
<p>👉若是 b 则表示为装置文件里面的可供储存的接口设备(可随机存取装置)；</p>
<p>👉若是 c 则表示为装置文件里面的串行端口设备，例如键盘、鼠标(一次性读取装置)。</p>
<p>🔻接下来的字符中，以三个为一组，且均为<code>rwx</code>的三个参数的组合。其中， <code>r</code>代表可读(read)、 <code>w</code>代表可写(write)、 <code>x</code>代表可执行(execute)。 要注意的是，这三个权限的位置不会改变，如果没有权限，就会出现减号 <code>-</code>而已。</p>
<p>🔺 每个文件的属性由左边第一部分的 10 个字符来确定。</p>
<figure class="media is-image" style="--ar:4.8546;--w:1369px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-480.avif 480w, https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-800.avif 800w, https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-1200.avif 1200w, https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-1369.avif 1369w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-1200.webp" srcset="https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-480.webp 480w, https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-800.webp 800w, https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-1200.webp 1200w, https://demo1.661366.xyz/assets/img/f75aa00ed552dc04-f75aa00ed5-1369.webp 1369w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1369" height="282" alt="" loading="lazy" decoding="async"></picture></figure>
<p>🌥 第 0 位确定文件类型，第 1-3 位确定属主（该文件的所有者）拥有该文件的权限。</p>
<p>🌦 第4-6位确定属组（所有者的同组用户）拥有该文件的权限。</p>
<p>🌧 第7-9位确定其他用户拥有该文件的权限。</p>
<p>⛈ 第 1、4、7 位表示读权限，如果用 r 字符表示，则有读权限，如果用 - 字符表示，则没有读权限。</p>
<p>🌩 第 2、5、8 位表示写权限，如果用 w 字符表示，则有写权限，如果用 - 字符表示没有写权限。</p>
<p>🌨 第 3、6、9 位表示可执行权限，如果用 x 字符表示，则有执行权限，如果用 - 字符表示，则没有执行权限。</p>
<figure class="media is-image" style="--ar:7.7303;--w:1175px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-480.avif 480w, https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-800.avif 800w, https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-1175.avif 1175w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-1175.webp" srcset="https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-480.webp 480w, https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-800.webp 800w, https://demo1.661366.xyz/assets/img/b949a22ebd674215-b949a22ebd-1175.webp 1175w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1175" height="152" alt="" loading="lazy" decoding="async"></picture></figure>
<p>☃️ 如果查看到是文件：链接数指的是硬链接个数。</p>
<p>⛄️ 如果查看的是文件夹：链接数指的是子文件夹个数。</p>
<h3 id="文件权限常用命令">文件权限常用命令</h3>
<h4 id="chmod命令">📚chmod命令</h4>
<p>📔 改变文件或目录权限。</p>
<p>🔖chmod命令来自于英文词组”change mode“的缩写。默认只有文件的所有者和管理员可以设置文件权限，普通用户只能管理自己文件的权限属性。</p>
<p>🔖设置权限时可以使用数字法，亦可使用字母表达式，对于目录文件建议加入-R参数进行递归操作，意味着不仅对于目录本身，也对目录内的子文件/目录都进行新权限的设定。</p>
<p>📎 Linux/Unix 的文件调用权限分为三级 : <code>文件所有者（Owner）</code>、<code>用户组（Group）</code>、<code>其它用户（Other Users）</code>。</p>
<figure class="media is-image" style="--ar:5.8608;--w:1600px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-480.avif 480w, https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-800.avif 800w, https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-1200.avif 1200w, https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-1200.webp" srcset="https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-480.webp 480w, https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-800.webp 800w, https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-1200.webp 1200w, https://demo1.661366.xyz/assets/img/4414e654da69ae5d-4414e654da-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="273" alt="" loading="lazy" decoding="async"></picture></figure>
<p>📎 只有文件所有者和超级用户可以修改文件或目录的权限。可以使用绝对模式（八进制数字模式），符号模式指定文件的权限。</p>
<figure class="media is-image" style="--ar:4.3127;--w:1600px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-480.avif 480w, https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-800.avif 800w, https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-1200.avif 1200w, https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-1600.avif 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-1200.webp" srcset="https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-480.webp 480w, https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-800.webp 800w, https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-1200.webp 1200w, https://demo1.661366.xyz/assets/img/f8c12bcf95ea9e1e-f8c12bcf95-1600.webp 1600w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1600" height="371" alt="" loading="lazy" decoding="async"></picture></figure>
<p>🔦 <strong>语法：</strong> <code>chmod [ {ugoa} {+-=} {rwx} ] 文件或目录</code></p>
<p>🔦 <strong>语法：</strong> <code>chmod [ mode=421 ] 文件或目录</code></p>
<p><code>u</code> 表示该文件的拥有者，<code>g</code>表示与该文件的拥有者属于同一个群体(group)者，<code>o</code>表示其他以外的人，<code>a</code>表示这三者皆是。</p>
<p><code>+</code> 表示增加权限、<code>-</code>表示取消权限、<code>=</code> 表示唯一设定权限。</p>
<p><code>r</code>表示可读取，<code>w</code>表示可写入，<code>x</code>表示可执行，<code>X</code>表示只有当该文件是个子目录或者该文件已经被设定过为可执行。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-c</td>
<td>若该文件权限确实已经更改，才显示其更改动作</td>
</tr>
<tr>
<td>-f</td>
<td>若该文件权限无法被更改也不显示错误讯息</td>
</tr>
<tr>
<td>-v</td>
<td>显示权限变更的详细资料</td>
</tr>
<tr>
<td>-R</td>
<td>对目前目录下的所有文件与子目录进行相同的权限变更(即以递回的方式逐个变更)</td>
</tr>
</tbody></table></div>
<p>📍 chmod命令可以使用八进制数来指定权限。</p>
<p>📍 文件或目录的权限位是由9个权限位来控制，每三位为一组，它们分别是文件所有者（User）的读、写、执行，用户组（Group）的读、写、执行以及其它用户（Other）的读、写、执行。</p>
<p>📍 文件权限被放在一个比特掩码中，掩码中指定的比特位设为1，用来说明一个类具有相应的优先级。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>#</th>
<th>权限</th>
<th>rwx</th>
<th>二进制</th>
</tr>
</thead>
<tbody><tr>
<td>7</td>
<td>读 + 写 + 执行</td>
<td>r w x</td>
<td>111</td>
</tr>
<tr>
<td>6</td>
<td>读 + 写</td>
<td>r w -</td>
<td>110</td>
</tr>
<tr>
<td>5</td>
<td>读 + 执行</td>
<td>r - x</td>
<td>101</td>
</tr>
<tr>
<td>4</td>
<td>只读</td>
<td>r - -</td>
<td>100</td>
</tr>
<tr>
<td>3</td>
<td>写 + 执行</td>
<td>- w x</td>
<td>011</td>
</tr>
<tr>
<td>2</td>
<td>只写</td>
<td>- w -</td>
<td>010</td>
</tr>
<tr>
<td>1</td>
<td>只执行</td>
<td>- - x</td>
<td>001</td>
</tr>
<tr>
<td>0</td>
<td>无</td>
<td>- - -</td>
<td>000</td>
</tr>
</tbody></table></div>
<p><strong>例：765 解释：</strong></p>
<p>👉所有者的权限用数字表达：属主的那三个权限位的数字加起来的总和。如 rwx ，也就是 4+2+1 ，应该是 7。</p>
<p>👉用户组的权限用数字表达：属组的那个权限位数字的相加的总和。如 rw- ，也就是 4+2+0 ，应该是 6。</p>
<p>👉其它用户的权限数字表达：其它用户权限位的数字相加的总和。如 r-x ，也就是 4+0+1 ，应该是 5。</p>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:29;--cols:102"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 设定某个文件的权限为775</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> 775 anaconda-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定某个文件让任何人都可以读取</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> a+r anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定某个目录及其内子文件任何人都可以读取和读取</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> -R a+r Documents</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定某个二进制命令文件上新增SUID特殊权限位</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> u+s /sbin/reboot </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将文件 file1.txt 设为所有人皆可读取</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> ugo+r file1.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将文件 file1.txt 设为所有人皆可读取</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> a+r file1.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将文件 file1.txt 与 file2.txt 设为该文件拥有者，与其所属同一个群体者可写入，但其他以外的人则不可写入</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> ug+w,o-w file1.txt file2.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 为 ex1.py 文件拥有者增加可执行权限</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> u+x ex1.py</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将目前目录下的所有文件与子目录皆设为任何人可读取</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> -R a+r *</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 此外chmod也可以用数字来表示权限如</span></span>
<span class="line"><span class="hljs-built_in">chmod</span> 777 file</span></code></pre></div>
<h4 id="chown命令">📚chown命令</h4>
<p>📔 改变文件或目录的用户和用户组信息。</p>
<p>🔖chown命令来自于英文词组”Change owner“的缩写。管理员可以改变一切文件的所属信息，而普通用户只能改变自己文件的所属信息。</p>
<p>🔦 <strong>语法格式：</strong> <code>chown [参数] 所属主:所属组 文件</code></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-R</td>
<td>对目前目录下的所有文件与目录进行相同的变更</td>
</tr>
<tr>
<td>-c</td>
<td>显示所属信息变更信息</td>
</tr>
<tr>
<td>-f</td>
<td>若该文件拥有者无法被更改也不要显示错误</td>
</tr>
<tr>
<td>-h</td>
<td>只对于链接文件进行变更，而非真正指向的文件</td>
</tr>
<tr>
<td>-v</td>
<td>显示拥有者变更的详细资料</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:81"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 改变指定文件的所属主与所属组</span></span>
<span class="line"><span class="hljs-built_in">chown</span> root:root /etc/fstab</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 改变指定文件的所属主与所属组，并显示过程</span></span>
<span class="line"><span class="hljs-built_in">chown</span> -c linuxprobe:linuxprobe /etc/fstab</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 改变指定目录及其内所有子文件的所属主与所属组</span></span>
<span class="line"><span class="hljs-built_in">chown</span> -R root:root /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 把 /var/run/httpd.pid 的所有者设置 root</span></span>
<span class="line"><span class="hljs-built_in">chown</span> root /var/run/httpd.pid</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将文件 file1.txt 的拥有者设为 runoob，群体的使用者 runoobgroup</span></span>
<span class="line"><span class="hljs-built_in">chown</span> runoob:runoobgroup file1.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前前目录下的所有文件与子目录的拥有者皆设为 runoob，群体的使用者 runoobgroup</span></span>
<span class="line"><span class="hljs-built_in">chown</span> -R runoob:runoobgroup *</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 把 /home/runoob 的关联组设置为 512 （关联组ID），不改变所有者</span></span>
<span class="line"><span class="hljs-built_in">chown</span> :512 /home/runoob</span></code></pre></div>
<h4 id="chgrp命令">📚chgrp命令</h4>
<p>📔 更改文件用户组。</p>
<p>🔖chgrp是英语单词“change group”的缩写，命令的作用和其中文释义一样，为用于变更文件或目录的所属群组。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-c</td>
<td>效果类似”-v”参数，但仅回报更改的部分</td>
</tr>
<tr>
<td>-f</td>
<td>不显示错误信息</td>
</tr>
<tr>
<td>-h</td>
<td>对符号连接的文件作修改，而不更动其他任何相关文件</td>
</tr>
<tr>
<td>-R</td>
<td>递归处理，将指定目录下的所有文件及子目录一并处理</td>
</tr>
<tr>
<td>-v</td>
<td>显示指令执行过程</td>
</tr>
<tr>
<td>–reference</td>
<td>把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:56"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 改变文件的群组属性</span></span>
<span class="line"><span class="hljs-built_in">chgrp</span> -v linuxcool file</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 根据指定文件改变文件的群组属性</span></span>
<span class="line"><span class="hljs-built_in">chgrp</span> --reference=file_1 file_2</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将/usr/linuxcool及其子目录下的所有文件的用户组改为cool</span></span>
<span class="line"><span class="hljs-built_in">chgrp</span> -R cool /usr/linuxcool</span></code></pre></div>
<h3 id="搜索文件常用命令">搜索文件常用命令</h3>
<h4 id="find命令">📚find命令</h4>
<p>📔 根据路径和条件搜索指定文件。</p>
<p>🔖find命令可以使用的参数很多，并且支持正则表达式，结合管道符后能够实现更加复杂的功能。</p>
<p>🔖find命令通常进行的是从根目录（/）开始的全盘搜索，有别于whereis、which、locate……等等的有条件或部分文件的搜索。对于服务器负载较高的情况，建议不要在高峰时期使用find命令的模糊搜索，会相对消耗较多的系统资源。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-name</td>
<td>匹配名称</td>
</tr>
<tr>
<td>-perm</td>
<td>匹配权限（mode为完全匹配，-mode为包含即可）</td>
</tr>
<tr>
<td>-user</td>
<td>匹配所有者</td>
</tr>
<tr>
<td>-group</td>
<td>匹配所有组</td>
</tr>
<tr>
<td>-mtime -n +n</td>
<td>匹配修改内容的时间（-n指n天以内，+n指n天以前）</td>
</tr>
<tr>
<td>-atime -n +n</td>
<td>匹配访问文件的时间（-n指n天以内，+n指n天以前）</td>
</tr>
<tr>
<td>-ctime -n +n</td>
<td>匹配修改文件权限的时间（-n指n天以内，+n指n天以前）</td>
</tr>
<tr>
<td>-nouser</td>
<td>匹配无所有者的文件</td>
</tr>
<tr>
<td>-nogroup</td>
<td>匹配无所有组的文件</td>
</tr>
<tr>
<td>-newer f1 !f2</td>
<td>匹配比文件f1新但比f2旧的文件</td>
</tr>
<tr>
<td>-type b/d/c/p/l/f</td>
<td>匹配文件类型（后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件）</td>
</tr>
<tr>
<td>-size</td>
<td>匹配文件的大小（+50KB为查找超过50KB的文件，而-50KB为查找小于50KB的文件）</td>
</tr>
<tr>
<td>-prune</td>
<td>忽略某个目录</td>
</tr>
<tr>
<td>-exec …… {};</td>
<td>后面可跟用于进一步处理搜索结果的命令</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:44;--cols:86;--fold:18" data-lines="44"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 全盘搜索系统中所有以.conf结尾的文件</span></span>
<span class="line"><span class="x-cmd">find</span> / -name *.conf</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在/home目录中搜索所有属于指定用户的文件</span></span>
<span class="line"><span class="x-cmd">find</span> /home -user linuxprobe</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 列出当前工作目录中的所有文件、目录以及子文件信息</span></span>
<span class="line"><span class="x-cmd">find</span> .</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在/var/log目录下搜索所有指定后缀的文件，后缀不需要大小写</span></span>
<span class="line"><span class="x-cmd">find</span> /var/log -iname <span class="hljs-string">&quot;*.log&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在/var/log目录下搜索所有后缀不是.log的文件</span></span>
<span class="line"><span class="x-cmd">find</span> /var/log ! -name <span class="hljs-string">&quot;*.log&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索当前工作目录中的所有近7天被修改过的文件</span></span>
<span class="line"><span class="x-cmd">find</span> . -mtime +7</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 全盘搜索系统中所有类型为目录，且权限为1777的目录文件</span></span>
<span class="line"><span class="x-cmd">find</span> / -<span class="hljs-built_in">type</span> d -perm 1777</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 全盘搜索系统中所有类型为普通文件，且可以执行的文件信息</span></span>
<span class="line"><span class="x-cmd">find</span> / -<span class="hljs-built_in">type</span> f -perm /a=x </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 全盘搜索系统中所有后缀为.mp4的文件，并删除所有查找到的文件</span></span>
<span class="line"><span class="x-cmd">find</span> / -name <span class="hljs-string">&quot;*.mp4&quot;</span> -<span class="hljs-built_in">exec</span> <span class="hljs-built_in">rm</span> -rf {} ;</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前目录及其子目录下所有文件后缀为 .c 的文件列出来</span></span>
<span class="line"><span class="x-cmd">find</span> . -name <span class="hljs-string">&quot;*.c&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前目录及其子目录中的所有文件列出</span></span>
<span class="line"><span class="x-cmd">find</span> . -<span class="hljs-built_in">type</span> f</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前目录及其子目录下所有最近 20 天内更新过的文件列出</span></span>
<span class="line"><span class="x-cmd">find</span> . -ctime  20</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查找 /var/log 目录中更改时间在 7 日以前的普通文件，并在删除之前询问它们</span></span>
<span class="line"><span class="x-cmd">find</span> /var/log -<span class="hljs-built_in">type</span> f -mtime +7 -ok <span class="hljs-built_in">rm</span> {} ;</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查找当前目录中文件属主具有读、写权限，并且文件所属组的用户和其他用户具有读权限的文件</span></span>
<span class="line"><span class="x-cmd">find</span> . -<span class="hljs-built_in">type</span> f -perm 644 -<span class="hljs-built_in">exec</span> <span class="hljs-built_in">ls</span> -l {} ;</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查找系统中所有文件长度为 0 的普通文件，并列出它们的完整路径</span></span>
<span class="line"><span class="x-cmd">find</span> / -<span class="hljs-built_in">type</span> f -size 0 -<span class="hljs-built_in">exec</span> <span class="hljs-built_in">ls</span> -l {} ;</span></code></pre></div>
<h4 id="updatedb命令">📚updatedb命令</h4>
<p>📔 创建或更新数据库文件。</p>
<p>🔖updatedb命令用来创建或更新 slocate/locate 命令所必需的数据库文件。</p>
<p>🔖updatedb命令的执行过程较长，因为在执行时它会遍历整个系统的目录树，并将所有的文件信息写入 slocate/locate 数据库文件中。</p>
<p>🔖slocate 本身具有一个数据库，里面存放了系统中文件与目录的相关信息。</p>
<p>🔖使用updatedb命令可以创建或更新locate所使用的数据库。如果数据库已经存在，它的数据是重复使用，以避免重读并没有改变的目录。updatedb通常每天由cron运行来更新默认的数据库。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-o</td>
<td>忽略默认的数据库文件，使用指定的slocate数据库文件</td>
</tr>
<tr>
<td>-U</td>
<td>更新指定目录的slocate数据库</td>
</tr>
<tr>
<td>-v</td>
<td>显示执行的详细过程</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:60"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 检测一个未存在的文件file.txt</span></span>
<span class="line"><span class="x-cmd">locate</span> file.txt    </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用 -U 参数可以指定要更新 slocate 数据库的目录</span></span>
<span class="line"><span class="x-cmd">updatedb</span> -U /root/file/</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建或更新mlocate数据库</span></span>
<span class="line"><span class="x-cmd">updatedb</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 创建mlocate数据库，只扫描/etc目录，写入数据库到db_file文件</span></span>
<span class="line"><span class="x-cmd">updatedb</span> -o db_file -U /etc</span></code></pre></div>
<h4 id="locate命令">📚locate命令</h4>
<p>📔快速查找文件或目录。</p>
<p>🔖locate命令与find命令进行全局搜索不同，locate命令是基于了数据文件（/var/lib/locatedb）进行的定点查找，由于缩小了搜索范围，因此快速快很多。</p>
<p>🔖Linux系统需定期执行下updatedb命令对数据库文件进行更新，然后再使用locate命令进行查找，这样才会更加准确。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-c</td>
<td>只输出找到的数量</td>
</tr>
<tr>
<td>-d</td>
<td>指定数据库所在的目录</td>
</tr>
<tr>
<td>-i</td>
<td>忽略大小写</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:16;--cols:49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">updatedb</span> </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索带有指定关键词的文件</span></span>
<span class="line"><span class="x-cmd">locate</span> network</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在指定的目录下，搜索带有指定关键词的文件</span></span>
<span class="line"><span class="x-cmd">locate</span> /etc/network</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查找 passwd 文件</span></span>
<span class="line"><span class="x-cmd">locate</span> passwd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索 etc 目录下所有以 sh 开头的文件</span></span>
<span class="line"><span class="x-cmd">locate</span> /etc/sh</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 忽略大小写搜索当前用户目录下所有以 r 开头的文件</span></span>
<span class="line"><span class="x-cmd">locate</span> -i ~/r</span></code></pre></div>
<h4 id="which命令">📚which命令</h4>
<p>📔查找命令文件。</p>
<p>🔖which命令能够快速搜索二进制程序所对应的位置。如果我们既不关心同名文件（find与locate），也不关心命令所对应的源代码和帮助文件（whereis），仅仅是想找到命令本身所在的路径，那么就使用which命令。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n</td>
<td>指定文件名长度（不含路径）</td>
</tr>
<tr>
<td>-p</td>
<td>指定文件名长度（含路径）</td>
</tr>
<tr>
<td>-w</td>
<td>指定输出时栏位的宽度</td>
</tr>
<tr>
<td>-V</td>
<td>显示版本信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查找某个指定命令文件所在位置</span></span>
<span class="line"><span class="hljs-built_in">which</span> reboot</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查找多个指定命令文件所在位置</span></span>
<span class="line"><span class="hljs-built_in">which</span> shutdown poweroff</span></code></pre></div>
<h4 id="whereis命令">📚whereis命令</h4>
<p>📔显示命令及相关文件的路径。</p>
<p>🔖whereis命令用来定位命令的二进制程序、源代码文件和man手册页等相关文件的路径。</p>
<p>🔖whereis命令查找速度非常快，因为它不是在磁盘中乱找，而是在一个数据库中查询;</p>
<p>🔖数据库是linux系统自动创建的，包含有本地所有文件的信息，并且每天通过自动执行updatedb命令更新一次。</p>
<p>🔖whereis命令的搜索结果有时候会不准确，比如刚添加的文件可能搜不到， 原因就是该数据库文件没有被更新。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-b</td>
<td>查找二进制程序或命令</td>
</tr>
<tr>
<td>-B</td>
<td>从指定目录下 查找二进制程序或命令</td>
</tr>
<tr>
<td>-m</td>
<td>查找man手册文件</td>
</tr>
<tr>
<td>-M</td>
<td>从指定目录下 查找man手册文件</td>
</tr>
<tr>
<td>-s</td>
<td>只查找源代码文件</td>
</tr>
<tr>
<td>-S</td>
<td>从指定目录下 查找源代码文件</td>
</tr>
<tr>
<td>-f</td>
<td>不显示文件名前的路径名称</td>
</tr>
<tr>
<td>-u</td>
<td>查找不包含指定类型的文件</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:35"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示ln命令的程序和man手册页的位置</span></span>
<span class="line"><span class="x-cmd">whereis</span> <span class="hljs-built_in">ln</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示ln命令的二进制程序的路径</span></span>
<span class="line"><span class="x-cmd">whereis</span> -b <span class="hljs-built_in">ln</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示ln命令的man手册页的路径</span></span>
<span class="line"><span class="x-cmd">whereis</span> -m <span class="hljs-built_in">ln</span></span></code></pre></div>
<h4 id="grep命令">📚grep命令</h4>
<p>📔用于查找文件里符合条件的字符串。</p>
<p>🔖grep来自于英文词组“global search regular expression and print out the line”的缩写，意思是用于全面搜索的正则表达式，并将结果输出。</p>
<p>🔖通常会将grep命令与正则表达式搭配使用，参数作为搜索过程中的补充或对输出结果的筛选，命令模式十分灵活。</p>
<p>🔖与之容易混淆的是egrep命令和fgrep命令。如果把grep命令当作是标准搜索命令，那么egrep则是扩展搜索命令，等价于“grep -E”命令，支持扩展的正则表达式。而fgrep则是快速搜索命令，等价于“grep -F”命令，不支持正则表达式，直接按照字符串内容进行匹配。</p>
<p>🔖管道符，“|”，表示将前一个命令的处理结果输出传递给后面的命令处理。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-i</td>
<td>忽略大小写</td>
</tr>
<tr>
<td>-c</td>
<td>只输出匹配行的数量</td>
</tr>
<tr>
<td>-l</td>
<td>只列出符合匹配的文件名，不列出具体的匹配行</td>
</tr>
<tr>
<td>-n</td>
<td>列出所有的匹配行，显示行号</td>
</tr>
<tr>
<td>-h</td>
<td>查询多文件时不显示文件名</td>
</tr>
<tr>
<td>-s</td>
<td>不显示不存在、没有匹配文本的错误信息</td>
</tr>
<tr>
<td>-v</td>
<td>显示不包含匹配文本的所有行</td>
</tr>
<tr>
<td>-w</td>
<td>匹配整词</td>
</tr>
<tr>
<td>-x</td>
<td>匹配整行</td>
</tr>
<tr>
<td>-r</td>
<td>递归搜索</td>
</tr>
<tr>
<td>-q</td>
<td>禁止输出任何结果，已退出状态表示搜索是否成功</td>
</tr>
<tr>
<td>-b</td>
<td>打印匹配行距文件头部的偏移量，以字节为单位</td>
</tr>
<tr>
<td>-o</td>
<td>与-b结合使用，打印匹配的词据文件头部的偏移量，以字节为单位</td>
</tr>
<tr>
<td>-F</td>
<td>匹配固定字符串的内容</td>
</tr>
<tr>
<td>-E</td>
<td>支持扩展的正则表达式</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:43;--cols:111;--fold:18" data-lines="43"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 搜索某个文件中，包含某个关键词的内容</span></span>
<span class="line"><span class="x-cmd">grep</span> root /etc/passwd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索某个文件中，以某个关键词开头的内容</span></span>
<span class="line"><span class="x-cmd">grep</span> ^root /etc/passwd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索多个文件中，包含某个关键词的内容</span></span>
<span class="line"><span class="x-cmd">grep</span> linuxprobe /etc/passwd /etc/shadow</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索多个文件中，包含某个关键词的内容，不显示文件名称</span></span>
<span class="line"><span class="x-cmd">grep</span> -h linuxprobe /etc/passwd /etc/shadow</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出在某个文件中，包含某个关键词行的数量</span></span>
<span class="line"><span class="x-cmd">grep</span> -c root /etc/passwd /etc/shadow</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索某个文件中，包含某个关键词位置的行号及内容</span></span>
<span class="line"><span class="x-cmd">grep</span> -n network anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索某个文件中，不包含某个关键词的内容</span></span>
<span class="line"><span class="x-cmd">grep</span> -v nologin /etc/passwd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索当前工作目录中，包含某个关键词内容的文件，未找到则提示</span></span>
<span class="line"><span class="x-cmd">grep</span> -l root *</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索当前工作目录中，包含某个关键词内容的文件，未找到不提示</span></span>
<span class="line"><span class="x-cmd">grep</span> -sl root *</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 递归搜索，不仅搜索指定目录，还搜索其内子目录内是否有关键词文件</span></span>
<span class="line"><span class="x-cmd">grep</span> -srl root /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索某个文件中，精准匹配到某个关键词的内容（搜索词应与整行内容完全一样才会显示，有别于一般搜索）</span></span>
<span class="line"><span class="x-cmd">grep</span> -x <span class="hljs-built_in">cd</span> anaconda-ks.cfg </span>
<span class="line"><span class="x-cmd">grep</span> -x cdrom anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 判断某个文件中，是否包含某个关键词，通过返回状态值输出结果（0为包含，1为不包含），方便在Shell脚本中判断和调用</span></span>
<span class="line"><span class="x-cmd">grep</span> -q linuxprobe anaconda-ks.cfg </span>
<span class="line"><span class="hljs-built_in">echo</span> $?</span>
<span class="line"></span>
<span class="line"><span class="x-cmd">grep</span> -q linuxcool anaconda-ks.cfg </span>
<span class="line"><span class="hljs-built_in">echo</span> $?</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 搜索某个文件中，空行的数量</span></span>
<span class="line"><span class="x-cmd">grep</span> -c ^$ anaconda-ks.cfg </span></code></pre></div>
<h3 id="解压缩常用命令">解压缩常用命令</h3>
<h4 id="gzip命令">📚gzip命令</h4>
<p>📔 压缩和解压文件。</p>
<p>🔖gzip命令来自于英文单词gunzip的缩写。gzip是一款使用广泛的压缩工具，文件经过压缩后一般会以.gz后缀结尾，与tar命令合用后即为.tar.gz后缀。</p>
<p>🔖gzip命令对文本文件的压缩比率通常能达到60%~70%，压缩后可以很好的提升存储空间的使用率，还能够在网络传输文件时减少等待时间。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>使用ASCII文字模式</td>
</tr>
<tr>
<td>-d</td>
<td>解开压缩文件</td>
</tr>
<tr>
<td>-f</td>
<td>强行压缩文件</td>
</tr>
<tr>
<td>-k</td>
<td>保留原文件</td>
</tr>
<tr>
<td>-l</td>
<td>列出压缩文件的相关信息</td>
</tr>
<tr>
<td>-L</td>
<td>显示版本与版权信息</td>
</tr>
<tr>
<td>-c</td>
<td>把压缩后的文件输出到标准输出设备，不去更动原始文件</td>
</tr>
<tr>
<td>-r</td>
<td>递归处理，将指定目录下的所有文件及子目录一并处理</td>
</tr>
<tr>
<td>-q</td>
<td>不显示警告信息</td>
</tr>
<tr>
<td>-n</td>
<td>压缩文件时，不保存原来的文件名称及时间戳记</td>
</tr>
<tr>
<td>-N</td>
<td>压缩文件时，保存原来的文件名称及时间戳记</td>
</tr>
<tr>
<td>-t</td>
<td>测试压缩文件是否正确无误</td>
</tr>
<tr>
<td>-S&lt;压缩字尾字符串&gt;</td>
<td>更改压缩字尾字符串。</td>
</tr>
<tr>
<td>-v</td>
<td>显示指令执行过程</td>
</tr>
<tr>
<td>-V</td>
<td>显示版本信息</td>
</tr>
<tr>
<td>-&lt;压缩效率&gt;</td>
<td>压缩效率是一个介于1－9的数值，预设值为&quot;6&quot;，指定愈大的数值，压缩效率就会愈高</td>
</tr>
<tr>
<td>–best</td>
<td>此参数的效果和指定&quot;-9&quot;参数相同</td>
</tr>
<tr>
<td>–fast</td>
<td>此参数的效果和指定&quot;-1&quot;参数相同</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:94"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 将指定的文件进行压缩，压缩包默认会以“原文件名.gz”保存到当前工作目录下，原文件会被自动删除</span></span>
<span class="line"><span class="x-cmd">gzip</span> anaconda-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 解压指定的压缩包文件，并显示解压过程。解压后的文件会保存在当前工作目录下，压缩包会被自动删除</span></span>
<span class="line"><span class="x-cmd">gzip</span> -dv anaconda-ks.cfg.gz </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将指定的文件进行压缩，但是不删除原文件</span></span>
<span class="line">[root@linuxcool ~]# gzip -k initial-setup-ks.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示指定文件的压缩信息</span></span>
<span class="line"><span class="x-cmd">gzip</span> -l initial-setup-ks.cfg.gz </span></code></pre></div>
<h4 id="gunzip命令">📚gunzip命令</h4>
<p>📔解压提取文件内容。</p>
<p>🔖gunzip命令来自于英文词组”Gnu unzip“的缩写。gunzip通常被用来解压那些被基于gzip格式压缩过的文件，也就是那些.gz结尾的压缩包。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>使用ASCII文字模式</td>
</tr>
<tr>
<td>-c</td>
<td>把解压后的文件输出到标准输出设备</td>
</tr>
<tr>
<td>-f</td>
<td>强行解开压缩文件，不理会文件名称或硬连接是否存在以及该文件是否为符号连接</td>
</tr>
<tr>
<td>-h</td>
<td>在线帮助</td>
</tr>
<tr>
<td>-l</td>
<td>列出压缩文件的相关信息</td>
</tr>
<tr>
<td>-L</td>
<td>显示版本与版权信息</td>
</tr>
<tr>
<td>-n</td>
<td>解压缩时，若压缩文件内含有原来的文件名称及时间戳记，则将其忽略不予处理</td>
</tr>
<tr>
<td>-N</td>
<td>解压缩时，若压缩文件内含有原来的文件名称及时间戳记，则将其回存到解开的文件上</td>
</tr>
<tr>
<td>-q</td>
<td>不显示警告信息</td>
</tr>
<tr>
<td>-r</td>
<td>递归处理，将指定目录下的所有文件及子目录一并处理</td>
</tr>
<tr>
<td>-S&lt;压缩字尾字符串&gt;</td>
<td>更改压缩字尾字符串</td>
</tr>
<tr>
<td>-t</td>
<td>测试压缩文件是否正确无误</td>
</tr>
<tr>
<td>-v</td>
<td>显示指令执行过程</td>
</tr>
<tr>
<td>-V</td>
<td>显示版本信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:48"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 解压指定的压缩包文件</span></span>
<span class="line"><span class="x-cmd">gunzip</span> Filename.gz</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 解压指定的压缩包文件，并输出解压过程</span></span>
<span class="line"><span class="x-cmd">gunzip</span> -v Filename.gz</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 测试指定的压缩包文件内容是否损坏，能够正常解压</span></span>
<span class="line"><span class="x-cmd">gunzip</span> -t Filename.gz</span></code></pre></div>
<h4 id="zip命令">📚zip命令</h4>
<p>📔压缩文件。</p>
<p>🔖通过zip命令可以将文件打包成.zip格式的压缩包，里面会附含文件的名称、路径、创建时间、上次修改时间等等信息，与tar命令相似。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-A</td>
<td>调整可执行的自动解压缩文件</td>
</tr>
<tr>
<td>-b&lt;工作目录&gt;</td>
<td>指定暂时存放文件的目录</td>
</tr>
<tr>
<td>-c</td>
<td>替每个被压缩的文件加上注释</td>
</tr>
<tr>
<td>-d</td>
<td>从压缩文件内删除指定的文件</td>
</tr>
<tr>
<td>-D</td>
<td>压缩文件内不建立目录名称</td>
</tr>
<tr>
<td>-f</td>
<td>更新现有的文件</td>
</tr>
<tr>
<td>-F</td>
<td>尝试修复已损坏的压缩文件</td>
</tr>
<tr>
<td>-g</td>
<td>将文件压缩后附加在既有的压缩文件之后，而非另行建立新的压缩文件</td>
</tr>
<tr>
<td>-i&lt;范本样式&gt;</td>
<td>只压缩符合条件的文件</td>
</tr>
<tr>
<td>-j</td>
<td>只保存文件名称及其内容，而不存放任何目录名称</td>
</tr>
<tr>
<td>-J</td>
<td>删除压缩文件前面不必要的数据</td>
</tr>
<tr>
<td>-L</td>
<td>显示版权信息</td>
</tr>
<tr>
<td>-m</td>
<td>将文件压缩并加入压缩文件后，删除原始文件，即把文件移到压缩文件中</td>
</tr>
<tr>
<td>-n&lt;字尾字符串&gt;</td>
<td>不压缩具有特定字尾字符串的文件</td>
</tr>
<tr>
<td>-o</td>
<td>以压缩文件内拥有最新更改时间的文件为准，将压缩文件的更改时间设成和该文件相同</td>
</tr>
<tr>
<td>-q</td>
<td>不显示指令执行过程</td>
</tr>
<tr>
<td>-r</td>
<td>递归处理，将指定目录下的所有文件和子目录一并处理</td>
</tr>
<tr>
<td>-S</td>
<td>包含系统和隐藏文件</td>
</tr>
<tr>
<td>-t&lt;日期时间&gt;</td>
<td>把压缩文件的日期设成指定的日期</td>
</tr>
<tr>
<td>-T</td>
<td>检查备份文件内的每个文件是否正确无误</td>
</tr>
<tr>
<td>-u</td>
<td>与 -f 参数类似，但是除了更新现有的文件外，也会将压缩文件中的其他文件解压缩到目录中。</td>
</tr>
<tr>
<td>-v</td>
<td>显示指令执行过程或显示版本信息</td>
</tr>
<tr>
<td>-w</td>
<td>在文件名称里假如版本编号，本参数仅在VMS操作系统下有效</td>
</tr>
<tr>
<td>-x&lt;范本样式&gt;</td>
<td>压缩时排除符合条件的文件</td>
</tr>
<tr>
<td>-X</td>
<td>不保存额外的文件属性</td>
</tr>
<tr>
<td>-y</td>
<td>直接保存符号连接，而非该连接所指向的文件，本参数仅在UNIX之类的系统下有效</td>
</tr>
<tr>
<td>-z</td>
<td>替压缩文件加上注释</td>
</tr>
<tr>
<td>-$</td>
<td>保存第一个被压缩文件所在磁盘的卷册名称</td>
</tr>
<tr>
<td>-&lt;压缩效率&gt;</td>
<td>压缩效率是一个介于1-9的数值</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:17;--cols:69"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 将指定目录及其内全部文件都打包成zip格式压缩包文件</span></span>
<span class="line"><span class="x-cmd">zip</span> -r backup1.zip /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前工作目录内所有以.cfg为后缀的文件打包</span></span>
<span class="line"><span class="x-cmd">zip</span> -r backup2.zip *.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 更新压缩包文件中某个文件</span></span>
<span class="line"><span class="x-cmd">zip</span> -dv backup2.zip anaconda-ks.cfg </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将/home/html/ 这个目录下所有文件和文件夹打包为当前目录下的 html.zip</span></span>
<span class="line"><span class="x-cmd">zip</span> -q -r html.zip /home/html</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 如果在我们在 /home/html 目录下，可以执行以下命令</span></span>
<span class="line"><span class="x-cmd">zip</span> -q -r html.zip *</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 从压缩文件 cp.zip 中删除文件 a.c</span></span>
<span class="line"><span class="x-cmd">zip</span> -dv cp.zip a.c</span></code></pre></div>
<h4 id="unzip命令">📚unzip命令</h4>
<p>📔解压缩zip格式文件。</p>
<p>🔖unzip命令用于解压缩zip格式文件，虽然Linux系统中更多的使用tar命令进行对压缩包的管理工作，但有时也会收到同Windows系统常用的.zip和.rar格式的压缩包文件。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-c</td>
<td>将解压缩的结果显示到屏幕上，并对字符做适当的转换</td>
</tr>
<tr>
<td>-f</td>
<td>更新现有的文件</td>
</tr>
<tr>
<td>-l</td>
<td>显示压缩文件内所包含的文件</td>
</tr>
<tr>
<td>-p</td>
<td>与-c参数类似，会将解压缩的结果显示到屏幕上，但不会执行任何的转换</td>
</tr>
<tr>
<td>-t</td>
<td>检查压缩文件是否正确</td>
</tr>
<tr>
<td>-v</td>
<td>执行时显示详细的信息</td>
</tr>
<tr>
<td>-z</td>
<td>仅显示压缩文件的备注文字</td>
</tr>
<tr>
<td>-a</td>
<td>对文本文件进行必要的字符转换</td>
</tr>
<tr>
<td>-b</td>
<td>不要对文本文件进行字符转换</td>
</tr>
<tr>
<td>-C</td>
<td>压缩文件中的文件名称区分大小写</td>
</tr>
<tr>
<td>-j</td>
<td>不处理压缩文件中原有的目录路径</td>
</tr>
<tr>
<td>-L</td>
<td>将压缩文件中的全部文件名改为小写</td>
</tr>
<tr>
<td>-n</td>
<td>解压缩时不要覆盖原有的文件</td>
</tr>
<tr>
<td>-o</td>
<td>不必先询问用户，unzip执行后覆盖原有文件</td>
</tr>
<tr>
<td>-q</td>
<td>执行时不显示任何信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 将压缩包文件解压到当前工作目录中</span></span>
<span class="line"><span class="x-cmd">unzip</span> latest.zip</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将压缩包文件解压到指定的目录中</span></span>
<span class="line"><span class="x-cmd">unzip</span> latest.zip -d /home</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 测试压缩包文件是否完整，文件有无损坏</span></span>
<span class="line"><span class="x-cmd">unzip</span> -t latest.zip</span></code></pre></div>
<h4 id="tar命令">📚tar命令</h4>
<p>📔压缩和解压缩文件。</p>
<p>🔖tar命令能够制作出Linux系统中常见的.tar、.tar.gz、.tar.bz2等格式的压缩包文件。对于RHEL7、CentOS7版本以后的系统，解压时可以不加压缩格式参数（如z或j），系统能自动进行分析并解压。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-A</td>
<td>新增文件到以存在的备份文件</td>
</tr>
<tr>
<td>-B</td>
<td>设置区块大小</td>
</tr>
<tr>
<td>-c</td>
<td>建立新的备份文件</td>
</tr>
<tr>
<td>-C</td>
<td>&lt;目录&gt; 仅压缩指定目录里的内容或解压缩到指定目录</td>
</tr>
<tr>
<td>-d</td>
<td>记录文件的差别</td>
</tr>
<tr>
<td>-x</td>
<td>从归档文件中提取文件</td>
</tr>
<tr>
<td>-t</td>
<td>列出备份文件的内容</td>
</tr>
<tr>
<td>-z</td>
<td>通过gzip指令压缩/解压缩文件，文件名最好为*.tar.gz</td>
</tr>
<tr>
<td>-Z</td>
<td>通过compress指令处理备份文件</td>
</tr>
<tr>
<td>-f&lt;备份文件&gt;</td>
<td>指定备份文件</td>
</tr>
<tr>
<td>-v</td>
<td>显示指令执行过程</td>
</tr>
<tr>
<td>-r</td>
<td>添加文件到已经压缩的文件</td>
</tr>
<tr>
<td>-u</td>
<td>添加改变了和现有的文件到已经存在的压缩文件</td>
</tr>
<tr>
<td>-j</td>
<td>通过bzip2指令压缩/解压缩文件，文件名最好为*.tar.bz2</td>
</tr>
<tr>
<td>-v</td>
<td>显示操作过程</td>
</tr>
<tr>
<td>-l</td>
<td>文件系统边界设置</td>
</tr>
<tr>
<td>-k</td>
<td>保留原有文件不覆盖</td>
</tr>
<tr>
<td>-m</td>
<td>保留文件不被覆盖</td>
</tr>
<tr>
<td>-w</td>
<td>确认压缩文件的正确性</td>
</tr>
<tr>
<td>-p</td>
<td>保留原来的文件权限与属性</td>
</tr>
<tr>
<td>-P</td>
<td>使用文件名的绝对路径，不移除文件名称前的“/”号</td>
</tr>
<tr>
<td>-N &lt;日期格式&gt;</td>
<td>只将较指定日期更新的文件保存到备份文件里</td>
</tr>
<tr>
<td>– -exclude=&lt;范本样式&gt;</td>
<td>排除符合范本样式的文件</td>
</tr>
<tr>
<td>– -remove-files</td>
<td>归档/压缩之后删除源文件</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:81"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 使用gzip压缩格式对某个目录进行打包操作，显示压缩过程，压缩包规范后缀为.tar.gz</span></span>
<span class="line"><span class="x-cmd">tar</span> czvf backup1.tar.gz /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用bzip2压缩格式对某个目录进行打包操作，显示压缩过程，压缩包规范后缀为.tar.bz2</span></span>
<span class="line"><span class="x-cmd">tar</span> cjvf backup2.tar.bz2 /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前工作目录内所有以.cfg为后缀的文件打包，不进行压缩</span></span>
<span class="line"><span class="x-cmd">tar</span> cvf backup3.tar *.cfg</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将当前工作目录内的所有以.cfg为后缀的文件打包，不进行压缩，并删除原始文件</span></span>
<span class="line"><span class="x-cmd">tar</span> cvf backup4.tar *.cfg --remove-files</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 解压某个压缩包到当前工作目录</span></span>
<span class="line"><span class="x-cmd">tar</span> xvf backup4.tar </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 解压某个压缩包到/etc目录</span></span>
<span class="line"><span class="x-cmd">tar</span> xvf backup4.tar -C /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看某个压缩包内文件信息（无需解压）</span></span>
<span class="line"><span class="x-cmd">tar</span> tvf backup4.tar </span></code></pre></div>
<h3 id="磁盘和分区常用命令">磁盘和分区常用命令</h3>
<h4 id="tree命令">📚tree命令</h4>
<p>📔以树状图形式列出目录内容。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示所有文件和目录</td>
</tr>
<tr>
<td>-C</td>
<td>彩色显示</td>
</tr>
<tr>
<td>-d</td>
<td>仅显示目录名称</td>
</tr>
<tr>
<td>-D</td>
<td>显示文件更改时间</td>
</tr>
<tr>
<td>-f</td>
<td>显示完整的相对路径名称</td>
</tr>
<tr>
<td>-g</td>
<td>显示文件所属群组名称</td>
</tr>
<tr>
<td>-i</td>
<td>不以阶梯状列出文件或目录名称</td>
</tr>
<tr>
<td>-l</td>
<td>直接显示连接文件所指向的原始目录</td>
</tr>
<tr>
<td>-n</td>
<td>不在文件和目录清单上加上色彩</td>
</tr>
<tr>
<td>-N</td>
<td>直接列出文件和目录名称</td>
</tr>
<tr>
<td>-p</td>
<td>列出权限标示</td>
</tr>
<tr>
<td>-P&lt;范本样式&gt;</td>
<td>只显示符合范本像是的文件或目录名称</td>
</tr>
<tr>
<td>-s</td>
<td>列出文件或目录大小</td>
</tr>
<tr>
<td>-t</td>
<td>用文件和目录的更改时间排序</td>
</tr>
<tr>
<td>-u</td>
<td>列出文件或目录的拥有者名称</td>
</tr>
<tr>
<td>-x</td>
<td>将范围局限在现行的文件系统中</td>
</tr>
<tr>
<td>-L</td>
<td>层级显示</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">显示当前工作目录下的文件层级情况</span>
<span class="line"><span class="x-cmd">tree</span></span>
<span class="line"></span>
<span class="line">以文件和目录的更改时间进行排序</span>
<span class="line"><span class="x-cmd">tree</span> -t</span>
<span class="line"></span>
<span class="line">以带有相对路径的形式，显示当前工作目录下的文件层级情况</span>
<span class="line"> <span class="x-cmd">tree</span> -f</span>
<span class="line"></span>
<span class="line">只显示目录的层级关系情况</span>
<span class="line"><span class="x-cmd">tree</span> -d</span></code></pre></div>
<h4 id="du命令">📚du命令</h4>
<p>📔查看文件或目录的大小。</p>
<p>🔖du命令来自于英文词组“Disk Usage”的缩写。不要将df和du命令混淆，df是用于查看磁盘或分区使用情况的命令，而du命令则是用于按照指定容量单位来查看文件或目录在磁盘中的占用情况。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示目录中所有文件大小</td>
</tr>
<tr>
<td>-k</td>
<td>以KB为单位显示文件大小</td>
</tr>
<tr>
<td>-m</td>
<td>以MB为单位显示文件大小</td>
</tr>
<tr>
<td>-g</td>
<td>以GB为单位显示文件大小</td>
</tr>
<tr>
<td>-h</td>
<td>以易读方式显示文件大小</td>
</tr>
<tr>
<td>-s</td>
<td>仅显示总计</td>
</tr>
<tr>
<td>-exclude=&lt;目录或文件&gt;</td>
<td>略过指定的目录或文件</td>
</tr>
<tr>
<td>–max-depth=&lt;目录层数&gt;</td>
<td>超过指定层数的目录后，予以忽略</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 以易读的容量格式显示指定目录内各个文件的大小信息</span></span>
<span class="line"><span class="hljs-built_in">du</span> -h /etc</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以易读的容量格式显示指定目录内总文件的大小信息</span></span>
<span class="line"><span class="hljs-built_in">du</span> -sh /etc </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示指定文件的大小信息（默认单位为K）</span></span>
<span class="line"><span class="hljs-built_in">du</span> anaconda-ks.cfg </span></code></pre></div>
<h4 id="df命令">📚df命令</h4>
<p>📔显示系统上磁盘空间的使用量情况。</p>
<p>🔖df命令来自于英文词组”Disk Free“的缩写。df命令显示的磁盘使用量情况含可用、已有及使用率等信息，默认单位为Kb，建议使用-h参数进行单位换算。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示所有系统文件</td>
</tr>
<tr>
<td>-B &lt;块大小&gt;</td>
<td>指定显示时的块大小</td>
</tr>
<tr>
<td>-h</td>
<td>以容易阅读的方式显示</td>
</tr>
<tr>
<td>-H</td>
<td>以1000字节为换算单位来显示</td>
</tr>
<tr>
<td>-i</td>
<td>显示索引字节信息</td>
</tr>
<tr>
<td>-k</td>
<td>指定块大小为1KB</td>
</tr>
<tr>
<td>-l</td>
<td>只显示本地文件系统</td>
</tr>
<tr>
<td>-t &lt;文件系统类型&gt;</td>
<td>只显示指定类型的文件系统</td>
</tr>
<tr>
<td>-T</td>
<td>输出时显示文件系统类型</td>
</tr>
<tr>
<td>– -sync</td>
<td>在取得磁盘使用信息前，先执行sync命令</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:53"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 带有容量单位的显示系统全部磁盘使用量情况</span></span>
<span class="line"><span class="hljs-built_in">df</span> -h</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 带有容量单位的显示指定磁盘分区使用量情况</span></span>
<span class="line"><span class="hljs-built_in">df</span> -h /boot</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示系统中所有文件系统格式为xfs的磁盘分区使用量情况</span></span>
<span class="line"><span class="hljs-built_in">df</span> -t xfs</span></code></pre></div>
<h4 id="free命令">📚free命令</h4>
<p>📔显示系统内存使用量情况，包含物理和交换内存的总量、使用量和空闲量情况。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>b</td>
<td>以Byte显示内存使用情况</td>
</tr>
<tr>
<td>-k</td>
<td>以kb为单位显示内存使用情况</td>
</tr>
<tr>
<td>-m</td>
<td>以mb为单位显示内存使用情况</td>
</tr>
<tr>
<td>-g</td>
<td>以gb为单位显示内存使用情况</td>
</tr>
<tr>
<td>-s</td>
<td>持续显示内存</td>
</tr>
<tr>
<td>-t</td>
<td>显示内存使用总合</td>
</tr>
<tr>
<td>-h</td>
<td>以易读的单位显示内存使用情况</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 以默认的容量单位显示内存使用量信息</span></span>
<span class="line"><span class="x-cmd">free</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以MB位单位显示内存使用量信息</span></span>
<span class="line"><span class="x-cmd">free</span> -m</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以易读的单位显示内存使用量信息</span></span>
<span class="line"><span class="x-cmd">free</span> -h</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以易读的单位显示内存使用量信息，每个10秒刷新一次</span></span>
<span class="line"><span class="x-cmd">free</span> -hs 10</span></code></pre></div>
<h4 id="lsblk">📚lsblk</h4>
<p>📔查看系统的磁盘使用情况。来自于英文词组”list block“的缩写。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示所有设备</td>
</tr>
<tr>
<td>-b</td>
<td>以字节单位显示设备大小</td>
</tr>
<tr>
<td>-d</td>
<td>不显示 slaves 或 holders</td>
</tr>
<tr>
<td>-e</td>
<td>排除设备</td>
</tr>
<tr>
<td>-f</td>
<td>显示文件系统信息</td>
</tr>
<tr>
<td>-h</td>
<td>显示帮助信息</td>
</tr>
<tr>
<td>-i</td>
<td>仅使用字符</td>
</tr>
<tr>
<td>-m</td>
<td>显示权限信息</td>
</tr>
<tr>
<td>-l</td>
<td>使用列表格式显示</td>
</tr>
<tr>
<td>-n</td>
<td>不显示标题</td>
</tr>
<tr>
<td>-o</td>
<td>输出列</td>
</tr>
<tr>
<td>-P</td>
<td>使用key=”value”格式显示</td>
</tr>
<tr>
<td>-r</td>
<td>使用原始格式显示</td>
</tr>
<tr>
<td>-t</td>
<td>显示拓扑结构信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:44"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示系统中所有磁盘设备的使用情况信息</span></span>
<span class="line"><span class="x-cmd">lsblk</span> -a</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示系统中磁盘设备的归属及权限信息</span></span>
<span class="line"><span class="x-cmd">lsblk</span> -m</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示系统中所有SCSI类型的磁盘设备信息</span></span>
<span class="line"><span class="x-cmd">lsblk</span> -S</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以列表格式显示磁盘设备信息，并且不显示标题</span></span>
<span class="line"><span class="x-cmd">sblk</span> -<span class="hljs-built_in">nl</span></span></code></pre></div>
<h4 id="mount命令">📚mount命令</h4>
<p>📔把文件系统挂载到目录。</p>
<p>🔖文件系统指的是被格式化过的硬盘或分区设备，进行挂载操作后，用户便可以在挂载目录中使用硬盘资源。默认情况下Linux系统并不会像Windows系统那样自动的挂载光盘和U盘设备，需要自行完成。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-t</td>
<td>指定挂载类型</td>
</tr>
<tr>
<td>-l</td>
<td>显示已加载的文件系统列表</td>
</tr>
<tr>
<td>-h</td>
<td>显示帮助信息并退出</td>
</tr>
<tr>
<td>-V</td>
<td>显示程序版本</td>
</tr>
<tr>
<td>-n</td>
<td>加载没有写入文件“/etc/mtab”中的文件系统</td>
</tr>
<tr>
<td>-r</td>
<td>将文件系统加载为只读模式</td>
</tr>
<tr>
<td>-a</td>
<td>加载文件“/etc/fstab”中描述的所有文件系统</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:68"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看当前系统中已有的文件系统信息（可结合管道符与grep命令进行过滤）</span></span>
<span class="line"><span class="x-cmd">mount</span> </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 挂载/etc/fstab文件中所有已定义的设备文件</span></span>
<span class="line"><span class="x-cmd">mount</span> -a</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将光盘设备挂载到指定目录</span></span>
<span class="line"><span class="x-cmd">mount</span> /dev/cdrom /media/cdrom</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 强制以xfs文件系统挂载硬盘设备到指定目录</span></span>
<span class="line"><span class="x-cmd">mount</span> -t xfs /dev/sdb /disk</span></code></pre></div>
<h4 id="umount命令">📚umount命令</h4>
<p>📔把文件系统挂载到目录。</p>
<p>🔖umount命令来自于英文单词unmount的缩写。umount卸载命令只需要提供设备名或挂载目录之一即可。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>卸载/etc/mtab中记录的所有文件系统</td>
</tr>
<tr>
<td>-h</td>
<td>显示帮助</td>
</tr>
<tr>
<td>-n</td>
<td>卸载时不要将信息存入/etc/mtab文件中</td>
</tr>
<tr>
<td>-r</td>
<td>尝试以只读的方式重新挂入文件系统</td>
</tr>
<tr>
<td>-t</td>
<td>仅卸载选项中所指定的文件系统</td>
</tr>
<tr>
<td>-v</td>
<td>执行时显示详细的信息</td>
</tr>
<tr>
<td>-V</td>
<td>显示版本信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 卸载指定的文件系统</span></span>
<span class="line"><span class="x-cmd">umount</span> /dev/sdb</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 卸载指定的文件系统并显示过程</span></span>
<span class="line"><span class="x-cmd">umount</span> -v /dev/cdrom</span></code></pre></div>
<h4 id="fdisk命令">📚fdisk命令</h4>
<p>📔管理磁盘的分区信息。</p>
<p>🔖fdisk命令来自于英文词组“Partition table manipulator for Linux”的缩写，其功能是用于管理磁盘的分区信息。fdisk命令可以用于对磁盘进行分区操作，根据实际情况进行合理划分。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-b</td>
<td>指定每个分区的大小</td>
</tr>
<tr>
<td>-l</td>
<td>列出指定的外围设备的分区表状况</td>
</tr>
<tr>
<td>-s</td>
<td>将指定的分区大小输出到标准输出上，单位为区块</td>
</tr>
<tr>
<td>-u</td>
<td>搭配”-l”参数列表，会用分区数目取代柱面数目，来表示每个分区的起始地址</td>
</tr>
<tr>
<td>-v</td>
<td>显示版本信息</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:24"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 查看当前系统的分区情况</span></span>
<span class="line"><span class="x-cmd">fdisk</span> -l</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 管理指定硬盘的分区</span></span>
<span class="line"><span class="x-cmd">fdisk</span> /dev/sda</span></code></pre></div>
<h4 id="cfdisk命令">📚cfdisk命令</h4>
<p>📔磁盘分区。</p>
<p>🔖cfdisk是用来磁盘分区的程序，它十分类似DOS的fdisk，具有互动式操作界面而非传统fdisk的问答式界面，您可以轻易地利用方向键来操控分区操作。cfdisk指令是一个基于鼠标的、用于硬盘分区的程序。输入指令cfdisk之后，将出现上面的图形界面，它是基于鼠标进行操作的，利用它可以进行磁盘的分区操作。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>在程序里不用反白代表选取，而以箭头表示</td>
</tr>
<tr>
<td>-c</td>
<td>忽略BIOS的数值，直接指定磁盘的柱面数目</td>
</tr>
<tr>
<td>-P</td>
<td>显示分区表的内容</td>
</tr>
<tr>
<td>-s</td>
<td>忽略BIOS的数值，直接指定磁盘的磁区数目</td>
</tr>
<tr>
<td>-v</td>
<td>显示版本信息</td>
</tr>
<tr>
<td>-z</td>
<td>不读取现有的分区，直接当作没有分区的新磁盘使用</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 进行磁盘分区</span></span>
<span class="line"><span class="x-cmd">cfsik</span> </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 进行磁盘分区，使用箭头进行操作，而不使用反白表示</span></span>
<span class="line"><span class="x-cmd">cfsik</span> -a</span></code></pre></div>
<h4 id="mkfs命令">📚mkfs命令</h4>
<p>📔对设备进行格式化文件系统操作。</p>
<p>🔖mkfs命令来自于英文词组“make file system”的缩写。在挂载使用硬盘空间前的最后一步，运维人员需要对整块硬盘或指定分区进行格式化文件系统操作，Linux系统支持的文件系统包含ext2、ext3、ext4、xfs、fat、msdos、vfat、minix等多种格式。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-V</td>
<td>详细显示模式</td>
</tr>
<tr>
<td>-t</td>
<td>给定档案系统的型式</td>
</tr>
<tr>
<td>-c</td>
<td>检查该设备是否有损坏</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:56"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 对指定的硬盘进行格式化文件系统操作</span></span>
<span class="line"><span class="x-cmd">mkfs</span> -t ext4 /dev/sdb</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 对指定的硬盘进行格式化文件系统操作，并输出详细过程信息</span></span>
<span class="line"><span class="x-cmd">mkfs</span> -V -t xfs /dev/sdb</span></code></pre></div>
<h3 id="进程管理常用命令">进程管理常用命令</h3>
<h4 id="ps命令">📚ps命令</h4>
<p>📔显示进程状态。</p>
<p>🔖ps命令来自于英文词组”process status“的缩写。使用ps命令可以查看到进程的所有信息，例如进程的号码、发起者、系统资源使用占比（处理器与内存）、运行状态等等。帮助我们及时的发现哪些进程出现”僵死“或”不可中断“等异常情况。经常会与kill命令搭配使用来中断和删除不必要的服务进程，避免服务器的资源浪费。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>a</td>
<td>显示现行终端机下的所有程序，包括其他用户的程序</td>
</tr>
<tr>
<td>-A</td>
<td>显示所有程序</td>
</tr>
<tr>
<td>c</td>
<td>显示每个程序真正的指令名称，而不包含路径</td>
</tr>
<tr>
<td>-d</td>
<td>显示所有程序，但不包括阶段作业管理员的程序</td>
</tr>
<tr>
<td>e</td>
<td>列出程序时，显示每个程序所使用的环境变量</td>
</tr>
<tr>
<td>-f</td>
<td>显示UID,PPIP,C与STIME栏位</td>
</tr>
<tr>
<td>g</td>
<td>显示现行终端机下的所有程序，包括所属组的程序</td>
</tr>
<tr>
<td>h</td>
<td>不显示标题列</td>
</tr>
<tr>
<td>-H</td>
<td>显示树状结构，表示程序间的相互关系</td>
</tr>
<tr>
<td>-l</td>
<td>采用详细的格式来显示程序状况</td>
</tr>
<tr>
<td>-N</td>
<td>显示所有的程序，除了执行ps指令终端机下的程序之外</td>
</tr>
<tr>
<td>r</td>
<td>只列出现行终端机正在执行中的程序</td>
</tr>
<tr>
<td>-T</td>
<td>显示现行终端机下的所有程序</td>
</tr>
<tr>
<td>u</td>
<td>以用户为主的格式来显示程序状况</td>
</tr>
<tr>
<td>v</td>
<td>采用虚拟内存的格式显示程序状况</td>
</tr>
<tr>
<td>-w或w</td>
<td>采用宽阔的格式来显示程序状况</td>
</tr>
<tr>
<td>x</td>
<td>显示所有程序，不以终端机来区分</td>
</tr>
<tr>
<td>–cols &lt;每列字符数&gt;</td>
<td>设置每列的最大字符数</td>
</tr>
<tr>
<td>–lines &lt;显示列数&gt;</td>
<td>设置显示画面的列数</td>
</tr>
</tbody></table></div>
<p><strong>ps 的参数非常多, 在此仅列出几个常用的参数并大略介绍含义:</strong></p>
<ul>
<li>-A 列出所有的进程</li>
<li>-w 显示加宽可以显示较多的资讯</li>
<li>-au 显示较详细的资讯</li>
<li>aux 显示所有包含其他使用者的进程</li>
</ul>
<p><strong>au(x) 输出格式 :</strong><code>USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND</code></p>
<ul>
<li>USER: 行程拥有者</li>
<li>PID: 进程的ID号</li>
<li>%CPU: 占用的 CPU 的百分比</li>
<li>%MEM: 占用理内存的百分比</li>
<li>VSZ: 占用的虚拟内存大小，单位KB</li>
<li>RSS: 占用理内存的大小，单位KB</li>
<li>TTY: 改进程是在哪个终端中运行的。对于CentOS来说，tty1是图像化界面，tty2-tty6是本地的字符界面终端。pst/0-255代表虚拟终端</li>
<li>STAT: 该行程的状态:<ul>
<li>R: 运行状态</li>
<li>D: 无法中断的休眠状态 (通常 IO 的进程)</li>
<li>R: 正在执行中</li>
<li>S: 静止状态(睡眠状态)</li>
<li>s: 包含子进程</li>
<li>l: 多线程</li>
<li>+: 前台显示</li>
<li>T: 暂停执行</li>
<li>Z: 不存在但暂时无法消除，僵尸状态</li>
<li>W: 没有足够的记忆体分页可分配</li>
<li>&lt;: 高优先序的行程</li>
<li>N: 低优先序的行程</li>
<li>L: 有记忆体分页分配并锁在记忆体内 (实时系统或捱A I/O)</li>
</ul>
</li>
<li>START: 进程开始时间</li>
<li>TIME: 执行的时间</li>
<li>COMMAND:所执行的指令</li>
</ul>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:29;--cols:66"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示系统中全部的进程信息，含详细信息</span></span>
<span class="line"><span class="x-cmd">ps</span> aux </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 结合输出重定向，将当前进程信息保留备份至指定文件</span></span>
<span class="line"><span class="x-cmd">ps</span> aux &gt; backup.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 结合管道操作符，将当前系统运行状态中指定进程信息过滤出来</span></span>
<span class="line"><span class="x-cmd">ps</span> -ef | grep ssh</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 结合管道操作符，将当前系统运行状态中指定用户的进程信息过滤出来</span></span>
<span class="line"><span class="x-cmd">ps</span> -u root</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 结合管道操作符与sort命令，依据处理器使用量（第三列）情况降序排序</span></span>
<span class="line"><span class="x-cmd">ps</span> aux | <span class="hljs-built_in">sort</span> -rnk 3</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 结合管道操作符与sort命令，依据内存使用量（第四列）情况降序排序</span></span>
<span class="line"><span class="x-cmd">ps</span> aux | <span class="hljs-built_in">sort</span> -rnk 4</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示所有进程信息，连同命令行</span></span>
<span class="line"><span class="x-cmd">ps</span> -ef</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示指定用户信息</span></span>
<span class="line"><span class="x-cmd">ps</span> -u root </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示 php 的进程</span></span>
<span class="line"><span class="x-cmd">ps</span> -ef | grep php</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示进程信息</span></span>
<span class="line"><span class="x-cmd">ps</span> -A </span></code></pre></div>
<h4 id="kill命令">📚kill命令</h4>
<p>📔杀死进程。</p>
<p>🔖Linux系统中如需结束某个进程，既可以使用如service或systemctl的管理命令来结束服务，也可以使用kill命令直接结束进程信息。如使用kill命令后进程并没有被结束，则可以使用信号9进行强制杀死动作。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-l</td>
<td>列出系统支持的信号</td>
</tr>
<tr>
<td>-s</td>
<td>指定向进程发送的信号</td>
</tr>
<tr>
<td>-a</td>
<td>不限制命令名和进程号的对应关系</td>
</tr>
<tr>
<td>-p</td>
<td>不发送任何信号</td>
</tr>
</tbody></table></div>
<p><strong>最常用的信号是：</strong></p>
<ul>
<li>1 (HUP)：重新加载进程。</li>
<li>9 (KILL)：杀死一个进程。</li>
<li>15 (TERM)：正常停止一个进程。</li>
</ul>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:45"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 列出系统支持的全部信号列表</span></span>
<span class="line"><span class="hljs-built_in">kill</span> -l</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 结束某个指定的进程（数字为对应的PID值）</span></span>
<span class="line"><span class="hljs-built_in">kill</span> 1518</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 强制结束某个指定的进程（数字为对应的PID值）</span></span>
<span class="line"><span class="hljs-built_in">kill</span> -9 1518</span></code></pre></div>
<h4 id="pstree命令">📚pstree命令</h4>
<p>📔以树状图显示进程。</p>
<p>🔖Linux系统中pstree命令的英文全称是“process tree”，树状图将会以 pid (如果有指定) 或是以 init 这个基本行程为根 (root)，如果有指定使用者 id，则树状图会只显示该使用者所拥有的行程。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示每个程序的完整指令，包含路径，参数或是常驻服务的标示</td>
</tr>
<tr>
<td>-c</td>
<td>不使用精简标示法</td>
</tr>
<tr>
<td>-G</td>
<td>使用VT100终端机的列绘图字符</td>
</tr>
<tr>
<td>-h</td>
<td>列出树状图时，特别标明现在执行的程序</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:58"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 特别表明在运行的进程</span></span>
<span class="line"><span class="x-cmd">pstree</span> -apnh <span class="hljs-comment"># 显示进程间的关系</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 同时显示用户名称</span></span>
<span class="line"><span class="x-cmd">pstree</span> -u <span class="hljs-comment"># 显示用户名称</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示当前所有进程的进程号和进程id</span></span>
<span class="line"><span class="x-cmd">pstree</span> -p</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示所有进程的所有详细信息，遇到相同的进程名可以压缩显示</span></span>
<span class="line"><span class="x-cmd">pstree</span>  -a</span></code></pre></div>
<h4 id="top命令">📚top命令</h4>
<p>📔 实时显示系统运行状态。</p>
<p>🔖top命令的功能是用于实时显示系统运行状态，包含处理器、内存、服务、进程等重要资源信息。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>q</td>
<td>没有任何延迟的显示速度，如果使用者是有 superuser 的权限，则 top 将会以最高的优先序执行</td>
</tr>
<tr>
<td>c</td>
<td>切换显示模式，共有两种模式，一是只显示执行档的名称，另一种是显示完整的路径与名称</td>
</tr>
<tr>
<td>S</td>
<td>累积模式，会将己完成或消失的子进程 ( dead child process ) 的 CPU time 累积起来</td>
</tr>
<tr>
<td>s</td>
<td>安全模式，将交谈式指令取消, 避免潜在的危机</td>
</tr>
<tr>
<td>b</td>
<td>批次档模式，搭配 “n” 参数一起使用，可以用来将 top 的结果输出到档案内</td>
</tr>
<tr>
<td>-d &lt;秒&gt;</td>
<td>改变显示的更新速度</td>
</tr>
<tr>
<td>-s</td>
<td>安全模式，不允许交互式指令</td>
</tr>
<tr>
<td>-i</td>
<td>不显示任何闲置或僵死的行程</td>
</tr>
<tr>
<td>-n</td>
<td>设定显示的总次数，完成后将会自动退出</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:56"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 以默认格式显示系统运行信息</span></span>
<span class="line"><span class="x-cmd">top</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以默认格式显示系统运行信息，但提供完整的进程路径及名称</span></span>
<span class="line"><span class="x-cmd">top</span> -c  </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 以批处理模式显示程序信息</span></span>
<span class="line"><span class="x-cmd">top</span> -b</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定每隔5秒刷新一次信息</span></span>
<span class="line"><span class="x-cmd">top</span> -d 5</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 设定总显示次数为3回，随后自动退出命令</span></span>
<span class="line"><span class="x-cmd">top</span> -n 3</span></code></pre></div>
<h4 id="netstat命令">📚netstat命令</h4>
<p>📔 显示网络状态。</p>
<p>🔖netstat命令来自于英文词组”network statistics“的缩写，其功能是用于显示各种网络相关信息，例如网络连接状态、路由表信息、接口状态、NAT、多播成员等等。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>显示所有连线中的Socket</td>
</tr>
<tr>
<td>-p</td>
<td>显示正在使用Socket的程序识别码和程序名称</td>
</tr>
<tr>
<td>-l</td>
<td>仅列出在监听的服务状态</td>
</tr>
<tr>
<td>-t</td>
<td>显示TCP传输协议的连线状况</td>
</tr>
<tr>
<td>-u</td>
<td>显示UDP传输协议的连线状况</td>
</tr>
<tr>
<td>-i</td>
<td>显示网络界面信息表单</td>
</tr>
<tr>
<td>-r</td>
<td>显示路由表信息</td>
</tr>
<tr>
<td>-n</td>
<td>直接使用IP地址，不通过域名服务器</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:17;--cols:43"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 显示系统网络状态中的所有连接信息</span></span>
<span class="line"><span class="x-cmd">netstat</span> -a</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示系统网络状态中的UDP连接信息</span></span>
<span class="line"><span class="x-cmd">netstat</span> -nu</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示系统网络状态中的UDP连接端口号使用信息</span></span>
<span class="line"><span class="x-cmd">netstat</span> -apu </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示网卡当前状态信息</span></span>
<span class="line"><span class="x-cmd">netstat</span> -i </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示网络路由表状态信息</span></span>
<span class="line"><span class="x-cmd">netstat</span> -r</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 找到某个服务所对应的连接信息</span></span>
<span class="line"><span class="x-cmd">netstat</span> -ap | grep ssh</span></code></pre></div>
<h3 id="定时常用命令">定时常用命令</h3>
<h4 id="crontab命令">📚crontab命令</h4>
<p>📔管理定时计划任务。</p>
<p>🔖crontab命令来自于英文词组“cron table”的缩写。定时计划任务，就计划好的任务，到了时间就会自动执行，在Linux系统中的crond是一个定时计划任务服务，用户只要能够按照正确的格式（分、时、日、月、星期、命令）写入到配置文件中，那么就会按照预定的周期时间自动的执行下去，而crontab命令则是用于配置的工具名称。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-e</td>
<td>编辑任务</td>
</tr>
<tr>
<td>-l</td>
<td>列出任务</td>
</tr>
<tr>
<td>-r</td>
<td>删除任务</td>
</tr>
<tr>
<td>-u</td>
<td>指定用户名字</td>
</tr>
</tbody></table></div>
<p><strong>时间格式：</strong> <code>f1 f2 f3 f4 f5 program</code></p>
<ul>
<li>其中 f1 是表示分钟，f2 表示小时，f3 表示一个月份中的第几日，f4 表示月份，f5 表示一个星期中的第几天。program 表示要执行的程序。</li>
<li>当 f1 为 <em>时表示每分钟都要执行 program，f2 为</em> 时表示每小时都要执行程序，其余类推。</li>
<li>当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行，f2 为 a-b 时表示从第 a 到第 b 小时都要执行，其余类推。</li>
<li>当 f1 为 <em>/n 时表示每 n 分钟个时间间隔执行一次，f2 为</em> /n 表示每 n 小时个时间间隔执行一次，其余类推。</li>
<li>当 f1 为 a, b, c,… 时表示第 a, b, c,… 分钟要执行，f2 为 a, b, c,… 时表示第 a, b, c…个小时要执行，其余类推。</li>
</ul>
<p>📃参考实例</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:34;--cols:136;--fold:18" data-lines="34"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">每一分钟执行一次 /bin/ls</span>
<span class="line">* * * * * /bin/ls</span>
<span class="line"></span>
<span class="line">在 12 月内, 每天的早上 6 点到 12 点，每隔 3 个小时 0 分钟执行一次 /usr/bin/backup</span>
<span class="line">0 6-12/3 * 12 * /usr/bin/backup</span>
<span class="line"></span>
<span class="line">周一到周五每天下午 5:00 寄一封信给 alex@domain.name：</span>
<span class="line">0 17 * * 1-5 mail -s <span class="hljs-string">&quot;hi&quot;</span> alex@domain.name &lt; /tmp/maildata</span>
<span class="line"></span>
<span class="line">每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;haha&quot;</span></span>
<span class="line">20 0-23/2 * * * <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;haha&quot;</span></span>
<span class="line"></span>
<span class="line">0 */2 * * * /sbin/service httpd restart  意思是每两个小时重启一次apache </span>
<span class="line"></span>
<span class="line">50 7 * * * /sbin/service sshd start  意思是每天7：50开启ssh服务 </span>
<span class="line"></span>
<span class="line">50 22 * * * /sbin/service sshd stop  意思是每天22：50关闭ssh服务 </span>
<span class="line"></span>
<span class="line">0 0 1,15 * * fsck /home  每月1号和15号检查/home 磁盘 </span>
<span class="line"></span>
<span class="line">1 * * * * /home/bruce/backup  每小时的第一分执行 /home/bruce/backup这个文件 </span>
<span class="line"></span>
<span class="line">00 03 * * 1-5 find /home <span class="hljs-string">&quot;*.xxx&quot;</span> -mtime +4 -<span class="hljs-built_in">exec</span> <span class="hljs-built_in">rm</span> {} ;  每周一至周五3点钟，在目录/home中，查找文件名为*.xxx的文件，并删除4天前的文件。</span>
<span class="line"></span>
<span class="line">30 6 */10 * * <span class="hljs-built_in">ls</span>  意思是每月的1、11、21、31日是的6：30执行一次<span class="hljs-built_in">ls</span>命令</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 管理当前用户的计划任务</span></span>
<span class="line"><span class="x-cmd">crontab</span> -e  </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 管理指定用户的计划任务</span></span>
<span class="line"><span class="x-cmd">crontab</span> -e -u user1</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查看当前用户的已有计划任务列表</span></span>
<span class="line"><span class="x-cmd">crontab</span> -l </span></code></pre></div>
<h3 id="软件包管理">软件包管理</h3>
<h4 id="rpm命令">📚rpm命令</h4>
<p>📔RPM软件包管理器。</p>
<p>🔖rpm命令来自于英文词组”RedHat Package Manager“的缩写，中文译为红帽软件包管理器，其功能是用于在Linux系统下对软件包进行安装、卸载、查询、验证、升级等工作。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-a</td>
<td>查询所有的软件包</td>
</tr>
<tr>
<td>-e</td>
<td>卸载软件包</td>
</tr>
<tr>
<td>-f</td>
<td>查询文件或命令属于哪个软件包</td>
</tr>
<tr>
<td>-i</td>
<td>安装软件包</td>
</tr>
<tr>
<td>-l</td>
<td>显示软件包的文件列表</td>
</tr>
<tr>
<td>-p</td>
<td>查询指定的rpm软件包</td>
</tr>
<tr>
<td>-q</td>
<td>查询软件包</td>
</tr>
<tr>
<td>-h</td>
<td>安装软件包时列出标记</td>
</tr>
<tr>
<td>-R</td>
<td>显示软件包的依赖关系</td>
</tr>
<tr>
<td>-s</td>
<td>显示文件状态，本参数需配合”-l”参数使用</td>
</tr>
<tr>
<td>-U</td>
<td>升级软件包</td>
</tr>
<tr>
<td>-v</td>
<td>显示命令执行过程</td>
</tr>
<tr>
<td>-vv</td>
<td>详细显示指令执行过程</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 正常安装软件包</span></span>
<span class="line"><span class="x-cmd">rpm</span> -ivh cockpit-185-2.el8.x86_64.rpm </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示系统已安装过的全部RPM软件包</span></span>
<span class="line"><span class="x-cmd">rpm</span> -qa</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 查询某个软件的安装路径</span></span>
<span class="line"><span class="x-cmd">rpm</span> -ql cockpit</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 卸载某个通过RPM软件包安装的服务</span></span>
<span class="line"><span class="x-cmd">rpm</span> -evh cockpit</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 升级某个软件包</span></span>
<span class="line"><span class="x-cmd">rpm</span> -Uvh cockpit-185-2.el8.x86_64.rpm </span></code></pre></div>
<h4 id="yum命令">📚yum命令</h4>
<p>📔基于RPM的软件包管理器。</p>
<p>🔖yum命令来自于英文词组”YellowdogUpdater,Modified“的缩写，其功能是用于在Linux系统中基于RPM技术进行软件包的管理工作。yum技术通用于RHEL、CentOS、Fedora、OpenSUSE等主流系统，可以让系统管理人员交互式的自动化更新和管理软件包，实现从指定服务器自动下载、更新、删除软件包的工作。</p>
<p>🔖yum软件仓库及命令能够自动处理软件依赖关系，一次性安装所需全部软件，无需繁琐的操作。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-h</td>
<td>显示帮助信息</td>
</tr>
<tr>
<td>-y</td>
<td>对所有的提问都回答“yes”</td>
</tr>
<tr>
<td>-c</td>
<td>指定配置文件</td>
</tr>
<tr>
<td>-q</td>
<td>安静模式</td>
</tr>
<tr>
<td>-v</td>
<td>详细模式</td>
</tr>
<tr>
<td>-t</td>
<td>检查外部错误</td>
</tr>
<tr>
<td>-d</td>
<td>设置调试等级（0-10）</td>
</tr>
<tr>
<td>-e</td>
<td>设置错误等级（0-10）</td>
</tr>
<tr>
<td>-R</td>
<td>设置yum处理一个命令的最大等待时间</td>
</tr>
<tr>
<td>-C</td>
<td>完全从缓存中运行，而不去下载或者更新任何头文件</td>
</tr>
<tr>
<td>install</td>
<td>安装rpm软件包</td>
</tr>
<tr>
<td>update</td>
<td>更新rpm软件包</td>
</tr>
<tr>
<td>check-update</td>
<td>检查是否有可用的更新rpm软件包</td>
</tr>
<tr>
<td>remove</td>
<td>删除指定的rpm软件包</td>
</tr>
<tr>
<td>list</td>
<td>显示软件包的信息</td>
</tr>
<tr>
<td>search</td>
<td>检查软件包的信息</td>
</tr>
<tr>
<td>info</td>
<td>显示指定的rpm软件包的描述信息和概要信息</td>
</tr>
<tr>
<td>clean</td>
<td>清理yum过期的缓存</td>
</tr>
<tr>
<td>shell</td>
<td>进入yum的shell提示符</td>
</tr>
<tr>
<td>resolvedep</td>
<td>显示rpm软件包的依赖关系</td>
</tr>
<tr>
<td>localinstall</td>
<td>安装本地的rpm软件包</td>
</tr>
<tr>
<td>localupdate</td>
<td>显示本地rpm软件包进行更新</td>
</tr>
<tr>
<td>deplist</td>
<td>显示rpm软件包的所有依赖关系</td>
</tr>
</tbody></table></div>
<p>📃参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:20;--cols:28"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 清理原有的软件仓库信息缓存</span></span>
<span class="line"><span class="x-cmd">yum</span> clean all</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 建立最新的软件仓库信息缓存</span></span>
<span class="line"><span class="x-cmd">yum</span> makecache</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 安装指定的服务及相关软件包</span></span>
<span class="line"><span class="x-cmd">yum</span> install httpd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 更新指定的服务及相关软件包</span></span>
<span class="line"><span class="x-cmd">yum</span> update httpd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 卸载指定的服务及相关软件包</span></span>
<span class="line"><span class="x-cmd">yum</span> remove httpd</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示可安装的软件包组列表</span></span>
<span class="line"><span class="x-cmd">yum</span> grouplist </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 显示指定服务的软件信息</span></span>
<span class="line"><span class="x-cmd">yum</span> info httpd</span></code></pre></div>
<h2 id="shell编程">🌐 SHELL编程</h2>
<h3 id="概述-2">概述</h3>
<p>🖋 Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。</p>
<p>🖊 Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。</p>
<p>🖌 Shell 脚本（shell script），是一种为 shell 编写的脚本程序。</p>
<p>🖍 Shell 编程跟 JavaScript、php 编程一样，只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。</p>
<p><strong>📃 Linux 的 Shell 种类众多，常见的有：</strong></p>
<p>Bourne Shell（/usr/bin/sh或/bin/sh）</p>
<p>Bourne Again Shell（/bin/bash）</p>
<p>C Shell（/usr/bin/csh）</p>
<p>K Shell（/usr/bin/ksh）</p>
<p>Shell for Root（/sbin/sh）</p>
<p>……</p>
<p><strong>📃 sh/bash/csh/Tcsh/ksh/pdksh等shell的区别：</strong></p>
<p>👉 <strong>sh(全称 Bourne Shell)：</strong> 是UNIX最初使用的 shell，而且在每种 UNIX 上都可以使用。</p>
<p>👉 <strong>Bourne Shell：</strong> 在 shell 编程方面相当优秀，但在处理与用户的交互方面做得不如其他几种 shell。</p>
<p>👉 <strong>bash(全称 Bourne Again Shell)：</strong> LinuxOS 默认的，它是 Bourne Shell 的扩展。 与 Bourne Shell 完全兼容，并且在 Bourne Shell 的基础上增加了很多特性。可以提供命令补全，命令编辑和命令历史等功能。它还包含了很多 C Shell 和 Korn Shell 中的优点，有灵活和强大的编辑接口，同时又很友好的用户界面。</p>
<p>👉 <strong>csh(全称 C Shell)：</strong> 是一种比 Bourne Shell更适合的变种 Shell，它的语法与 C 语言很相似。</p>
<p>👉 <strong>Tcsh：</strong> 是 Linux 提供的 C Shell 的一个扩展版本。Tcsh 包括命令行编辑，可编程单词补全，拼写校正，历史命令替换，作业控制和类似 C 语言的语法，他不仅和 Bash Shell 提示符兼容，而且还提供比 Bash Shell 更多的提示符参数。</p>
<p>👉 <strong>ksh(全称 Korn Shell)：</strong> 集合了 C Shell 和 Bourne Shell 的优点并且和 Bourne Shell 完全兼容。</p>
<p>👉 <strong>pdksh：</strong> 是 Linux 系统提供的 ksh 的扩展。pdksh 支持人物控制，可以在命令行上挂起，后台执行，唤醒或终止程序。</p>
<p><strong>📃 Linux 提供的 Shell 解析器有</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo ~]$ <span class="hljs-built_in">cat</span> /etc/shells</span>
<span class="line">/bin/sh</span>
<span class="line">/bin/bash</span>
<span class="line">/usr/bin/sh</span>
<span class="line">/usr/bin/bash</span>
<span class="line">/bin/tcsh</span>
<span class="line">/bin/csh</span></code></pre></div>
<p><strong>📃 bash 和 sh 的关系</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo bin]$ ll | grep bash</span>
<span class="line">-rwxr-xr-x. 1 root root 941880 5 月 11 2016 bash</span>
<span class="line"><span class="x-cmd">lrwxrwxrwx.</span> 1 root root 4 5 月 27 2017 sh -&gt; bash</span></code></pre></div>
<p><strong>📃 Centos 默认的解析器是 bash</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:28"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo bin]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$SHELL</span></span>
<span class="line">/bin/bash</span></code></pre></div>
<h3 id="编写shell脚本">编写shell脚本</h3>
<p>脚本以 <code>#!/bin/bash</code> 开头（指定解析器）</p>
<p><code>#!</code>告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序。</p>
<p>打开文本编辑器(可以使用 vi/vim 命令来创建文件)，新建一个文件 helloworld.sh，扩展名为 sh（sh代表shell），扩展名并不影响脚本执行。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:39"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">touch</span> helloworld.sh</span>
<span class="line">[root@Demo shells]$ vim helloworld.sh</span>
<span class="line"></span>
<span class="line">// 在 helloworld.sh 中输入如下内容</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">#!/bin/bash</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;helloworld&quot;</span></span></code></pre></div>
<p><code>#!</code> 是一个约定的标记，它告诉系统这个脚本需要什么解释器来执行，即使用哪一种 Shell。</p>
<p><code>echo</code> 命令用于向窗口输出文本。</p>
<h3 id="脚本的常用执行方式">脚本的常用执行方式</h3>
<p><strong>📝 采用 bash 或 sh+脚本的相对路径或绝对路径（不用赋予脚本+x 权限）</strong></p>
<p>📃 sh+脚本的相对路径</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ sh ./helloworld.sh</span>
<span class="line"><span class="x-cmd">Helloworld</span></span></code></pre></div>
<p>📃 sh+脚本的绝对路径</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ sh /home/shells/helloworld.sh</span>
<span class="line"><span class="x-cmd">helloworld</span></span></code></pre></div>
<p>📃 bash+脚本的相对路径</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:40"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ bash ./helloworld.sh</span>
<span class="line"><span class="x-cmd">Helloworld</span></span></code></pre></div>
<p>📃 bash+脚本的绝对路径</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:51"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ bash /home/shells/helloworld.sh</span>
<span class="line"><span class="x-cmd">Helloworld</span></span></code></pre></div>
<p>🖇 这种执行方法，本质是 bash 解析器帮你执行脚本，所以脚本本身不需要执权限。</p>
<p><strong>📝 采用输入脚本的绝对路径或相对路径执行脚本（必须具有可执行权限+x）</strong></p>
<p>📃 首先要赋予 helloworld.sh 脚本的+x 权限</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:42"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">chmod</span> +x helloworld.sh</span></code></pre></div>
<p>📃 相对路径</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:35"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ ./helloworld.sh</span>
<span class="line"><span class="x-cmd">Helloworld</span></span></code></pre></div>
<p>📃 绝对路径</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ /home/shells/helloworld.sh</span>
<span class="line"><span class="x-cmd">Helloworld</span></span></code></pre></div>
<p>🖇 这种执行方法，本质是脚本需要自己执行，所以需要执行权限。</p>
<p><strong>📝 在脚本的路径前加上 “.” 或者 source</strong></p>
<p>📃 编写以下脚本</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">cat</span> test.sh</span>
<span class="line"><span class="hljs-comment">#!/bin/bash</span></span>
<span class="line">A=5</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$A</span></span></code></pre></div>
<p>📃 分别用 sh，bash，./ 和 . 的方式来执行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ bash test.sh</span>
<span class="line">5</span>
<span class="line">[root@Demo shells]$ sh test.sh</span>
<span class="line">5</span>
<span class="line">[root@Demo shells]$ ./test.sh</span>
<span class="line">5</span>
<span class="line">[root@Demo shells]$ . test.sh</span>
<span class="line">5</span></code></pre></div>
<p>🔗 前两种方式都是在当前 shell 中打开一个子 shell 来执行脚本内容，当脚本内容结束，则子 shell 关闭，回到父 shell 中。</p>
<p>🔗 第三种，也就是使用在脚本路径前加“.”或者 source 的方式，可以使脚本内容在当前shell 里执行，而无需打开子 shell！这也是为什么我们每次要修改完/etc/profile 文件以后，需要 source 一下的原因。</p>
<p>🔗 开子 shell 与不开子 shell 的区别就在于，环境变量的继承关系，如在子 shell 中设置的当前变量，父 shell 是不可见的。</p>
<h3 id="变量">变量</h3>
<p><strong>常用系统变量</strong></p>
<p><code>$HOME</code>、<code>$PWD</code>、<code>$SHELL</code>、<code>$USER</code>等</p>
<p>📃 查看系统变量的值</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$HOME</span></span></code></pre></div>
<p>📃 显示当前 Shell 中所有变量</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:23"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">set</span></span></code></pre></div>
<p>📍 定义变量时，变量名不加美元符号</p>
<p>📍 变量名和等号之间不能有空格</p>
<p>📍 <strong>变量名的命名须遵循规则：</strong></p>
<p>👉 命名只能使用英文字母，数字和下划线，首个字符不能以数字开头。</p>
<p>👉 中间不能有空格，可以使用下划线 _。</p>
<p>👉 不能使用标点符号。</p>
<p>👉 不能使用bash里的关键字（可用help命令查看保留关键字）。</p>
<p>📍 使用一个定义过的变量，只要在变量名前面加美元符号即可。</p>
<p>📍 变量名外面的花括号是可选的，加不加都行，加花括号是为了帮助解释器识别变量的边界 (推荐给所有变量加上花括号)。</p>
<p>📍 已定义的变量，可以被重新定义。</p>
<p>📍 <strong>变量类型：运行shell时，会同时存在三种变量：</strong></p>
<p>👉 <strong>局部变量：</strong> 局部变量在脚本或命令中定义，仅在当前shell实例中有效，其他shell启动的程序不能访问局部变量。</p>
<p>👉 <strong>环境变量：</strong> 所有的程序，包括shell启动的程序，都能访问环境变量，有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。</p>
<p>👉 <strong>shell变量：</strong> shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量，有一部分是局部变量，这些变量保证了shell的正常运行。</p>
<p>📍 只读变量：使用 <code>readonly</code>命令可以将变量定义为只读变量，只读变量的值不能被改变。</p>
<p>📍 删除变量：使用 <code>unset</code>命令可以删除变量。变量被删除后不能再次使用。unset 命令不能删除只读变量。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:49;--cols:62;--fold:18" data-lines="49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 定义变量 A</span></span>
<span class="line">[root@Demo shells]$ A=5</span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$A</span></span>
<span class="line">5</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 给变量 A 重新赋值</span></span>
<span class="line">[root@Demo shells]$ A=8</span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$A</span></span>
<span class="line">8</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 撤销变量 A</span></span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">unset</span> A</span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$A</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 声明静态的变量 B=2，不能 unset</span></span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">readonly</span> B=2</span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$B</span></span>
<span class="line">2</span>
<span class="line">[root@Demo shells]$ B=9</span>
<span class="line">-bash: B: <span class="hljs-built_in">readonly</span> variable </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 在 bash 中，变量默认类型都是字符串类型，无法直接进行数值运算</span></span>
<span class="line">[root@Demo shells]$ C=1+2</span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$C</span></span>
<span class="line">1+2</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 变量的值如果有空格，需要使用双引号或单引号括起来</span></span>
<span class="line">[root@Demo shells]$ D=I love you</span>
<span class="line">-bash: world: <span class="hljs-built_in">command</span> not found</span>
<span class="line">[root@Demo shells]$ D=<span class="hljs-string">&quot;I love you&quot;</span></span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$D</span></span>
<span class="line"><span class="x-cmd">I</span> love you</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 可把变量提升为全局环境变量，可供其他 Shell 程序使用</span></span>
<span class="line"><span class="hljs-comment"># 在 helloworld.sh 文件中增加 echo $B</span></span>
<span class="line">[root@Demo shells]$ vim helloworld.sh</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">#!/bin/bash</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;helloworld&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$B</span></span>
<span class="line"></span>
<span class="line">[root@Demo shells]$ ./helloworld.sh</span>
<span class="line"><span class="x-cmd">Helloworld</span></span>
<span class="line"><span class="hljs-comment"># 发现并没有打印输出变量 B 的值</span></span>
<span class="line"><span class="hljs-comment"># 使用 export 变量名</span></span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">export</span> B</span>
<span class="line">[root@Demo shells]$ ./helloworld.sh</span>
<span class="line"><span class="x-cmd">helloworld</span></span>
<span class="line">2</span></code></pre></div>
<h3 id="shell-注释">Shell 注释</h3>
<p>以 # 开头的行就是注释，会被解释器忽略，通过每一行加一个 # 号设置多行注释。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:22"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 这是一个注释</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">##### 开始 #####</span></span>
<span class="line"><span class="hljs-comment">#</span></span>
<span class="line"><span class="hljs-comment">#</span></span>
<span class="line"><span class="hljs-comment"># 这里可以添加描述信息</span></span>
<span class="line"><span class="hljs-comment">#</span></span>
<span class="line"><span class="hljs-comment">#</span></span>
<span class="line"><span class="hljs-comment">##### 结束  #####</span></span></code></pre></div>
<p>多行注释还可以使用以下格式</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:11"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">:&lt;&lt;<span class="hljs-string">EOF</span></span>
<span class="line"><span class="hljs-string">注释内容...</span></span>
<span class="line"><span class="hljs-string">注释内容...</span></span>
<span class="line"><span class="hljs-string">注释内容...</span></span>
<span class="line"><span class="hljs-string">EOF</span></span></code></pre></div>
<p>EOF 也可以使用其他符号</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:11"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">:&lt;&lt;<span class="hljs-string">&#x27;</span></span>
<span class="line"><span class="hljs-string">注释内容...</span></span>
<span class="line"><span class="hljs-string">注释内容...</span></span>
<span class="line"><span class="hljs-string">注释内容...</span></span>
<span class="line"><span class="hljs-string">&#x27;</span></span>
<span class="line"></span>
<span class="line">:&lt;&lt;!</span>
<span class="line">注释内容...</span>
<span class="line">注释内容...</span>
<span class="line">注释内容...</span>
<span class="line">!</span></code></pre></div>
<h3 id="shell字符串">shell字符串</h3>
<p>字符串是shell编程中最常用最有用的数据类型，字符串可以用单引号，也可以用双引号，也可以不用引号。</p>
<p><strong>单引号：</strong></p>
<p>👉 单引号里的任何字符都会原样输出，单引号字符串中的变量是无效的；</p>
<p>👉 单引号字串中不能出现单独一个的单引号（对单引号使用转义符后也不行），但可成对出现，作为字符串拼接使用。</p>
<p><strong>双引号</strong></p>
<p>👉 双引号里可以有变量。</p>
<p>👉 双引号里可以出现转义字符。</p>
<p>📃 拼接字符串</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:66"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">your_name=<span class="hljs-string">&quot;runoob&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用双引号拼接,输出结果为：hello, runoob ! hello, runoob !</span></span>
<span class="line">greeting=<span class="hljs-string">&quot;hello, &quot;</span><span class="hljs-variable">$your_name</span><span class="hljs-string">&quot; !&quot;</span></span>
<span class="line">greeting_1=<span class="hljs-string">&quot;hello, <span class="hljs-variable">${your_name}</span> !&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$greeting</span>  <span class="hljs-variable">$greeting_1</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用单引号拼接,输出结果为：hello, runoob ! hello, ${your_name} !</span></span>
<span class="line">greeting_2=<span class="hljs-string">&#x27;hello, &#x27;</span><span class="hljs-variable">$your_name</span><span class="hljs-string">&#x27; !&#x27;</span></span>
<span class="line">greeting_3=<span class="hljs-string">&#x27;hello, ${your_name} !&#x27;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$greeting_2</span>  <span class="hljs-variable">$greeting_3</span></span></code></pre></div>
<p>📃 获取字符串长度</p>
<div class="codeblock has-lines" style="--ln:2;--rows:19;--cols:88"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">string=<span class="hljs-string">&quot;abcd&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${#string}</span>   <span class="hljs-comment"># 输出 4</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 变量为数组时，${#string} 等价于 ${#string[0]}:</span></span>
<span class="line"></span>
<span class="line">string=<span class="hljs-string">&quot;abcd&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${#string[0]}</span>   <span class="hljs-comment"># 输出 4</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 计算字符长度也可是使用 length。</span></span>
<span class="line">string=<span class="hljs-string">&quot;hello,everyone my name is mingming&quot;</span>  <span class="hljs-comment"># string字符串里边有空格,所以需要添加双引号</span></span>
<span class="line"><span class="hljs-built_in">expr</span> length <span class="hljs-string">&quot;<span class="hljs-variable">$string</span>&quot;</span>   <span class="hljs-comment"># 输出:34</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用 expr 命令时，表达式中的运算符左右必须包含空格，如果不包含空格，将会输出表达式本身</span></span>
<span class="line"><span class="hljs-built_in">expr</span> 5+6     <span class="hljs-comment"># 直接输出 5+6</span></span>
<span class="line"><span class="hljs-built_in">expr</span> 5 + 6   <span class="hljs-comment"># 输出 11</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 对于某些运算符，还需要我们使用符号&quot;&quot;进行转义，否则就会提示语法错误</span></span>
<span class="line"><span class="hljs-built_in">expr</span> 5 * 6       <span class="hljs-comment"># 输出错误</span></span>
<span class="line"><span class="hljs-built_in">expr</span> 5 * 6      <span class="hljs-comment"># 输出30</span></span></code></pre></div>
<p>📃 提取子字符串</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 以下实例从字符串第 2 个字符开始截取 4 个字符</span></span>
<span class="line">string=<span class="hljs-string">&quot;runoob is a great site&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${string:1:4}</span>   <span class="hljs-comment"># 输出 unoo</span></span></code></pre></div>
<p>📃 查找子字符串</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:48"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">查找字符 i 或 o 的位置(哪个字母先出现就计算哪个)</span>
<span class="line">string=<span class="hljs-string">&quot;runoob is a great site&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> `<span class="hljs-built_in">expr</span> index <span class="hljs-string">&quot;<span class="hljs-variable">$string</span>&quot;</span> io`  <span class="hljs-comment"># 输出 4</span></span></code></pre></div>
<p>📃 字符串截取</p>
<p>0️⃣ 定义字符串</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">var=http://www.aaa.com/123.htm</span></code></pre></div>
<p>1️⃣ <code>#</code>号截取，删除左边字符，保留右边字符</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var#*//}</span> <span class="hljs-comment"># 结果是 ：www.aaa.com/123.htm</span></span></code></pre></div>
<p>📎 <code>#</code> 其中 var 是变量名，# 号是运算符，*// 表示从左边开始删除第一个 // 号及左边的所有字符，即删除 http://</p>
<p>2️⃣## 号截取，删除左边字符，保留右边字符</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var##*/}</span> <span class="hljs-comment"># 结果是 123.htm</span></span></code></pre></div>
<p>📎 <code>##*/</code> 表示从左边开始删除最后（最右边）一个 / 号及左边的所有字符，即删除 <a href="http://www.aaa.com/" target="_blank" rel="noopener">http://www.aaa.com/</a></p>
<p>3️⃣ %号截取，删除右边字符，保留左边字符</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:43"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var%/*}</span> <span class="hljs-comment"># 结果是：http://www.aaa.com</span></span></code></pre></div>
<p>📎 <code>%/*</code>表示从右边开始，删除第一个 / 号及右边的字符</p>
<p>4️⃣ %% 号截取，删除右边字符，保留左边字符</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var%%/*}</span> <span class="hljs-comment"># 结果是：http:</span></span></code></pre></div>
<p>📎 <code>%%/*</code>表示从右边开始，删除最后（最左边）一个 / 号及右边的字符</p>
<p>5️⃣ 从左边第几个字符开始，及字符的个数</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var:0:5}</span> <span class="hljs-comment"># 结果是：http:</span></span></code></pre></div>
<p>📎 其中的 0 表示左边第一个字符开始，5 表示字符的总个数。</p>
<p>6️⃣ 从左边第几个字符开始，一直到结束。</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:44"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var:7}</span> <span class="hljs-comment"># 结果是 ：www.aaa.com/123.htm</span></span></code></pre></div>
<p>📎 其中的 7 表示左边第8个字符开始，一直到结束。</p>
<p>7️⃣ 从右边第几个字符开始，及字符的个数</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var:0-7:3}</span> <span class="hljs-comment"># 结果是：123</span></span></code></pre></div>
<p>📎 其中的 0-7 表示右边算起第七个字符开始，3 表示字符的个数。</p>
<p>8️⃣ 从右边第几个字符开始，一直到结束。</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:33"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${var:0-7}</span> <span class="hljs-comment"># 结果是：123.htm</span></span></code></pre></div>
<p>📎 表示从右边第七个字符开始，一直到结束。</p>
<p>📍 左边的第一个字符是用 0 表示，右边的第一个字符用 0-1 表示。</p>
<p><strong># ## % %%</strong>:</p>
<p>👉 <code>#</code>、<code>##</code> 表示从左边开始删除。一个 <code>#</code>表示从左边删除到第一个指定的字符；两个 <code>#</code>表示从左边删除到最后一个指定的字符。</p>
<p>👉 <code>%</code>、<code>%%</code> 表示从右边开始删除。一个 <code>%</code>表示从右边删除到第一个指定的字符；两个<code>%</code> 表示从右边删除到最后一个指定的字符。</p>
<p>👉 删除包括了指定的字符本身。</p>
<p>📃 read 命令用于获取键盘输入信息</p>
<p>🔦 它的语法形式一般是：<code>read [-options] [variable...]</code></p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:62"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># -p 参数由于设置提示信息</span></span>
<span class="line"><span class="hljs-built_in">read</span> -p <span class="hljs-string">&quot;input a val:&quot;</span> a    <span class="hljs-comment">#获取键盘输入的 a 变量数字</span></span>
<span class="line"><span class="hljs-built_in">read</span> -p <span class="hljs-string">&quot;input b val:&quot;</span> b    <span class="hljs-comment">#获取键盘输入的 b 变量数字</span></span>
<span class="line">r=$[a+b]                    <span class="hljs-comment">#计算a+b的结果 赋值给r  不能有空格</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;result = <span class="hljs-variable">${r}</span>&quot;</span>        <span class="hljs-comment">#输出显示结果 r</span></span></code></pre></div>
<h3 id="传递参数">传递参数</h3>
<p>向脚本传递参数，脚本内获取参数的格式为：$n。n 代表一个数字，其中 $0 为执行的文件名（包含文件路径），1 为执行脚本的第一个参数，2 为执行脚本的第二个参数，以此类推……</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:13;--cols:28"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Shell 传递参数实例！&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;执行的文件名：<span class="hljs-variable">$0</span>&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第一个参数为：<span class="hljs-variable">$1</span>&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第二个参数为：<span class="hljs-variable">$2</span>&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第三个参数为：<span class="hljs-variable">$3</span>&quot;</span>;</span>
<span class="line"></span>
<span class="line">$ <span class="hljs-built_in">chmod</span> +x test.sh </span>
<span class="line">$ ./test.sh 1 2 3</span>
<span class="line"><span class="x-cmd">Shell</span> 传递参数实例！</span>
<span class="line">执行的文件名：./test.sh</span>
<span class="line">第一个参数为：1</span>
<span class="line">第二个参数为：2</span>
<span class="line">第三个参数为：3</span></code></pre></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>$#</td>
<td>传递到脚本的参数个数</td>
</tr>
<tr>
<td>$*</td>
<td>以一个单字符串显示所有向脚本传递的参数。如&quot;$*“用「”」括起来的情况、以&quot;$1 $2 … $n&quot;的形式输出所有参数。</td>
</tr>
<tr>
<td>$$</td>
<td>脚本运行的当前进程ID号</td>
</tr>
<tr>
<td>$!</td>
<td>后台运行的最后一个进程的ID号</td>
</tr>
<tr>
<td>$@</td>
<td>与∗ 相同，但是使用时加引号，并在引号中返回每个参数。如 &quot; *相同，但是使用时加引号，并在引号中返回每个参数。如&quot;∗相同，但是使用时加引号，并在引号中返回每个参数。如&quot;@“用「”」括起来的情况、以&quot;$1&quot; “2 &quot; … &quot; 2&quot; … &quot;2&quot;…&quot;n” 的形式输出所有参数。</td>
</tr>
<tr>
<td>$-</td>
<td>显示Shell使用的当前选项，与set命令功能相同。</td>
</tr>
<tr>
<td>$?</td>
<td>显示最后命令的退出状态。0表示没有错误，其他任何值表明有错误。</td>
</tr>
</tbody></table></div>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:40"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Shell 传递参数实例！&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第一个参数为：<span class="hljs-variable">$1</span>&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;参数个数为：<span class="hljs-variable">$#</span>&quot;</span>;</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;传递的参数作为一个字符串显示：$*&quot;</span>;</span>
<span class="line"></span>
<span class="line">$ <span class="hljs-built_in">chmod</span> +x test.sh </span>
<span class="line">$ ./test.sh 1 2 3</span>
<span class="line"><span class="x-cmd">Shell</span> 传递参数实例！</span>
<span class="line">第一个参数为：1</span>
<span class="line">参数个数为：3</span>
<span class="line">传递的参数作为一个字符串显示：1 2 3</span></code></pre></div>
<p><strong>$*与 $@</strong></p>
<p>👉 都是引用所有参数。</p>
<p>👉 只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3，，则 &quot; * &quot; 等价于 “1 2 3”（传递了一个参数），而 “@” 等价于 “1” “2” “3”（传递了三个参数）。</p>
<div class="codeblock has-lines" style="--ln:2;--rows:18;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;-- $* 演示 ---&quot;</span></span>
<span class="line"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">&quot;$*&quot;</span>; <span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-variable">$i</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;-- <span class="hljs-variable">$@</span> 演示 ---&quot;</span></span>
<span class="line"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">&quot;<span class="hljs-variable">$@</span>&quot;</span>; <span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-variable">$i</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line"></span>
<span class="line">$ <span class="hljs-built_in">chmod</span> +x test.sh </span>
<span class="line">$ ./test.sh 1 2 3</span>
<span class="line">-- $* 演示 ---</span>
<span class="line">1 2 3</span>
<span class="line">-- <span class="hljs-variable">$@</span> 演示 ---</span>
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span></code></pre></div>
<h3 id="shell-数组">Shell 数组</h3>
<p>bash支持一维数组（不支持多维数组），并且没有限定数组的大小。</p>
<p>数组元素的下标由 0 开始编号。获取数组中的元素要利用下标，下标可以是整数或算术表达式，其值应大于或等于 0。</p>
<p>在 Shell 中，用括号来表示数组，数组元素用&quot;空格&quot;符号分割开。定义数组的一般形式为： <code>数组名=(值1 值2 ... 值n)</code></p>
<p>📃 例如：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:78"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">array_name=(value0 value1 value2 value3)</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 或者</span></span>
<span class="line">array_name=(</span>
<span class="line"><span class="x-cmd">value0</span></span>
<span class="line"><span class="x-cmd">value1</span></span>
<span class="line"><span class="x-cmd">value2</span></span>
<span class="line"><span class="x-cmd">value3</span></span>
<span class="line">)</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 还可以单独定义数组的各个分量。可以不使用连续的下标，而且下标的范围没有限制。</span></span>
<span class="line">array_name[0]=value0</span>
<span class="line">array_name[1]=value1</span>
<span class="line">array_name[n]=valuen</span></code></pre></div>
<p>读取数组</p>
<p>读取数组元素值的一般格式是：<code>${数组名[下标]}</code></p>
<p>📃 例如：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:37"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">valuen=<span class="hljs-variable">${array_name[n]}</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用 @ 符号可以获取数组中的所有元素</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${array_name[@]}</span></span></code></pre></div>
<p>获取数组的长度</p>
<p>📃 例如：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 获取数组长度的方法与获取字符串长度的方法相同</span></span>
<span class="line"><span class="hljs-comment"># 取得数组元素的个数</span></span>
<span class="line">length=<span class="hljs-variable">${#array_name[@]}</span></span>
<span class="line"><span class="hljs-comment"># 或者</span></span>
<span class="line">length=<span class="hljs-variable">${#array_name[*]}</span></span>
<span class="line"><span class="hljs-comment"># 取得数组单个元素的长度</span></span>
<span class="line">lengthn=<span class="hljs-variable">${#array_name[n]}</span></span></code></pre></div>
<p><strong>关联数组</strong></p>
<p>Bash 支持关联数组，可以使用任意的字符串、或者整数作为下标来访问数组元素。关联数组使用 declare 命令来声明。</p>
<p>🔦 <strong>语法格式：</strong><code>declare -A array_name</code></p>
<ul>
<li>-A 选项就是用于声明一个关联数组。</li>
<li>关联数组的键是唯一的。</li>
</ul>
<p>📃 创建一个关联数组 site，并创建不同的键值</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:47"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">declare</span> -A site=([<span class="hljs-string">&quot;A&quot;</span>]=<span class="hljs-string">&quot;1&quot;</span> [<span class="hljs-string">&quot;B&quot;</span>]=<span class="hljs-string">&quot;2&quot;</span> [<span class="hljs-string">&quot;C&quot;</span>]=<span class="hljs-string">&quot;3&quot;</span>)</span></code></pre></div>
<p>📃 也可以先声明一个关联数组，然后再设置键和值</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:15"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">declare</span> -A site</span>
<span class="line">site[<span class="hljs-string">&quot;A&quot;</span>]=<span class="hljs-string">&quot;1&quot;</span></span>
<span class="line">site[<span class="hljs-string">&quot;B&quot;</span>]=<span class="hljs-string">&quot;2&quot;</span></span>
<span class="line">site[<span class="hljs-string">&quot;C&quot;</span>]=<span class="hljs-string">&quot;3&quot;</span></span></code></pre></div>
<p>📃 访问关联数组元素可以使用指定的键</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:42"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">declare</span> -A site</span>
<span class="line">site[<span class="hljs-string">&quot;A&quot;</span>]=<span class="hljs-string">&quot;1&quot;</span></span>
<span class="line">site[<span class="hljs-string">&quot;B&quot;</span>]=<span class="hljs-string">&quot;2&quot;</span></span>
<span class="line">site[<span class="hljs-string">&quot;C&quot;</span>]=<span class="hljs-string">&quot;3&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${site[&quot;B&quot;]}</span>  <span class="hljs-comment"># 执行脚本，输出结果：2</span></span></code></pre></div>
<p>📃 在数组前加一个感叹号<code>!</code>可以获取数组的所有键</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">declare</span> -A site</span>
<span class="line">site[<span class="hljs-string">&quot;A&quot;</span>]=<span class="hljs-string">&quot;1&quot;</span></span>
<span class="line">site[<span class="hljs-string">&quot;B&quot;</span>]=<span class="hljs-string">&quot;2&quot;</span></span>
<span class="line">site[<span class="hljs-string">&quot;C&quot;</span>]=<span class="hljs-string">&quot;3&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;数组的键为: <span class="hljs-variable">${!site[*]}</span>&quot;</span> <span class="hljs-comment"># A B C</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;数组的键为: <span class="hljs-variable">${!site[@]}</span>&quot;</span> <span class="hljs-comment"># A B C</span></span></code></pre></div>
<p>📃 数组的值也可以写入变量</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:40"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">A=1</span>
<span class="line">my_array=(<span class="hljs-variable">$A</span> B C D)</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第一个元素为: <span class="hljs-variable">${my_array[0]}</span>&quot;</span>  <span class="hljs-comment"># 1</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第二个元素为: <span class="hljs-variable">${my_array[1]}</span>&quot;</span>  <span class="hljs-comment"># b</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第三个元素为: <span class="hljs-variable">${my_array[2]}</span>&quot;</span>  <span class="hljs-comment"># c</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第四个元素为: <span class="hljs-variable">${my_array[3]}</span>&quot;</span>  <span class="hljs-comment"># d</span></span></code></pre></div>
<p>📃 字符串转数组</p>
<div class="codeblock has-lines" style="--ln:2;--rows:13;--cols:57"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">words=<span class="hljs-string">&quot;aaa bbb ccc&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">#字符串转数组，空格是分隔符</span></span>
<span class="line">array=(<span class="hljs-variable">${words// / }</span>)  </span>
<span class="line"><span class="hljs-comment">#打印数组最后一个成员</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${array[<span class="hljs-variable">${#array[*]}</span>-1]}</span>  <span class="hljs-comment"># ccc</span></span>
<span class="line"><span class="hljs-comment">#打印数组长度</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${#array[*]}</span>  <span class="hljs-comment"># 3</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">#字符串不转换为数组，在循环实现以空格为分隔符打印每个成员</span></span>
<span class="line"><span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> <span class="hljs-variable">${words}</span>; <span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-variable">${word}</span>       <span class="hljs-comment"># aaabbbccc</span></span>
<span class="line"><span class="hljs-keyword">done</span></span></code></pre></div>
<p>📃 字符串替换</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:53"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 使用 string/pattern/string 进行首个 pattern 的替换</span></span>
<span class="line">string=<span class="hljs-string">&quot;text, dummy, text, dummy&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${string/text/TEXT}</span>   <span class="hljs-comment"># TEXT, dummy, text, dummy</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 使用 string//pattern/string 进行全部 pattern 的替换</span></span>
<span class="line">string=<span class="hljs-string">&quot;text, dummy, text, dummy&quot;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-variable">${string//text/TEXT}</span>  <span class="hljs-comment"># TEXT, dummy, TEXT, dummy</span></span></code></pre></div>
<h3 id="运算符">运算符</h3>
<p>原生bash不支持简单的数学运算，但是可以通过其他命令来实现，例如 awk 和 expr，expr 最常用。</p>
<p><code>expr</code>是一款表达式计算工具，使用它能完成表达式的求值操作。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:43"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">val=`<span class="hljs-built_in">expr</span> 2 + 2`</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;两数之和为 : <span class="hljs-variable">$val</span>&quot;</span>   <span class="hljs-comment"># 两数之和为 : 4</span></span></code></pre></div>
<p>表达式和运算符之间要有空格，例如 2+2 是不对的，必须写成 2 + 2。</p>
<p><strong>算术运算符</strong></p>
<p>假定变量 a 为 10，变量 b 为 20：</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>加法</td>
<td><code>expr $a + $b</code> 结果为 30</td>
</tr>
<tr>
<td>-</td>
<td>减法</td>
<td><code>expr $a - $b</code> 结果为 -10</td>
</tr>
<tr>
<td>*</td>
<td>乘法</td>
<td><code>expr $a * $b</code> 结果为 200</td>
</tr>
<tr>
<td>/</td>
<td>除法</td>
<td><code>expr $b / $a</code> 结果为 2</td>
</tr>
<tr>
<td>%</td>
<td>取余</td>
<td><code>expr $b % $a</code> 结果为 0</td>
</tr>
<tr>
<td>=</td>
<td>赋值</td>
<td>a=$b 把变量 b 的值赋给 a</td>
</tr>
<tr>
<td>==</td>
<td>相等。用于比较两个数字，相同则返回 true</td>
<td>[ $a == $b ] 返回 false</td>
</tr>
<tr>
<td>!=</td>
<td>不相等。用于比较两个数字，不相同则返回 true</td>
<td>[ $a != $b ] 返回 true</td>
</tr>
</tbody></table></div>
<p>🔗 条件表达式要放在方括号之间，并且要有空格。[a = = a<mark><em>a</em>\</mark>b] 是错误的，必须写成 [ $a == $b ]。</p>
<p><strong>关系运算符</strong></p>
<p>假定变量 a 为 10，变量 b 为 20：</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>-eq</td>
<td>检测两个数是否相等，相等返回 true</td>
<td>[ $a -eq $b ] 返回 false</td>
</tr>
<tr>
<td>-ne</td>
<td>检测两个数是否不相等，不相等返回 true</td>
<td>[ $a -ne $b ] 返回 true</td>
</tr>
<tr>
<td>-gt</td>
<td>检测左边的数是否大于右边的，如果是，则返回 true</td>
<td>[ $a -gt $b ] 返回 false</td>
</tr>
<tr>
<td>-lt</td>
<td>检测左边的数是否小于右边的，如果是，则返回 true</td>
<td>[ $a -lt $b ] 返回 true</td>
</tr>
<tr>
<td>-ge</td>
<td>检测左边的数是否大于等于右边的，如果是，则返回 true</td>
<td>[ $a -ge $b ] 返回 false</td>
</tr>
<tr>
<td>-le</td>
<td>检测左边的数是否小于等于右边的，如果是，则返回 true</td>
<td>[ $a -le $b ] 返回 true</td>
</tr>
</tbody></table></div>
<p><strong>布尔运算符</strong></p>
<p>假定变量 a 为 10，变量 b 为 20：</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>!</td>
<td>非运算，表达式为 true 则返回 false，否则返回 true</td>
<td>[ ! false ] 返回 true</td>
</tr>
<tr>
<td>-o</td>
<td>或运算，有一个表达式为 true 则返回 true</td>
<td>[ $a -lt 20 -o $b -gt 100 ] 返回 true</td>
</tr>
<tr>
<td>-a</td>
<td>与运算，两个表达式都为 true 才返回 true</td>
<td>[ $a -lt 20 -a $b -gt 100 ] 返回 false</td>
</tr>
</tbody></table></div>
<p><strong>逻辑运算符</strong></p>
<p>假定变量 a 为 10，变量 b 为 20:</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>&amp;&amp;</td>
<td>逻辑的 AND</td>
<td>[[ $a -lt 100 &amp;&amp; $b -gt 100 ]] 返回 false</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table></div>
<p><strong>字符串运算符</strong></p>
<p>假定变量 a 为 “abc”，变量 b 为 “efg”：</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>=</td>
<td>检测两个字符串是否相等，相等返回 true</td>
<td>[ $a = $b ] 返回 false</td>
</tr>
<tr>
<td>!=</td>
<td>检测两个字符串是否不相等，不相等返回 true</td>
<td>[ $a != $b ] 返回 true</td>
</tr>
<tr>
<td>-z</td>
<td>检测字符串长度是否为0，为0返回 true</td>
<td>[ -z $a ] 返回 false</td>
</tr>
<tr>
<td>-n</td>
<td>检测字符串长度是否不为 0，不为 0 返回 true</td>
<td>[ -n “$a” ] 返回 true</td>
</tr>
<tr>
<td>$</td>
<td>检测字符串是否不为空，不为空返回 true</td>
<td>[ $a ] 返回 true</td>
</tr>
</tbody></table></div>
<p><strong>文件测试运算符</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-b file</td>
<td>检测文件是否是块设备文件，如果是，则返回 true</td>
</tr>
<tr>
<td>-c file</td>
<td>检测文件是否是字符设备文件，如果是，则返回 true</td>
</tr>
<tr>
<td>-d file</td>
<td>检测文件是否是目录，如果是，则返回 true</td>
</tr>
<tr>
<td>-f file</td>
<td>检测文件是否是普通文件（既不是目录，也不是设备文件），如果是，则返回 true</td>
</tr>
<tr>
<td>-g file</td>
<td>检测文件是否设置了 SGID 位，如果是，则返回 true</td>
</tr>
<tr>
<td>-k file</td>
<td>检测文件是否设置了粘着位(Sticky Bit)，如果是，则返回 true</td>
</tr>
<tr>
<td>-p file</td>
<td>检测文件是否是有名管道，如果是，则返回 true</td>
</tr>
<tr>
<td>-u file</td>
<td>检测文件是否设置了 SUID 位，如果是，则返回 true</td>
</tr>
<tr>
<td>-r file</td>
<td>检测文件是否可读，如果是，则返回 true</td>
</tr>
<tr>
<td>-w file</td>
<td>检测文件是否可写，如果是，则返回 true</td>
</tr>
<tr>
<td>-x file</td>
<td>检测文件是否可执行，如果是，则返回 true</td>
</tr>
<tr>
<td>-s file</td>
<td>检测文件是否为空（文件大小是否大于0），不为空返回 true</td>
</tr>
<tr>
<td>-e file</td>
<td>检测文件（包括目录）是否存在，如果是，则返回 true</td>
</tr>
</tbody></table></div>
<p>其他检查符：</p>
<ul>
<li>-S: 判断某文件是否 socket</li>
<li>-L: 检测文件是否存在并且是一个符号链接</li>
</ul>
<h3 id="echo命令-2">echo命令</h3>
<p>用于字符串的输出。命令格式：<code>echo string</code></p>
<p>📃 显示普通字符串</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;It is a test&quot;</span></span>
<span class="line"><span class="hljs-comment"># 这里的双引号完全可以省略，以下命令与上面实例效果一致</span></span>
<span class="line"><span class="hljs-built_in">echo</span> It is a <span class="hljs-built_in">test</span></span></code></pre></div>
<p>📃 显示转义字符</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:73"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&quot;</span>It is a <span class="hljs-built_in">test</span><span class="hljs-string">&quot;&quot;</span>   <span class="hljs-comment"># 结果将是:&quot;It is a test&quot;  同样，双引号也可以省略</span></span></code></pre></div>
<p>📃 显示变量</p>
<p>read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:60"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-meta">#!/bin/sh</span></span>
<span class="line"><span class="hljs-built_in">read</span> name </span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$name</span> It is a test&quot;</span></span>
<span class="line"><span class="hljs-comment"># 以上代码保存为 test.sh，name 接收标准输入的变量，结果将是:</span></span>
<span class="line">[root@www ~]# sh test.sh</span>
<span class="line"><span class="x-cmd">OK</span>                     <span class="hljs-comment">#标准输入</span></span>
<span class="line"><span class="x-cmd">OK</span> It is a <span class="hljs-built_in">test</span>        <span class="hljs-comment">#输出</span></span></code></pre></div>
<p>📃 显示换行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:29"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> -e <span class="hljs-string">&quot;OK! n&quot;</span> <span class="hljs-comment"># -e 开启转义</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;It is a test&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出结果：</span></span>
<span class="line">OK!</span>
<span class="line"></span>
<span class="line"><span class="x-cmd">It</span> is a <span class="hljs-built_in">test</span></span></code></pre></div>
<p>📃 显示不换行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:38"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-meta">#!/bin/sh</span></span>
<span class="line"><span class="hljs-built_in">echo</span> -e <span class="hljs-string">&quot;OK! c&quot;</span> <span class="hljs-comment"># -e 开启转义 c 不换行</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;It is a test&quot;</span></span>
<span class="line"><span class="hljs-comment"># 输出结果：OK! It is a test</span></span></code></pre></div>
<p>📃 显示结果定向至文件</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:28"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;It is a test&quot;</span> &gt; myfile</span></code></pre></div>
<p>📃 原样输出字符串，不进行转义或取变量(用单引号)</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:19"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;$name&quot;&#x27;</span></span>
<span class="line"><span class="hljs-comment"># 输出结果： $name&quot;</span></span></code></pre></div>
<p>📃 显示命令执行结果</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:33"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> `<span class="hljs-built_in">date</span>`  <span class="hljs-comment"># 结果将显示当前日期</span></span></code></pre></div>
<p><strong>echo输出的字符串</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>能否引用变量</th>
<th>能否引用转移符</th>
<th>能否引用文本格式符(如：换行符、制表符)</th>
</tr>
</thead>
<tbody><tr>
<td>单引号</td>
<td>否</td>
<td>否</td>
</tr>
<tr>
<td>双引号</td>
<td>能</td>
<td>能</td>
</tr>
<tr>
<td>无引号</td>
<td>能</td>
<td>能</td>
</tr>
</tbody></table></div>
<h3 id="read-命令">read 命令</h3>
<p>ead 命令一个一个词组地接收输入的参数，每个词组需要使用空格进行分隔；如果输入的词组个数大于需要的参数个数，则多出的词组将被作为整体为最后一个参数接收。</p>
<p>测试文件 test.sh 代码如下：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">read</span> firstStr secondStr</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第一个参数:<span class="hljs-variable">$firstStr</span>; 第二个参数:<span class="hljs-variable">$secondStr</span>&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 执行测试：</span></span>
<span class="line">$ sh test.sh </span>
<span class="line">一 二 三 四</span>
<span class="line">第一个参数:一; 第二个参数:二 三 四</span>
<span class="line">1234567</span>
<span class="line"><span class="hljs-built_in">read</span> -p <span class="hljs-string">&quot;请输入一段文字:&quot;</span> -n 6 -t 5 -s password</span>
<span class="line"><span class="hljs-built_in">echo</span> -e <span class="hljs-string">&quot;npassword is <span class="hljs-variable">$password</span>&quot;</span></span>
<span class="line">12</span></code></pre></div>
<p><strong>参数说明：</strong></p>
<ul>
<li>-p 输入提示文字</li>
<li>-n 输入字符长度限制(达到6位，自动结束)</li>
<li>-t 输入限时</li>
<li>-s 隐藏输入内容</li>
</ul>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:18"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ sh test.sh </span>
<span class="line">请输入一段文字:</span>
<span class="line"><span class="x-cmd">password</span> is abcdef</span></code></pre></div>
<h3 id="printf-命令">printf 命令</h3>
<p>printf 命令模仿 C 程序库（library）里的 printf() 程序。</p>
<p>printf 由 POSIX 标准所定义，因此使用 printf 的脚本比使用 echo 移植性好。</p>
<p>printf 使用引用文本或空格分隔的参数，外面可以在 printf 中使用格式化字符串，还可以制定字符串的宽度、左右对齐方式等。默认的 printf 不会像 echo 自动添加换行符，可以手动添加 n。</p>
<p>🔦 <strong>printf 命令的语法：</strong> <code>printf format-string [arguments...]</code></p>
<p><strong>参数说明：</strong></p>
<ul>
<li>format-string: 为格式控制字符串</li>
<li>arguments: 为参数列表。</li>
</ul>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:27"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Hello, Shell&quot;</span></span>
<span class="line">Hello, Shell</span>
<span class="line"></span>
<span class="line">$ <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;Hello, Shelln&quot;</span></span>
<span class="line">Hello, Shell</span>
<span class="line">12345</span>
<span class="line"><span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%-10s %-8s %-4sn&quot;</span> </span>
<span class="line"><span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%-10s %-8s %-4.2fn&quot;</span></span>
<span class="line"><span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%-10s %-8s %-4.2fn&quot;</span></span>
<span class="line"><span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%-10s %-8s %-4.2fn&quot;</span></span>
<span class="line">1234</span></code></pre></div>
<p><code>%s %c %d %f</code>都是格式替代符，<code>％s</code>输出一个字符串，<code>％d</code>整型输出，<code>％c</code>输出一个字符，<code>％f</code>输出实数，以小数形式输出。</p>
<p><code>%-10s</code>指一个宽度为 10 个字符（- 表示左对齐，没有则表示右对齐），任何字符都会被显示在 10 个字符宽的字符内，如果不足则自动以空格填充，超过也会将内容全部显示出来。</p>
<p><code>%-4.2f</code>指格式化为小数，其中 .2 指保留2位小数。</p>
<p><strong>printf 的转义</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>序列</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>a</td>
<td>警告字符，通常为ASCII的BEL字符</td>
</tr>
<tr>
<td>b</td>
<td>后退</td>
</tr>
<tr>
<td>c</td>
<td>抑制（不显示）输出结果中任何结尾的换行字符（只在%b格式指示符控制下的参数字符串中有效），而且，任何留在参数里的字符、任何接下来的参数以及任何留在格式字符串中的字符，都被忽略</td>
</tr>
<tr>
<td>f</td>
<td>换页（formfeed）</td>
</tr>
<tr>
<td>n</td>
<td>换行</td>
</tr>
<tr>
<td>r</td>
<td>回车（Carriage return）</td>
</tr>
<tr>
<td>t</td>
<td>水平制表符</td>
</tr>
<tr>
<td>v</td>
<td>垂直制表符</td>
</tr>
<tr>
<td></td>
<td>一个字面上的反斜杠字符</td>
</tr>
<tr>
<td>ddd</td>
<td>表示1到3位数八进制值的字符。仅在格式字符串中有效</td>
</tr>
<tr>
<td>ddd</td>
<td>表示1到3位的八进制值字符</td>
</tr>
</tbody></table></div>
<h3 id="test-命令">test 命令</h3>
<p>Shell中的 test 命令用于检查某个条件是否成立，它可以进行数值、字符和文件三个方面的测试。</p>
<p><strong>数值</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-eq</td>
<td>等于则为真</td>
</tr>
<tr>
<td>-ne</td>
<td>不等于则为真</td>
</tr>
<tr>
<td>-gt</td>
<td>大于则为真</td>
</tr>
<tr>
<td>-ge</td>
<td>大于等于则为真</td>
</tr>
<tr>
<td>-lt</td>
<td>小于则为真</td>
</tr>
<tr>
<td>-le</td>
<td>小于等于则为真</td>
</tr>
</tbody></table></div>
<p><strong>字符串</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>=</td>
<td>等于则为真</td>
</tr>
<tr>
<td>!=</td>
<td>不相等则为真</td>
</tr>
<tr>
<td>-z 字符串</td>
<td>字符串的长度为零则为真</td>
</tr>
<tr>
<td>-n字符串</td>
<td>字符串的长度不为零则为真</td>
</tr>
</tbody></table></div>
<p><strong>文件</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-e 文件名</td>
<td>如果文件存在则为真</td>
</tr>
<tr>
<td>-r 文件名</td>
<td>如果文件存在且可读则为真</td>
</tr>
<tr>
<td>-w 文件名</td>
<td>如果文件存在且可写则为真</td>
</tr>
<tr>
<td>-x 文件名</td>
<td>如果文件存在且可执行则为真</td>
</tr>
<tr>
<td>-s 文件名</td>
<td>如果文件存在且至少有一个字符则为真</td>
</tr>
<tr>
<td>-d 文件名</td>
<td>如果文件存在且为目录则为真</td>
</tr>
<tr>
<td>-f 文件名</td>
<td>如果文件存在且为普通文件则为真</td>
</tr>
<tr>
<td>-c 文件名</td>
<td>如果文件存在且为字符型特殊文件则为真</td>
</tr>
<tr>
<td>-b 文件名</td>
<td>如果文件存在且为块特殊文件则为真</td>
</tr>
</tbody></table></div>
<h3 id="if判断">IF判断</h3>
<p><strong>if 语法格式：</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:13"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">if</span> condition</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">    <span class="x-cmd">command1</span> </span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line">    ...</span>
<span class="line">    <span class="x-cmd">commandN</span> </span>
<span class="line"><span class="hljs-keyword">fi</span></span></code></pre></div>
<p>写成一行（适用于终端命令提示符）：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:60"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">if</span> [ $(ps -ef | grep -c <span class="hljs-string">&quot;ssh&quot;</span>) -gt 1 ]; <span class="hljs-keyword">then</span> <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;true&quot;</span>; <span class="hljs-keyword">fi</span></span></code></pre></div>
<p><strong>if else 语法格式</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:13"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">if</span> condition</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">    <span class="x-cmd">command1</span> </span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line">    ...</span>
<span class="line">    <span class="x-cmd">commandN</span></span>
<span class="line"><span class="hljs-keyword">else</span></span>
<span class="line">    <span class="hljs-built_in">command</span></span>
<span class="line"><span class="hljs-keyword">fi</span></span></code></pre></div>
<p><strong>if else-if else 语法格式</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:16"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">if</span> condition1</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">    <span class="x-cmd">command1</span></span>
<span class="line"><span class="hljs-keyword">elif</span> condition2 </span>
<span class="line"><span class="hljs-keyword">then</span> </span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line"><span class="hljs-keyword">else</span></span>
<span class="line">    <span class="x-cmd">commandN</span></span>
<span class="line"><span class="hljs-keyword">fi</span></span></code></pre></div>
<p>📍 if else 的 […] 判断语句中大于使用 -gt，小于使用 -lt。</p>
<p>📍 如果使用 ((…)) 作为判断语句，大于和小于可以直接使用 &gt; 和 &lt;。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:42;--cols:53;--fold:18" data-lines="42"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">a=10</span>
<span class="line">b=20</span>
<span class="line"><span class="hljs-keyword">if</span> [ <span class="hljs-variable">$a</span> == <span class="hljs-variable">$b</span> ]</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;a 等于 b&quot;</span></span>
<span class="line"><span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$a</span> -gt <span class="hljs-variable">$b</span> ]</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;a 大于 b&quot;</span></span>
<span class="line"><span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$a</span> -lt <span class="hljs-variable">$b</span> ]</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;a 小于 b&quot;</span></span>
<span class="line"><span class="hljs-keyword">else</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;没有符合的条件&quot;</span></span>
<span class="line"><span class="hljs-keyword">fi</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 或者 </span></span>
<span class="line"></span>
<span class="line">a=10</span>
<span class="line">b=20</span>
<span class="line"><span class="hljs-keyword">if</span> (( <span class="hljs-variable">$a</span> == <span class="hljs-variable">$b</span> ))</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;a 等于 b&quot;</span></span>
<span class="line"><span class="hljs-keyword">elif</span> (( <span class="hljs-variable">$a</span> &gt; <span class="hljs-variable">$b</span> ))</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;a 大于 b&quot;</span></span>
<span class="line"><span class="hljs-keyword">elif</span> (( <span class="hljs-variable">$a</span> &lt; <span class="hljs-variable">$b</span> ))</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;a 小于 b&quot;</span></span>
<span class="line"><span class="hljs-keyword">else</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;没有符合的条件&quot;</span></span>
<span class="line"><span class="hljs-keyword">fi</span></span>
<span class="line">12345678910111213141516171819202122232425262728293031</span>
<span class="line"><span class="hljs-comment"># 与 test 命令结合使用</span></span>
<span class="line">num1=$[2*3]</span>
<span class="line">num2=$[1+5]</span>
<span class="line"><span class="hljs-keyword">if</span> <span class="hljs-built_in">test</span> $[num1] -eq $[num2]</span>
<span class="line"><span class="hljs-keyword">then</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;两个数字相等!&#x27;</span></span>
<span class="line"><span class="hljs-keyword">else</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;两个数字不相等!&#x27;</span></span>
<span class="line"><span class="hljs-keyword">fi</span></span>
<span class="line">123456789</span></code></pre></div>
<h3 id="for-循环">for 循环</h3>
<p><strong>for循环格式</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:32"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">for</span> var <span class="hljs-keyword">in</span> item1 item2 ... itemN</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="x-cmd">command1</span></span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line">    ...</span>
<span class="line">    <span class="x-cmd">commandN</span></span>
<span class="line"><span class="hljs-keyword">done</span></span></code></pre></div>
<p><strong>或者</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:33"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">for</span> 变量 <span class="hljs-keyword">in</span> item1 item2 ... itemN</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="x-cmd">command1</span></span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line">    ...</span>
<span class="line">    <span class="x-cmd">commandN</span></span>
<span class="line"><span class="hljs-keyword">done</span></span></code></pre></div>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:21;--cols:33"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">for</span> loop <span class="hljs-keyword">in</span> 1 2 3 4 5</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;The value is: <span class="hljs-variable">$loop</span>&quot;</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line"><span class="hljs-comment"># 输出结果：</span></span>
<span class="line"><span class="x-cmd">The</span> value is: 1</span>
<span class="line"><span class="x-cmd">The</span> value is: 2</span>
<span class="line"><span class="x-cmd">The</span> value is: 3</span>
<span class="line"><span class="x-cmd">The</span> value is: 4</span>
<span class="line"><span class="x-cmd">The</span> value is: 5</span>
<span class="line"></span>
<span class="line"><span class="hljs-keyword">for</span> str <span class="hljs-keyword">in</span> This is a string</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-variable">$str</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line"><span class="hljs-comment"># 输出结果：</span></span>
<span class="line"><span class="x-cmd">This</span></span>
<span class="line"><span class="x-cmd">is</span></span>
<span class="line"><span class="x-cmd">a</span></span>
<span class="line"><span class="x-cmd">string</span></span>
<span class="line">123456789101112131415161718192021</span></code></pre></div>
<h3 id="while-语句">while 语句</h3>
<p><strong>语法格式</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:15"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">while</span> condition</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">command</span></span>
<span class="line"><span class="hljs-keyword">done</span></span></code></pre></div>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:12;--cols:18"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">while</span>(( <span class="hljs-variable">$int</span>&lt;=<span class="hljs-number">5</span> ))</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-variable">$int</span></span>
<span class="line">    <span class="hljs-built_in">let</span> <span class="hljs-string">&quot;int++&quot;</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出结果</span></span>
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span></code></pre></div>
<h3 id="until-循环">until 循环</h3>
<p>until 循环执行一系列命令直至条件为 true 时停止。</p>
<p>until 循环与 while 循环在处理方式上刚好相反。</p>
<p>一般 while 循环优于 until 循环，但在某些时候—也只是极少数情况下，until 循环更加有用。</p>
<p><strong>until 语法格式</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:106"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">until</span> condition  <span class="hljs-comment">#condition 一般为条件表达式，如果返回值为 false，则继续执行循环体内的语句，否则跳出循环。</span></span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">command</span></span>
<span class="line"><span class="hljs-keyword">done</span></span></code></pre></div>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:19;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">a=0</span>
<span class="line"></span>
<span class="line"><span class="hljs-keyword">until</span> [ ! <span class="hljs-variable">$a</span> -lt 10 ]</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">   <span class="hljs-built_in">echo</span> <span class="hljs-variable">$a</span></span>
<span class="line">   a=`<span class="hljs-built_in">expr</span> <span class="hljs-variable">$a</span> + 1`</span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出结果为：</span></span>
<span class="line">0</span>
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">8</span>
<span class="line">9</span></code></pre></div>
<h3 id="case-语句">case 语句</h3>
<p>case … esac 为多选择语句，与其他语言中的 switch … case 语句类似，是一种多分支选择结构，每个 case 分支用右圆括号开始，用两个分号 ;; 表示 break，即执行结束，跳出整个 case … esac 语句，esac（就是 case 反过来）作为结束标记。</p>
<p><strong>语法格式</strong></p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:12"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-keyword">case</span> 值 <span class="hljs-keyword">in</span></span>
<span class="line">模式1)</span>
<span class="line">    <span class="x-cmd">command1</span></span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line">    ...</span>
<span class="line">    <span class="x-cmd">commandN</span></span>
<span class="line">    ;;</span>
<span class="line">模式2)</span>
<span class="line">    <span class="x-cmd">command1</span></span>
<span class="line">    <span class="x-cmd">command2</span></span>
<span class="line">    ...</span>
<span class="line">    <span class="x-cmd">commandN</span></span>
<span class="line">    ;;</span>
<span class="line"><span class="hljs-keyword">esac</span></span></code></pre></div>
<p>case 取值后面必须为单词 in，每一模式必须以右括号结束。取值可以为变量或常数，匹配发现取值符合某一模式后，其间所有命令开始执行直至 ;; 。</p>
<p>取值将检测匹配的每一个模式。一旦模式匹配，则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式，使用星号 * 捕获该值，再执行后面的命令。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:15;--cols:43"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;输入 1 到 4 之间的数字:&#x27;</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;你输入的数字为:&#x27;</span></span>
<span class="line"><span class="hljs-built_in">read</span> aNum</span>
<span class="line"><span class="hljs-keyword">case</span> <span class="hljs-variable">$aNum</span> <span class="hljs-keyword">in</span></span>
<span class="line">    1)  <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;你选择了 1&#x27;</span></span>
<span class="line">    ;;</span>
<span class="line">    2)  <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;你选择了 2&#x27;</span></span>
<span class="line">    ;;</span>
<span class="line">    3)  <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;你选择了 3&#x27;</span></span>
<span class="line">    ;;</span>
<span class="line">    4)  <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;你选择了 4&#x27;</span></span>
<span class="line">    ;;</span>
<span class="line">    *)  <span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;你没有输入 1 到 4 之间的数字&#x27;</span></span>
<span class="line">    ;;</span>
<span class="line"><span class="hljs-keyword">esac</span></span></code></pre></div>
<h3 id="break-和-continue">break 和 continue</h3>
<p>break 命令允许跳出所有循环（终止执行后面的所有循环）。</p>
<p>continue 命令与 break 命令类似，它不会跳出所有循环，仅仅跳出当前循环。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:29;--cols:49"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-meta">#!/bin/bash</span></span>
<span class="line"><span class="hljs-keyword">while</span> :</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">&quot;输入 1 到 5 之间的数字:&quot;</span></span>
<span class="line">    <span class="hljs-built_in">read</span> aNum</span>
<span class="line">    <span class="hljs-keyword">case</span> <span class="hljs-variable">$aNum</span> <span class="hljs-keyword">in</span></span>
<span class="line">        1|2|3|4|5) <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;你输入的数字为 <span class="hljs-variable">$aNum</span>!&quot;</span></span>
<span class="line">        ;;</span>
<span class="line">        *) <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;你输入的数字不是 1 到 5 之间的!&quot;</span></span>
<span class="line">            <span class="hljs-built_in">break</span></span>
<span class="line">        ;;</span>
<span class="line">    <span class="hljs-keyword">esac</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line">12345678910111213</span>
<span class="line"><span class="hljs-comment">#!/bin/bash</span></span>
<span class="line"><span class="hljs-keyword">while</span> :</span>
<span class="line"><span class="hljs-keyword">do</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">&quot;输入 1 到 5 之间的数字: &quot;</span></span>
<span class="line">    <span class="hljs-built_in">read</span> aNum</span>
<span class="line">    <span class="hljs-keyword">case</span> <span class="hljs-variable">$aNum</span> <span class="hljs-keyword">in</span></span>
<span class="line">        1|2|3|4|5) <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;你输入的数字为 <span class="hljs-variable">$aNum</span>!&quot;</span></span>
<span class="line">        ;;</span>
<span class="line">        *) <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;你输入的数字不是 1 到 5 之间的!&quot;</span></span>
<span class="line">            <span class="hljs-built_in">continue</span></span>
<span class="line">            <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;结束&quot;</span></span>
<span class="line">        ;;</span>
<span class="line">    <span class="hljs-keyword">esac</span></span>
<span class="line"><span class="hljs-keyword">done</span></span>
<span class="line">1234567891011121314</span></code></pre></div>
<h3 id="函数">函数</h3>
<h4 id="basename">basename</h4>
<p><strong>基本语法：</strong> <code>basename [string / pathname] [suffix]</code></p>
<p>basename 命令会删掉所有的前缀包括最后一个（‘/’）字符，然后将字符串显示出来。</p>
<p>basename 可以理解为取路径里的文件名称.</p>
<p>suffix 为后缀，如果 suffix 被指定了，basename 会将 pathname 或 string 中的 suffix 去掉。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:58"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 截取该/home/user1/banzhang.txt 路径的文件名称</span></span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">basename</span> /home/user1/banzhang.txt</span>
<span class="line"><span class="x-cmd">banzhang.txt</span></span>
<span class="line">[root@Demo shells]$ <span class="hljs-built_in">basename</span> /home/user1/banzhang.txt .txt</span>
<span class="line"><span class="x-cmd">banzhang</span></span></code></pre></div>
<h4 id="dirname">dirname</h4>
<p>dirname 文件绝对路径</p>
<p>从给定的包含绝对路径的文件名中去除文件名，然后返回剩下的路径。</p>
<p>dirname 可以理解为取文件路径的绝对路径名称。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:55"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 获取 banzhang.txt 文件的路径</span></span>
<span class="line">[atguigu@hadoop101 ~]$ <span class="hljs-built_in">dirname</span> /home/user1/banzhang.txt</span>
<span class="line">/home/user1</span></code></pre></div>
<h4 id="自定义函数">自定义函数</h4>
<p><strong>定义格式</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:25"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[ <span class="hljs-keyword">function</span> ] funname [()]</span>
<span class="line"></span>
<span class="line">{</span>
<span class="line"></span>
<span class="line">    action;</span>
<span class="line"></span>
<span class="line">    [<span class="hljs-built_in">return</span> int;]</span>
<span class="line"></span>
<span class="line">}</span></code></pre></div>
<p>可以带function fun() 定义，也可以直接fun() 定义,不带任何参数。</p>
<p>参数返回，可以显示加：return 返回，如果不加，将以最后一条命令运行结果，作为返回值。 return后跟数值n(0-255）。</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:33;--cols:52;--fold:18" data-lines="33"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-function"><span class="hljs-title">demoFun</span></span>(){</span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;这是我的第一个 shell 函数!&quot;</span></span>
<span class="line">}</span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;-----函数开始执行-----&quot;</span></span>
<span class="line"><span class="x-cmd">demoFun</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;-----函数执行完毕-----&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出结果</span></span>
<span class="line">-----函数开始执行-----</span>
<span class="line">这是我的第一个 shell 函数!</span>
<span class="line">-----函数执行完毕-----</span>
<span class="line">1234567891011</span>
<span class="line"><span class="hljs-function"><span class="hljs-title">funWithReturn</span></span>(){</span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;这个函数会对输入的两个数字进行相加运算...&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;输入第一个数字: &quot;</span></span>
<span class="line">    <span class="hljs-built_in">read</span> aNum</span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;输入第二个数字: &quot;</span></span>
<span class="line">    <span class="hljs-built_in">read</span> anotherNum</span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;两个数字分别为 <span class="hljs-variable">$aNum</span> 和 <span class="hljs-variable">$anotherNum</span> !&quot;</span></span>
<span class="line">    <span class="hljs-built_in">return</span> $((<span class="hljs-variable">$aNum</span>+<span class="hljs-variable">$anotherNum</span>))</span>
<span class="line">}</span>
<span class="line"><span class="x-cmd">funWithReturn</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;输入的两个数字之和为 $? !&quot;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出类似下面：</span></span>
<span class="line">这个函数会对输入的两个数字进行相加运算...</span>
<span class="line">输入第一个数字: </span>
<span class="line">1</span>
<span class="line">输入第二个数字: </span>
<span class="line">2</span>
<span class="line">两个数字分别为 1 和 2 !</span>
<span class="line">输入的两个数字之和为 3 !</span>
<span class="line">1234567891011121314151617181920</span></code></pre></div>
<p>在Shell中，调用函数时可以向其传递参数。在函数体内部，通过 $n 的形式来获取参数的值，例如，$1表示第一个参数，$2表示第二个参数…</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:21;--cols:86"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-function"><span class="hljs-title">funWithParam</span></span>(){</span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第一个参数为 <span class="hljs-variable">$1</span> !&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第二个参数为 <span class="hljs-variable">$2</span> !&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第十个参数为 <span class="hljs-variable">$10</span> !&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第十个参数为 <span class="hljs-variable">${10}</span> !&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;第十一个参数为 <span class="hljs-variable">${11}</span> !&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;参数总数有 <span class="hljs-variable">$#</span> 个!&quot;</span></span>
<span class="line">    <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;作为一个字符串输出所有参数 $* !&quot;</span></span>
<span class="line">}</span>
<span class="line"><span class="x-cmd">funWithParam</span> 1 2 3 4 5 6 7 8 9 34 73</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出结果：</span></span>
<span class="line">第一个参数为 1 !</span>
<span class="line">第二个参数为 2 !</span>
<span class="line">第十个参数为 10 !</span>
<span class="line">第十个参数为 34 !</span>
<span class="line">第十一个参数为 73 !</span>
<span class="line">参数总数有 11 个!</span>
<span class="line">作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># $10 不能获取第十个参数，获取第十个参数需要${10}。当n&gt;=10时，需要使用${n}来获取参数。</span></span></code></pre></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数处理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>$#</td>
<td>传递到脚本或函数的参数个数</td>
</tr>
<tr>
<td>$*</td>
<td>以一个单字符串显示所有向脚本传递的参数</td>
</tr>
<tr>
<td>$$</td>
<td>脚本运行的当前进程ID号</td>
</tr>
<tr>
<td>$!</td>
<td>后台运行的最后一个进程的ID号</td>
</tr>
<tr>
<td>$@</td>
<td>与$*相同，但是使用时加引号，并在引号中返回每个参数。</td>
</tr>
<tr>
<td>$-</td>
<td>显示Shell使用的当前选项，与set命令功能相同。</td>
</tr>
<tr>
<td>$?</td>
<td>显示最后命令的退出状态。0表示没有错误，其他任何值表明有错误。</td>
</tr>
</tbody></table></div>
<h3 id="输入-输出重定向">输入/输出重定向</h3>
<div class="table-wrap"><table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>command &gt; file</td>
<td>将输出重定向到 file。</td>
</tr>
<tr>
<td>command &lt; file</td>
<td>将输入重定向到 file。</td>
</tr>
<tr>
<td>command &gt;&gt; file</td>
<td>将输出以追加的方式重定向到 file。</td>
</tr>
<tr>
<td>n &gt; file</td>
<td>将文件描述符为 n 的文件重定向到 file。</td>
</tr>
<tr>
<td>n &gt;&gt; file</td>
<td>将文件描述符为 n 的文件以追加的方式重定向到 file。</td>
</tr>
<tr>
<td>n &gt;&amp; m</td>
<td>将输出文件 m 和 n 合并。</td>
</tr>
<tr>
<td>n &lt;&amp; m</td>
<td>将输入文件 m 和 n 合并。</td>
</tr>
<tr>
<td>&lt;&lt; tag</td>
<td>将开始标记 tag 和结束标记 tag 之间的内容作为输入。</td>
</tr>
</tbody></table></div>
<blockquote>
<p>文件描述符 0 通常是标准输入（STDIN），1 是标准输出（STDOUT），2 是标准错误输出（STDERR）。</p>
</blockquote>
<p><strong>输出重定向</strong></p>
<p><strong>语法：</strong><code>command1 &gt; file1</code></p>
<p>上面这个命令执行command1然后将输出的内容存入file1。</p>
<p>任何file1内的已经存在的内容将被新内容替代。如果要将新内容添加在文件末尾，请使用&gt;&gt;操作符。</p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:62"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;AAAAA&quot;</span> &gt; <span class="hljs-built_in">users</span></span>
<span class="line">$ <span class="hljs-built_in">cat</span> <span class="hljs-built_in">users</span></span>
<span class="line"><span class="x-cmd">AAAAA</span></span>
<span class="line">$</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 如果不希望文件内容被覆盖，可以使用 &gt;&gt; 追加到文件末尾，例如：</span></span>
<span class="line">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;AAAAA&quot;</span> &gt;&gt; <span class="hljs-built_in">users</span></span>
<span class="line">$ <span class="hljs-built_in">cat</span> <span class="hljs-built_in">users</span></span>
<span class="line"><span class="x-cmd">AAAAA</span></span>
<span class="line"><span class="x-cmd">AAAAA</span></span>
<span class="line">$</span></code></pre></div>
<p><strong>输入重定向</strong></p>
<p><strong>语法：</strong> <code>command1 &lt; file1</code></p>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:13;--cols:98"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 接着以上实例，我们需要统计 users 文件的行数,执行以下命令：</span></span>
<span class="line">$ <span class="hljs-built_in">wc</span> -l <span class="hljs-built_in">users</span></span>
<span class="line">       2 <span class="hljs-built_in">users</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">#也可以将输入重定向到 users 文件：</span></span>
<span class="line"></span>
<span class="line">$  <span class="hljs-built_in">wc</span> -l &lt; <span class="hljs-built_in">users</span></span>
<span class="line">       2        </span>
<span class="line"><span class="hljs-comment"># 上面两个例子的结果不同：第一个例子，会输出文件名；第二个不会，因为它仅仅知道从标准输入读取内容。</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 同时替换输入和输出，执行command1，从文件infile读取内容，然后将输出写入到outfile中。</span></span>
<span class="line"><span class="x-cmd">command1</span> &lt; infile &gt; outfile </span>
<span class="line">12345678910111213</span></code></pre></div>
<p><strong>一般情况下，每个 Unix/Linux 命令运行时都会打开三个文件：</strong></p>
<p>👉 标准输入文件(stdin)：stdin的文件描述符为0，Unix程序默认从stdin读取数据。</p>
<p>👉 标准输出文件(stdout)：stdout 的文件描述符为1，Unix程序默认向stdout输出数据。</p>
<p>👉 标准错误文件(stderr)：stderr的文件描述符为2，Unix程序会向stderr流中写入错误信息。</p>
<p>默认情况下，command &gt; file 将 stdout 重定向到 file，command &lt; file 将stdin 重定向到 file。</p>
<p>如果希望 stderr 重定向到 file，可以这样写：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:16"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> 2&gt;file</span></code></pre></div>
<p>如果希望 stderr 追加到 file 文件末尾，可以这样写：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:17"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> 2&gt;&gt;file</span></code></pre></div>
<p>表示标准错误文件(stderr)。如果希望将 stdout 和 stderr 合并后重定向到 file，可以这样写：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> &gt; file 2&gt;&amp;1</span></code></pre></div>
<p>或者</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:22"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> &gt;&gt; file 2&gt;&amp;1</span></code></pre></div>
<p>如果希望对 stdin 和 stdout 都重定向，可以这样写：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:24"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> &lt; file1 &gt;file2</span></code></pre></div>
<p>command 命令将 stdin 重定向到 file1，将 stdout 重定向到 file2。</p>
<p><strong>Here Document</strong></p>
<p>Here Document 是 Shell 中的一种特殊的重定向方式，用来将输入重定向到一个交互式 Shell 脚本或程序。</p>
<p>它的基本的格式如下：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:20"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">command</span> &lt;&lt; <span class="hljs-string">delimiter</span></span>
<span class="line"><span class="hljs-string">    document</span></span>
<span class="line"><span class="hljs-string">delimiter</span></span></code></pre></div>
<p>它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。</p>
<blockquote>
<p>结尾的delimiter 一定要顶格写，前面不能有任何字符，后面也不能有任何字符，包括空格和 tab 缩进。 开始的delimiter前后的空格会被忽略掉。</p>
</blockquote>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 在命令行中通过 wc -l 命令计算 Here Document 的行数：</span></span>
<span class="line">$ <span class="hljs-built_in">wc</span> -l &lt;&lt; <span class="hljs-string">EOF</span></span>
<span class="line"><span class="hljs-string">    AAA</span></span>
<span class="line"><span class="hljs-string">    BBB</span></span>
<span class="line"><span class="hljs-string">    CCC</span></span>
<span class="line"><span class="hljs-string">EOF</span></span>
<span class="line">3          <span class="hljs-comment"># 输出结果为 3 行</span></span>
<span class="line">$</span></code></pre></div>
<p>我们也可以将 Here Document 用在脚本中，例如：</p>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-meta">#!/bin/bash</span></span>
<span class="line"><span class="hljs-built_in">cat</span> &lt;&lt; <span class="hljs-string">EOF</span></span>
<span class="line"><span class="hljs-string">    AAA</span></span>
<span class="line"><span class="hljs-string">    BBB</span></span>
<span class="line"><span class="hljs-string">    CCC</span></span>
<span class="line"><span class="hljs-string">EOF</span></span>
<span class="line"><span class="hljs-comment"># 执行以上脚本，输出结果：</span></span>
<span class="line"><span class="x-cmd">AAA</span></span>
<span class="line"><span class="x-cmd">BBB</span></span>
<span class="line"><span class="x-cmd">CCC</span></span></code></pre></div>
<p><strong>/dev/null 文件</strong></p>
<p>如果希望执行某个命令，但又不希望在屏幕上显示输出结果，那么可以将输出重定向到 /dev/null</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> &gt; /dev/null</span></code></pre></div>
<p><code>/dev/null</code> 是一个特殊的文件，写入到它的内容都会被丢弃；如果尝试从该文件读取内容，那么什么也读不到。但是 /dev/null 文件非常有用，将命令的输出重定向到它，会起到&quot;禁止输出&quot;的效果。</p>
<p>如果希望屏蔽 stdout 和 stderr，可以这样写：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">command</span> &gt; /dev/null 2&gt;&amp;1</span></code></pre></div>
<blockquote>
<p>注意：0 是标准输入（STDIN），1 是标准输出（STDOUT），2 是标准错误输出（STDERR）。 这里的 2 和 &gt; 之间不可以有空格，2&gt; 是一体的时候才表示错误输出。</p>
</blockquote>
<h3 id="简单正则使用">简单正则使用</h3>
<p>正则表达式使用单个字符串来描述、匹配一系列符合某个语法规则的字符串。在很多文本编辑器里，正则表达式通常被用来检索、替换那些符合某个模式的文本。在 Linux 中，grep，sed，awk 等文本处理工具都支持通过正则表达式进行模式匹配。</p>
<p><strong>常用的特殊字符</strong></p>
<p><code>^</code>匹配一行的开头</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">cat</span> /etc/passwd | grep ^a </span>
<span class="line"><span class="hljs-comment"># 会匹配出所有以 a 开头的行</span></span></code></pre></div>
<p><code>$</code>匹配一行的结束</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:45"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">cat</span> /etc/passwd | grep t$</span>
<span class="line"><span class="hljs-comment"># 会匹配出所有以 t 结尾的行</span></span></code></pre></div>
<p><code>.</code>匹配一个任意的字符</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:47"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">cat</span> /etc/passwd | grep r..t</span>
<span class="line"><span class="hljs-comment"># 会匹配包含 rabt,rbbt,rxdt,root 等的所有行</span></span></code></pre></div>
<p><code>*</code> 不单独使用，他和上一个字符连用，表示匹配上一个字符 0 次或多次</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:47"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">cat</span> /etc/passwd | grep ro*t</span>
<span class="line"><span class="hljs-comment"># 会匹配 rt, rot, root, rooot, roooot 等所有行</span></span></code></pre></div>
<p><strong>字符区间（中括号）：<code>[ ]</code></strong></p>
<ul>
<li>[ ] 表示匹配某个范围内的一个字符</li>
<li>[6,8] ------ 匹配 6 或者 8</li>
<li>[0-9] ------ 匹配一个 0-9 的数字</li>
<li>[0-9]* ------ 匹配任意长度的数字字符串</li>
<li>[a-z] ------匹配一个 a-z 之间的字符</li>
<li>[a-z]* ------ 匹配任意长度的字母字符串</li>
<li>[a-c, e-f] ----- 匹配 a-c 或者 e-f 之间的任意字符</li>
</ul>
<p>表示转义，并不会单独使用。由于所有特殊字符都有其特定匹配模式，当我们想匹配某一特殊字符本身时（例如，我想找出所有包含 ‘$’ 的行），就会碰到困难。此时我们就要将转义字符和特殊字符连用，来表示特殊字符本身。</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:61"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">[root@Demo shells]$ <span class="hljs-built_in">cat</span> /etc/passwd | grep ‘a<span class="hljs-variable">$b</span>’ </span>
<span class="line"><span class="hljs-comment"># 就会匹配所有包含 a$b 的行。注意需要使用单引号将表达式引起来</span></span></code></pre></div>
<h3 id="文本处理">文本处理</h3>
<h4 id="cut-命令">cut 命令</h4>
<p>cut 的工作就是“剪”，具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。</p>
<p>🌊 <strong>语法：</strong> <code>cut [参数] [file]</code></p>
<p>cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。</p>
<p>如果不指定 File 参数，cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-b</td>
<td>以字节为单位进行分割。这些字节位置将忽略多字节字符边界，除非也指定了 -n 标志。</td>
</tr>
<tr>
<td>-c</td>
<td>以字符为单位进行分割。</td>
</tr>
<tr>
<td>-d</td>
<td>自定义分隔符，默认为制表符。</td>
</tr>
<tr>
<td>-f</td>
<td>-d一起使用，指定显示哪个区域。</td>
</tr>
<tr>
<td>-n</td>
<td>取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内，该字符将被写出；否则，该字符将被排除</td>
</tr>
</tbody></table></div>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># 当执行who命令时，会输出类似如下的内容：</span></span>
<span class="line">$ <span class="hljs-built_in">who</span></span>
<span class="line"><span class="x-cmd">rocrocket</span> :0           2009-01-08 11:07</span>
<span class="line"><span class="x-cmd">rocrocket</span> pts/0        2009-01-08 11:23 (:0.0)</span>
<span class="line"><span class="x-cmd">rocrocket</span> pts/1        2009-01-08 14:15 (:0.0)</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 如果我们想提取每一行的第3个字节，如下：</span></span>
<span class="line">$ <span class="hljs-built_in">who</span>|<span class="hljs-built_in">cut</span> -b 3</span>
<span class="line"><span class="x-cmd">c</span></span>
<span class="line"><span class="x-cmd">c</span></span></code></pre></div>
<h4 id="文件包含">文件包含</h4>
<p>Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。</p>
<p>语法格式：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">. filename   <span class="hljs-comment"># 注意点号(.)和文件名中间有一空格</span></span></code></pre></div>
<p>或</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:15"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">source</span> filename</span></code></pre></div>
<p>📃 参考实例</p>
<p>创建两个 shell 脚本文件。</p>
<p>1️⃣ test1.sh</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-meta">#!/bin/bash</span></span>
<span class="line">url=<span class="hljs-string">&quot;http://www.baidu.com&quot;</span></span></code></pre></div>
<p>2️⃣ test2.sh</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:67"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-meta">#!/bin/bash</span></span>
<span class="line">. ./test1.sh  <span class="hljs-comment">#使用 . 号来引用test1.sh 文件，或者 source ./test1.sh</span></span>
<span class="line"><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;百度地址：<span class="hljs-variable">$url</span>&quot;</span></span></code></pre></div>
<p>3️⃣ 为 test2.sh 添加可执行权限并执行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:20"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">chmod</span> +x test2.sh </span>
<span class="line">$ ./test2.sh </span></code></pre></div>
<p><strong>运行结果：</strong> 百度地址：<a href="http://www.runoob.com" target="_blank" rel="noopener">http://www.runoob.com</a></p>
<blockquote>
<p>被包含的文件 test1.sh 不需要可执行权限。</p>
</blockquote>
<h4 id="sed-命令">sed 命令</h4>
<p>sed 命令是利用脚本来处理文本文件。</p>
<p>sed 可依照脚本的指令来处理、编辑文本文件。</p>
<p>Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。</p>
<p>🌊 <strong>语法：</strong> <code>sed [-hnV][-e&lt;script&gt;][-f&lt;script文件&gt;][文本文件]</code></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-e &lt;script&gt;</td>
<td>以选项中指定的script来处理输入的文本文件</td>
</tr>
<tr>
<td>-f&lt;script文件&gt;</td>
<td>以选项中指定的script文件来处理输入的文本文件</td>
</tr>
<tr>
<td>-h</td>
<td>显示帮助</td>
</tr>
<tr>
<td>-n</td>
<td>仅显示script处理后的结果</td>
</tr>
<tr>
<td>-V</td>
<td>显示版本信息</td>
</tr>
</tbody></table></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>动作</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>a</td>
<td>新增， a 的后面可以接字串，而这些字串会在新的一行出现(目前的下一行)</td>
</tr>
<tr>
<td>c</td>
<td>取代， c 的后面可以接字串，这些字串可以取代 n1,n2 之间的行！</td>
</tr>
<tr>
<td>d</td>
<td>删除，因为是删除，所以 d 后面通常不接任何东西；</td>
</tr>
<tr>
<td>i</td>
<td>插入， i 的后面可以接字串，而这些字串会在新的一行出现(目前的上一行)</td>
</tr>
<tr>
<td>p</td>
<td>打印，亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行</td>
</tr>
<tr>
<td>s</td>
<td>取代，可以直接进行取代的工作！通常这个 s 的动作可以搭配正则表达式</td>
</tr>
</tbody></table></div>
<p>📃 <strong>参考实例</strong></p>
<p>0️⃣ <strong>创建一个 testfile 文件</strong></p>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">cat</span> testfile <span class="hljs-comment">#查看testfile 中的内容  </span></span>
<span class="line"><span class="x-cmd">HELLO</span> LINUX!  </span>
<span class="line"><span class="x-cmd">Linux</span> is a free unix-type opterating system.  </span>
<span class="line"><span class="x-cmd">This</span> is a linux testfile!  </span>
<span class="line"><span class="x-cmd">Linux</span> <span class="hljs-built_in">test</span> </span>
<span class="line"><span class="x-cmd">Google</span></span>
<span class="line"><span class="x-cmd">Taobao</span></span>
<span class="line"><span class="x-cmd">Runoob</span></span>
<span class="line"><span class="x-cmd">Tesetfile</span></span>
<span class="line"><span class="x-cmd">Wiki</span></span></code></pre></div>
<p><strong>以行为单位的新增/删除</strong></p>
<p>1️⃣ 在 testfile 文件的第四行后添加一行，并将结果输出到标准输出</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">sed</span> -e 4anewLine testfile </span></code></pre></div>
<p>使用 sed 命令后，输出结果如下</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ sed -e 4anewLine testfile </span>
<span class="line"><span class="x-cmd">HELLO</span> LINUX!  </span>
<span class="line"><span class="x-cmd">Linux</span> is a free unix-type opterating system.  </span>
<span class="line"><span class="x-cmd">This</span> is a linux testfile!  </span>
<span class="line"><span class="x-cmd">Linux</span> <span class="hljs-built_in">test</span> </span>
<span class="line"><span class="x-cmd">newLine</span></span>
<span class="line"><span class="x-cmd">Google</span></span>
<span class="line"><span class="x-cmd">Taobao</span></span>
<span class="line"><span class="x-cmd">Runoob</span></span>
<span class="line"><span class="x-cmd">Tesetfile</span></span>
<span class="line"><span class="x-cmd">Wiki</span></span></code></pre></div>
<p>2️⃣ 将 testfile 的内容列出并且列印行号，同时，将第 2~5 行删除</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;2,5d&#x27;</span></span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<blockquote>
<p>sed 的动作为 2,5d， d 是删除的意思，因为删除了 2-5 行，所以显示的数据就没有 2-5 行，原本应该是要下达 sed -e 才对，但没有 -e 也是可以的，同时也要注意的是， sed 后面接的动作，请以 ‘…’ 两个单引号括住</p>
</blockquote>
<p>3️⃣ 只要删除第 2 行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:35"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;2d&#x27;</span> </span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line">     3  This is a linux testfile!  </span>
<span class="line">     4  Linux <span class="hljs-built_in">test</span> </span>
<span class="line">     5  Google</span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<p>4️⃣ 要删除第 3 到最后一行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;3,$d&#x27;</span> </span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line">     2  Linux is a free unix-type opterating system.  </span></code></pre></div>
<p>5️⃣ 在第二行后(即加在第三行) 加上drink tea? 字样</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;2a drink tea&#x27;</span></span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line">     2  Linux is a free unix-type opterating system.  </span>
<span class="line"><span class="x-cmd">drink</span> tea</span>
<span class="line">     3  This is a linux testfile!  </span>
<span class="line">     4  Linux <span class="hljs-built_in">test</span> </span>
<span class="line">     5  Google</span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<p>6️⃣ 如果是要在第二行前，命令如下</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;2i drink tea&#x27;</span> </span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line"><span class="x-cmd">drink</span> tea</span>
<span class="line">     2  Linux is a free unix-type opterating system.  </span>
<span class="line">     3  This is a linux testfile!  </span>
<span class="line">     4  Linux <span class="hljs-built_in">test</span> </span>
<span class="line">     5  Google</span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<p>7️⃣如果是要增加两行以上，在第二行后面加入两行字，例如 Drink tea or … 与 drink beer?</p>
<div class="codeblock has-lines" style="--ln:2;--rows:14;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;2a Drink tea or ......</span></span>
<span class="line"><span class="hljs-string">drink beer ?&#x27;</span></span>
<span class="line"></span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line">     2  Linux is a free unix-type opterating system.  </span>
<span class="line"><span class="x-cmd">Drink</span> tea or ......</span>
<span class="line"><span class="x-cmd">drink</span> beer ?</span>
<span class="line">     3  This is a linux testfile!  </span>
<span class="line">     4  Linux <span class="hljs-built_in">test</span> </span>
<span class="line">     5  Google</span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<blockquote>
<p>每一行之间都必须要以反斜杠 来进行标记</p>
</blockquote>
<p><strong>以行为单位的替换与显示</strong></p>
<p>1️⃣ 将第 2-5 行的内容取代成为 No 2-5 number</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:40"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed <span class="hljs-string">&#x27;2,5c No 2-5 number&#x27;</span></span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line"><span class="x-cmd">No</span> 2-5 number</span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<p>2️⃣仅列出 testfile 文件内的第 5-7 行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:29"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed -n <span class="hljs-string">&#x27;5,7p&#x27;</span></span>
<span class="line">     5  Google</span>
<span class="line">     6  Taobao</span>
<span class="line">     7  Runoob</span></code></pre></div>
<p><strong>数据的搜寻并显示</strong></p>
<p>1️⃣ 搜索 testfile 有 oo 关键字的行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed -n <span class="hljs-string">&#x27;/oo/p&#x27;</span></span>
<span class="line">     5  Google</span>
<span class="line">     7  Runoob</span></code></pre></div>
<blockquote>
<p>如果 root 找到，除了输出所有行，还会输出匹配行。</p>
</blockquote>
<p><strong>数据的搜寻并删除</strong></p>
<p>1️⃣ 删除 testfile 所有包含 oo 的行，其他行输出</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed  <span class="hljs-string">&#x27;/oo/d&#x27;</span></span>
<span class="line">     1  HELLO LINUX!  </span>
<span class="line">     2  Linux is a free unix-type opterating system.  </span>
<span class="line">     3  This is a linux testfile!  </span>
<span class="line">     4  Linux <span class="hljs-built_in">test</span> </span>
<span class="line">     6  Taobao</span>
<span class="line">     8  Tesetfile</span>
<span class="line">     9  Wiki</span></code></pre></div>
<p><strong>数据的搜寻并执行命令</strong></p>
<p>1️⃣ 搜索 testfile，找到 oo 对应的行，执行后面花括号中的一组命令，每个命令之间用分号分隔，这里把 oo 替换为 kk，再输出这行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:62"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed -n <span class="hljs-string">&#x27;/oo/{s/oo/kk/;p;q}&#x27;</span>  <span class="hljs-comment"># 最后的 q 是退出</span></span>
<span class="line">     5  Gkkgle</span></code></pre></div>
<p><strong>数据的查找与替换</strong></p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:48"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-comment"># sed 的查找与替换的与 vi 命令类似，语法格式如下</span></span>
<span class="line"><span class="x-cmd">sed</span> <span class="hljs-string">&#x27;s/要被取代的字串/新的字串/g&#x27;</span></span></code></pre></div>
<p>1️⃣ 将 testfile 文件中每行第一次出现的 oo 用字符串 kk 替换</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">sed</span> -e <span class="hljs-string">&#x27;s/oo/kk/&#x27;</span> testfile</span></code></pre></div>
<p>2️⃣ g 标识符表示全局查找替换，使 sed 对文件中所有符合的字符串都被替换，修改后内容会到标准输出，不会修改原文件</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:27"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">sed</span> -e <span class="hljs-string">&#x27;s/oo/kk/g&#x27;</span> testfile</span></code></pre></div>
<p>3️⃣ 选项 i 使 sed 修改文件</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:27"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">sed</span> -i <span class="hljs-string">&#x27;s/oo/kk/g&#x27;</span> testfile</span></code></pre></div>
<p>4️⃣ 批量操作当前目录下以 test 开头的文件</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">sed</span> -i <span class="hljs-string">&#x27;s/oo/kk/g&#x27;</span> ./test*</span></code></pre></div>
<p>5️⃣ 使用 /sbin/ifconfig 查询 IP，将 IP 前，后的部分予以删除</p>
<div class="codeblock has-lines" style="--ln:2;--rows:18;--cols:84"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ /sbin/ifconfig eth0</span>
<span class="line"><span class="x-cmd">eth0</span> Link encap:Ethernet HWaddr 00:90:CC:A6:34:84</span>
<span class="line"><span class="x-cmd">inet</span> addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0</span>
<span class="line"><span class="x-cmd">inet6</span> addr: fe80::290:ccff:fea6:3484/64 Scope:Link</span>
<span class="line"><span class="x-cmd">UP</span> BROADCAST RUNNING MULTICAST MTU:1500 Metric:1</span>
<span class="line">.....(以下省略).....</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 本机的 ip 是 192.168.1.100</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将 IP 前面的部分予以删除</span></span>
<span class="line">$ /sbin/ifconfig eth0 | grep <span class="hljs-string">&#x27;inet addr&#x27;</span> | sed <span class="hljs-string">&#x27;s/^.*addr://g&#x27;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0</span></span>
<span class="line"><span class="hljs-comment"># 接下来则是删除后续的部分，即：192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 将 IP 后面的部分予以删除</span></span>
<span class="line">$ /sbin/ifconfig eth0 | grep <span class="hljs-string">&#x27;inet addr&#x27;</span> | sed <span class="hljs-string">&#x27;s/^.*addr://g&#x27;</span> | sed <span class="hljs-string">&#x27;s/Bcast.*$//g&#x27;</span></span>
<span class="line">192.168.1.100</span></code></pre></div>
<p><strong>多点编辑</strong></p>
<p>1️⃣ 一条 sed 命令，删除 testfile 第三行到末尾的数据，并把 HELLO 替换为 RUNOOB :</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:54"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">nl</span> testfile | sed -e <span class="hljs-string">&#x27;3,$d&#x27;</span> -e <span class="hljs-string">&#x27;s/HELLO/RUNOOB/&#x27;</span></span>
<span class="line">     1  RUNOOB LINUX!  </span>
<span class="line">     2  Linux is a free unix-type opterating system.  </span></code></pre></div>
<blockquote>
<p>-e 表示多点编辑，第一个编辑命令删除 testfile 第三行到末尾的数据，第二条命令搜索 HELLO 替换为 RUNOOB。</p>
</blockquote>
<p><strong>直接修改文件内容</strong></p>
<p>1️⃣ regular_express.txt 文件内容</p>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:26"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">cat</span> regular_express.txt </span>
<span class="line"><span class="x-cmd">runoob.</span></span>
<span class="line"><span class="x-cmd">google.</span></span>
<span class="line"><span class="x-cmd">taobao.</span></span>
<span class="line"><span class="x-cmd">facebook.</span></span>
<span class="line"><span class="x-cmd">zhihu-</span></span>
<span class="line"><span class="x-cmd">weibo-</span></span></code></pre></div>
<p>2️⃣ 利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !</p>
<div class="codeblock has-lines" style="--ln:1;--rows:8;--cols:39"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ sed -i <span class="hljs-string">&#x27;s/.$/!/g&#x27;</span> regular_express.txt</span>
<span class="line">$ <span class="hljs-built_in">cat</span> regular_express.txt </span>
<span class="line">runoob!</span>
<span class="line">google!</span>
<span class="line">taobao!</span>
<span class="line">facebook!</span>
<span class="line"><span class="x-cmd">zhihu-</span></span>
<span class="line">weibo-:q:q </span></code></pre></div>
<p>3️⃣利用 sed 直接在 regular_express.txt 最后一行加入 # This is a test</p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ sed -i <span class="hljs-string">&#x27;$a # This is a test&#x27;</span> regular_express.txt</span>
<span class="line">$ <span class="hljs-built_in">cat</span> regular_express.txt </span>
<span class="line">runoob!</span>
<span class="line">google!</span>
<span class="line">taobao!</span>
<span class="line">facebook!</span>
<span class="line"><span class="x-cmd">zhihu-</span></span>
<span class="line"><span class="x-cmd">weibo-</span></span>
<span class="line"><span class="hljs-comment"># This is a test</span></span></code></pre></div>
<blockquote>
<p>$ 代表的是最后一行，而 a 的动作是新增，因此该文件最后新增 # This is a test！</p>
</blockquote>
<h4 id="awk命令">awk命令</h4>
<p>AWK 是一种处理文本文件的语言，是一个强大的文本分析工具，把文件逐行的读入，以空格为默认分隔符将每行切片，切开的部分再进行分析处理。</p>
<p>AWK 是取了三位创始人 Alfred Aho，Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符。</p>
<p>🌊 <strong>语法：</strong> <code>awk [选项参数] ‘/pattern1/{action1} /pattern2/{action2}...’ filename</code></p>
<blockquote>
<p>pattern：表示 awk 在数据中查找的内容，就是匹配模式<br>action：在找到匹配内容时所执行的一系列命令</p>
</blockquote>
<div class="table-wrap"><table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-F</td>
<td>指定输入文件分隔符</td>
</tr>
<tr>
<td>-v</td>
<td>赋值一个用户定义变量</td>
</tr>
</tbody></table></div>
<p><strong>基本使用</strong></p>
<p>0️⃣ log.txt文本内容</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">2 this is a <span class="hljs-built_in">test</span></span>
<span class="line">3 Do you like awk</span>
<span class="line">This<span class="hljs-string">&#x27;s a test</span></span>
<span class="line"><span class="hljs-string">10 There are orange,apple,mongo</span></span></code></pre></div>
<p>1️⃣ <strong>用法一：</strong> <code>awk &#39;{[pattern] action}&#39; {filenames}</code> 行匹配语句 awk ‘’ 只能用单引号</p>
<p>每行按空格或TAB分割，输出文本中的1、4项</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:30"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $ awk <span class="hljs-string">&#x27;{print $1,$4}&#x27;</span> log.txt</span>
<span class="line"> 2 a</span>
<span class="line"> 3 like</span>
<span class="line"> This<span class="hljs-string">&#x27;s</span></span>
<span class="line"><span class="hljs-string"> 10 orange,apple,mongo</span></span></code></pre></div>
<p>格式化输出</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:45"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $ awk <span class="hljs-string">&#x27;{printf &quot;%-8s %-10sn&quot;,$1,$4}&#x27;</span> log.txt</span>
<span class="line"> 2        a</span>
<span class="line"> 3        like</span>
<span class="line"> This<span class="hljs-string">&#x27;s</span></span>
<span class="line"><span class="hljs-string"> 10       orange,apple,mongo</span></span></code></pre></div>
<p>2️⃣ <strong>用法二：</strong> <code>awk -F</code> -F相当于内置变量FS, 指定分割字符</p>
<p>使用&quot;,&quot;分割</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:37"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $  awk -F, <span class="hljs-string">&#x27;{print $1,$2}&#x27;</span>   log.txt</span>
<span class="line"> 2 this is a <span class="hljs-built_in">test</span></span>
<span class="line"> 3 Do you like awk</span>
<span class="line"> This<span class="hljs-string">&#x27;s a test</span></span>
<span class="line"><span class="hljs-string"> 10 There are orange apple</span></span></code></pre></div>
<p>使用内建变量分隔</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:48"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $ awk <span class="hljs-string">&#x27;BEGIN{FS=&quot;,&quot;} {print $1,$2}&#x27;</span>     log.txt</span>
<span class="line"> 2 this is a <span class="hljs-built_in">test</span></span>
<span class="line"> 3 Do you like awk</span>
<span class="line"> This<span class="hljs-string">&#x27;s a test</span></span>
<span class="line"><span class="hljs-string"> 10 There are orange apple</span></span></code></pre></div>
<p>使用多个分隔符.先使用空格分割，然后对分割结果再使用&quot;,&quot;分割</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:46"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $ awk -F <span class="hljs-string">&#x27;[ ,]&#x27;</span>  <span class="hljs-string">&#x27;{print $1,$2,$5}&#x27;</span>   log.txt</span>
<span class="line"> 2 this <span class="hljs-built_in">test</span></span>
<span class="line"> 3 Do awk</span>
<span class="line"> This<span class="hljs-string">&#x27;s a</span></span>
<span class="line"><span class="hljs-string"> 10 There apple</span></span></code></pre></div>
<p>1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣🔟</p>
<p>3️⃣ <strong>用法三：</strong> <code>awk -v</code> 设置变量</p>
<div class="codeblock has-lines" style="--ln:2;--rows:12;--cols:48"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $ awk -va=1 <span class="hljs-string">&#x27;{print $1,$1+a}&#x27;</span> log.txt</span>
<span class="line"> 2 3</span>
<span class="line"> 3 4</span>
<span class="line"> This<span class="hljs-string">&#x27;s 1</span></span>
<span class="line"><span class="hljs-string"> 10 11</span></span>
<span class="line"><span class="hljs-string">12345</span></span>
<span class="line"><span class="hljs-string"> $ awk -va=1 -vb=s &#x27;</span>{<span class="hljs-built_in">print</span> <span class="hljs-variable">$1</span>,<span class="hljs-variable">$1</span>+a,<span class="hljs-variable">$1b</span>}<span class="hljs-string">&#x27; log.txt</span></span>
<span class="line"><span class="hljs-string"> 2 3 2s</span></span>
<span class="line"><span class="hljs-string"> 3 4 3s</span></span>
<span class="line"><span class="hljs-string"> This&#x27;</span>s 1 This<span class="hljs-string">&#x27;ss</span></span>
<span class="line"><span class="hljs-string"> 10 11 10s</span></span>
<span class="line"><span class="hljs-string">12345</span></span></code></pre></div>
<p>4️⃣ <strong>用法四：</strong> <code>awk -f {awk脚本} {文件名}</code></p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:25"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"> $ awk -f cal.awk log.txt</span></code></pre></div>
<p><strong>运算符</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>= += -= *= /= %= ^= **=</td>
<td>赋值</td>
</tr>
<tr>
<td>?:</td>
<td>C条件表达式</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>&amp;&amp;</td>
<td>逻辑与</td>
</tr>
<tr>
<td>~ 和 !~</td>
<td>匹配正则表达式和不匹配正则表达式</td>
</tr>
<tr>
<td>&lt; &lt;= &gt; &gt;= != ==</td>
<td>关系运算符</td>
</tr>
<tr>
<td>空格</td>
<td>连接</td>
</tr>
<tr>
<td>+ -</td>
<td>加，减</td>
</tr>
<tr>
<td>* / %</td>
<td>乘，除与求余</td>
</tr>
<tr>
<td>+ - !</td>
<td>一元加，减和逻辑非</td>
</tr>
<tr>
<td>^ ***</td>
<td>求幂</td>
</tr>
<tr>
<td>++ –</td>
<td>增加或减少，作为前缀或后缀</td>
</tr>
<tr>
<td>$</td>
<td>字段引用</td>
</tr>
<tr>
<td>in</td>
<td>数组成员</td>
</tr>
</tbody></table></div>
<p>📃 参考实例</p>
<p>过滤第一列大于2的行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:31"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;$1&gt;2&#x27;</span> log.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出</span></span>
<span class="line">3 Do you like awk</span>
<span class="line">This<span class="hljs-string">&#x27;s a test</span></span>
<span class="line"><span class="hljs-string">10 There are orange,apple,mongo</span></span></code></pre></div>
<p>过滤第一列等于2的行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:35"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;$1==2 {print $1,$3}&#x27;</span> log.txt</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出</span></span>
<span class="line">2 is</span></code></pre></div>
<p>过滤第一列大于2并且第二列等于’Do’的行</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:50"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;$1&gt;2 &amp;&amp; $2==&quot;Do&quot; {print $1,$2,$3}&#x27;</span> log.txt </span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出</span></span>
<span class="line">3 Do you</span></code></pre></div>
<p><strong>内建变量</strong></p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>变量</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>$n</td>
<td>当前记录的第n个字段，字段间由FS分隔</td>
</tr>
<tr>
<td>$0</td>
<td>完整的输入记录</td>
</tr>
<tr>
<td>ARGC</td>
<td>命令行参数的数目</td>
</tr>
<tr>
<td>ARGIND</td>
<td>命令行中当前文件的位置(从0开始算)</td>
</tr>
<tr>
<td>ARGV</td>
<td>包含命令行参数的数组</td>
</tr>
<tr>
<td>CONVFMT</td>
<td>数字转换格式(默认值为%.6g)ENVIRON环境变量关联数组</td>
</tr>
<tr>
<td>ERRNO</td>
<td>最后一个系统错误的描述</td>
</tr>
<tr>
<td>FIELDWIDTHS</td>
<td>字段宽度列表(用空格键分隔)</td>
</tr>
<tr>
<td>FILENAME</td>
<td>当前文件名</td>
</tr>
<tr>
<td>FNR</td>
<td>各文件分别计数的行号</td>
</tr>
<tr>
<td>FS</td>
<td>字段分隔符(默认是任何空格)</td>
</tr>
<tr>
<td>IGNORECASE</td>
<td>如果为真，则进行忽略大小写的匹配</td>
</tr>
<tr>
<td>NF</td>
<td>一条记录的字段的数目</td>
</tr>
<tr>
<td>NR</td>
<td>已经读出的记录数，就是行号，从1开始</td>
</tr>
<tr>
<td>OFMT</td>
<td>数字的输出格式(默认值是%.6g)</td>
</tr>
<tr>
<td>OFS</td>
<td>输出字段分隔符，默认值与输入字段分隔符一致。</td>
</tr>
<tr>
<td>ORS</td>
<td>输出记录分隔符(默认值是一个换行符)</td>
</tr>
<tr>
<td>RLENGTH</td>
<td>由match函数所匹配的字符串的长度</td>
</tr>
<tr>
<td>RS</td>
<td>记录分隔符(默认是一个换行符)</td>
</tr>
<tr>
<td>RSTART</td>
<td>由match函数所匹配的字符串的第一个位置</td>
</tr>
<tr>
<td>SUBSEP</td>
<td>数组下标分隔符(默认值是/034)</td>
</tr>
</tbody></table></div>
<p>📃 参考实例</p>
<div class="codeblock has-lines" style="--ln:2;--rows:16;--cols:271"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;BEGIN{printf &quot;%4s %4s %4s %4s %4s %4s %4s %4s %4sn&quot;,&quot;FILENAME&quot;,&quot;ARGC&quot;,&quot;FNR&quot;,&quot;FS&quot;,&quot;NF&quot;,&quot;NR&quot;,&quot;OFS&quot;,&quot;ORS&quot;,&quot;RS&quot;;printf &quot;---------------------------------------------n&quot;} {printf &quot;%4s %4s %4s %4s %4s %4s %4s %4s %4sn&quot;,FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}&#x27;</span>  log.txt</span>
<span class="line"><span class="x-cmd">FILENAME</span> ARGC  FNR   FS   NF   NR  OFS  ORS   RS</span>
<span class="line">---------------------------------------------</span>
<span class="line"><span class="x-cmd">log.txt</span>    2    1         5    1</span>
<span class="line"><span class="x-cmd">log.txt</span>    2    2         5    2</span>
<span class="line"><span class="x-cmd">log.txt</span>    2    3         3    3</span>
<span class="line"><span class="x-cmd">log.txt</span>    2    4         4    4</span>
<span class="line">1234567</span>
<span class="line">$ awk -F<span class="hljs-string">&#x27; &#x27;</span>BEGIN{<span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%4s %4s %4s %4s %4s %4s %4s %4s %4sn&quot;</span>,<span class="hljs-string">&quot;FILENAME&quot;</span>,<span class="hljs-string">&quot;ARGC&quot;</span>,<span class="hljs-string">&quot;FNR&quot;</span>,<span class="hljs-string">&quot;FS&quot;</span>,<span class="hljs-string">&quot;NF&quot;</span>,<span class="hljs-string">&quot;NR&quot;</span>,<span class="hljs-string">&quot;OFS&quot;</span>,<span class="hljs-string">&quot;ORS&quot;</span>,<span class="hljs-string">&quot;RS&quot;</span>;<span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;---------------------------------------------n&quot;</span>} {<span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%4s %4s %4s %4s %4s %4s %4s %4s %4sn&quot;</span>,FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}<span class="hljs-string">&#x27;  log.txt</span></span>
<span class="line"><span class="hljs-string">FILENAME ARGC  FNR   FS   NF   NR  OFS  ORS   RS</span></span>
<span class="line"><span class="hljs-string">---------------------------------------------</span></span>
<span class="line"><span class="hljs-string">log.txt    2    1    &#x27;</span>    1    1</span>
<span class="line"><span class="x-cmd">log.txt</span>    2    2    <span class="hljs-string">&#x27;    1    2</span></span>
<span class="line"><span class="hljs-string">log.txt    2    3    &#x27;</span>    2    3</span>
<span class="line"><span class="x-cmd">log.txt</span>    2    4    <span class="hljs-string">&#x27;    1    4</span></span>
<span class="line"><span class="hljs-string">1234567</span></span></code></pre></div>
<p>输出顺序号 NR, 匹配文本行号</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:39"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;{print NR,FNR,$1,$2,$3}&#x27;</span> log.txt</span>
<span class="line">1 1 2 this is</span>
<span class="line">2 2 3 Do you</span>
<span class="line">3 3 This<span class="hljs-string">&#x27;s a test</span></span>
<span class="line"><span class="hljs-string">4 4 10 There are</span></span></code></pre></div>
<p>指定输出分割符</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:44"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$  awk <span class="hljs-string">&#x27;{print $1,$2,$5}&#x27;</span> OFS=<span class="hljs-string">&quot; $ &quot;</span>  log.txt</span>
<span class="line">2 $ this $ <span class="hljs-built_in">test</span></span>
<span class="line">3 $ Do $ awk</span>
<span class="line">This<span class="hljs-string">&#x27;s $ a $</span></span>
<span class="line"><span class="hljs-string">10 $ There $</span></span></code></pre></div>
<p>使用正则，字符串匹配，输出第二列包含 “th”，并打印第二列与第四列</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:75"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;$2 ~ /th/ {print $2,$4}&#x27;</span> log.txt     <span class="hljs-comment"># ~ 表示模式开始。// 中是模式。</span></span>
<span class="line"><span class="x-cmd">this</span> a</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 输出包含 &quot;re&quot; 的行</span></span>
<span class="line">$ awk <span class="hljs-string">&#x27;/re/ &#x27;</span> log.txt</span>
<span class="line">10 There are orange,apple,mongo</span></code></pre></div>
<p>忽略大小写</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:42"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;BEGIN{IGNORECASE=1} /this/&#x27;</span> log.txt</span>
<span class="line">2 this is a <span class="hljs-built_in">test</span></span>
<span class="line">This<span class="hljs-string">&#x27;s a test</span></span></code></pre></div>
<p>模式取反</p>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:40"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk <span class="hljs-string">&#x27;$2 !~ /th/ {print $2,$4}&#x27;</span> log.txt</span>
<span class="line"><span class="x-cmd">Do</span> like</span>
<span class="line"><span class="x-cmd">a</span></span>
<span class="line"><span class="x-cmd">There</span> orange,apple,mongo</span>
<span class="line"></span>
<span class="line">$ awk <span class="hljs-string">&#x27;!/th/ {print $2,$4}&#x27;</span> log.txt</span>
<span class="line"><span class="x-cmd">Do</span> like</span>
<span class="line"><span class="x-cmd">a</span></span>
<span class="line"><span class="x-cmd">There</span> orange,apple,mongo</span></code></pre></div>
<p><strong>awk脚本</strong></p>
<p>BEGIN{ 这里面放的是执行前的语句 }</p>
<p>END {这里面放的是处理完所有的行后要执行的语句 }</p>
<p>📃 参考实例</p>
<p>有这么一个文件（成绩表）：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:21"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">cat</span> score.txt</span>
<span class="line"><span class="x-cmd">Marry</span>   2143 78 84 77</span>
<span class="line"><span class="x-cmd">Jack</span>    2321 66 78 45</span>
<span class="line"><span class="x-cmd">Tom</span>     2122 48 77 71</span>
<span class="line"><span class="x-cmd">Mike</span>    2537 87 97 95</span>
<span class="line"><span class="x-cmd">Bob</span>     2415 40 57 62</span></code></pre></div>
<p>awk 脚本如下</p>
<div class="codeblock has-lines" style="--ln:2;--rows:24;--cols:74"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ <span class="hljs-built_in">cat</span> cal.awk</span>
<span class="line"><span class="hljs-comment">#!/bin/awk -f</span></span>
<span class="line"><span class="hljs-comment">#运行前</span></span>
<span class="line"><span class="x-cmd">BEGIN</span> {</span>
<span class="line">    <span class="x-cmd">math</span> = 0</span>
<span class="line">    <span class="x-cmd">english</span> = 0</span>
<span class="line">    <span class="x-cmd">computer</span> = 0</span>
<span class="line"></span>
<span class="line">    <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;NAME    NO.   MATH  ENGLISH  COMPUTER   TOTALn&quot;</span></span>
<span class="line">    <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;---------------------------------------------n&quot;</span></span>
<span class="line">}</span>
<span class="line"><span class="hljs-comment">#运行中</span></span>
<span class="line">{</span>
<span class="line">    math+=<span class="hljs-variable">$3</span></span>
<span class="line">    english+=<span class="hljs-variable">$4</span></span>
<span class="line">    computer+=<span class="hljs-variable">$5</span></span>
<span class="line">    <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;%-6s %-6s %4d %8d %8d %8dn&quot;</span>, <span class="hljs-variable">$1</span>, <span class="hljs-variable">$2</span>, <span class="hljs-variable">$3</span>,<span class="hljs-variable">$4</span>,<span class="hljs-variable">$5</span>, <span class="hljs-variable">$3</span>+<span class="hljs-variable">$4</span>+<span class="hljs-variable">$5</span></span>
<span class="line">}</span>
<span class="line"><span class="hljs-comment">#运行后</span></span>
<span class="line"><span class="x-cmd">END</span> {</span>
<span class="line">    <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;---------------------------------------------n&quot;</span></span>
<span class="line">    <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;  TOTAL:%10d %8d %8d n&quot;</span>, math, english, computer</span>
<span class="line">    <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;AVERAGE:%10.2f %8.2f %8.2fn&quot;</span>, math/NR, english/NR, computer/NR</span>
<span class="line">}</span></code></pre></div>
<p>执行结果</p>
<div class="codeblock has-lines" style="--ln:2;--rows:11;--cols:45"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line">$ awk -f cal.awk score.txt</span>
<span class="line"><span class="x-cmd">NAME</span>    NO.   MATH  ENGLISH  COMPUTER   TOTAL</span>
<span class="line">---------------------------------------------</span>
<span class="line"><span class="x-cmd">Marry</span>  2143     78       84       77      239</span>
<span class="line"><span class="x-cmd">Jack</span>   2321     66       78       45      189</span>
<span class="line"><span class="x-cmd">Tom</span>    2122     48       77       71      196</span>
<span class="line"><span class="x-cmd">Mike</span>   2537     87       97       95      279</span>
<span class="line"><span class="x-cmd">Bob</span>    2415     40       57       62      159</span>
<span class="line">---------------------------------------------</span>
<span class="line">  TOTAL:       319      393      350</span>
<span class="line">AVERAGE:     63.80    78.60    70.00</span></code></pre></div>
]]></content:encoded><category>Linux</category></item>
<item><title>后端表格导出的三种方式</title><link>https://demo1.661366.xyz/blog/backend-table-export/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/backend-table-export/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>大部分是用前两个。</description><content:encoded><![CDATA[<h3 id="大部分是用前两个">大部分是用前两个。</h3>
<p>最好POM文件配置一个，不要配置多个表格导出工具。<br>因为需要平衡依赖的版本，比较麻烦。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>特性</th>
<th>EasyPoi</th>
<th>EasyExcel (推荐)</th>
<th>Jxls</th>
</tr>
</thead>
<tbody><tr>
<td>核心思路</td>
<td>注解流。在Java Bean上加注解，自动映射。</td>
<td>性能流。注重内存优化，防止OOM，链式调用。</td>
<td>模板流。在Excel里画好样式写标签，填充数据。</td>
</tr>
<tr>
<td>上手难度</td>
<td>⭐⭐⭐⭐⭐ (极简)</td>
<td>⭐⭐⭐⭐ (简单)</td>
<td>⭐⭐⭐ (需要学模板语法)</td>
</tr>
<tr>
<td>样式复杂度</td>
<td>弱 (适合标准表格)</td>
<td>中 (主要靠代码控制策略)</td>
<td>强(所见即所得，适合复杂报表)</td>
</tr>
<tr>
<td>大数据性能</td>
<td>差 (容易内存溢出)</td>
<td>极强(百万数据秒级导出)</td>
<td>一般 (基于POI，大数据量慢)</td>
</tr>
<tr>
<td>维护状态</td>
<td>更新较慢，文档一般</td>
<td>阿里维护，社区活跃</td>
<td>稳定，但更新不频繁</td>
</tr>
<tr>
<td>最适合场景</td>
<td>后台管理系统的简单CRUD列表导出。</td>
<td>海量数据导出、高并发场景、不想OOM。</td>
<td>复杂的财务报表、发票、合同等格式要求严格的场景。</td>
</tr>
</tbody></table></div>
]]></content:encoded><category>Java</category></item>
<item><title>网站套CF问题-重定向次数过多</title><link>https://demo1.661366.xyz/blog/cloudflare-too-many-redirects/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/cloudflare-too-many-redirects/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>解决方法参考 该网页无法正常运作</description><content:encoded><![CDATA[<h2 id="解决方法参考"><a href="https://www.wppop.com/wordpress-cloudflare-redirect-too-many-times-error-solution.html" target="_blank" rel="noopener">解决方法参考</a></h2>
<h2 id="该网页无法正常运作">该网页无法正常运作</h2>
<p>blog.331633.xyz 将您重定向的次数过多。</p>
<h4 id="cloudflare-导致-重定向的次数过多-的常见原因">CloudFlare 导致“重定向的次数过多”的常见原因</h4>
<ul>
<li>HTTP 与 HTTPS 重定向冲突：当站点设置了从 HTTP 到 HTTPS 的重定向，而 CloudFlare 同时也启用了“总是使用 HTTPS” 或 “ 自动 HTTPS 重写” 等功能时，可能会导致无限循环重定向。例如，当一个HTTP请求到达服务器时，服务器可能将其重定向到HTTPS，而CloudFlare又尝试将这个HTTPS请求重定向回HTTP，从而形成循环。</li>
<li>CloudFlare 的 SSL 配置错误：CloudFlare 提供了多种 SSL 模式，包括“灵活”、“全站”、“全站（严格）”等。如果站点服务器的 SSL 配置与 CloudFlare 的 SSL 模式不匹配，可能会导致重定向错误。比如，使用“灵活”模式时，CloudFlare 会与浏览器通过 HTTPS 通信，但与源服务器则通过 HTTP 通信，如果源服务器也设置了从HTTP到HTTPS的重定向，就会出现循环重定向。</li>
<li>网站配置问题：一些网站管理系统，如 WordPress，可能会在后台强制执行某些URL结构或协议。如果这些设置与 CloudFlare 的配置不一致，可能会导致重定向循环。</li>
</ul>
<p>更改为完全：</p>
<figure class="media is-image" style="--ar:1.6917;--w:1015px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-480.avif 480w, https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-800.avif 800w, https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-1015.avif 1015w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-1015.webp" srcset="https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-480.webp 480w, https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-800.webp 800w, https://demo1.661366.xyz/assets/img/b0302eea3d230864-b0302eea3d-1015.webp 1015w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1015" height="600" alt="" fetchpriority="high" decoding="async"></picture></figure>
<p>然后清理：缓存</p>
<figure class="media is-image" style="--ar:3.4031;--w:1562px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-480.avif 480w, https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-800.avif 800w, https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-1200.avif 1200w, https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-1562.avif 1562w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-1200.webp" srcset="https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-480.webp 480w, https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-800.webp 800w, https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-1200.webp 1200w, https://demo1.661366.xyz/assets/img/4fa6cacc91a647ca-4fa6cacc91-1562.webp 1562w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1562" height="459" alt="" loading="lazy" decoding="async"></picture></figure>
]]></content:encoded><category>Cloudflare</category><category>Nginx</category></item>
<item><title>Debian系统代理设置</title><link>https://demo1.661366.xyz/blog/debian-proxy/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/debian-proxy/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>一、手动修改配置文件 1、使用编辑器打开网络配置文件：sudo nano /etc/environment</description><content:encoded><![CDATA[<p>一、手动修改配置文件<br>1、使用编辑器打开网络配置文件：sudo nano /etc/environment</p>
<p>2、在文件的末尾添加下面的内容，以设置HTTP和HTTPS代理服务器：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:56"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">http_proxy=&quot;http://proxy_server_ip:proxy_server_port/&quot;</span>
<span class="line">https_proxy=&quot;https://proxy_server_ip:proxy_server_port/&quot;</span></code></pre></div>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:47"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">export</span> http_proxy=<span class="hljs-string">&quot;http://192.168.3.108:10808&quot;</span></span>
<span class="line"><span class="hljs-built_in">export</span> https_proxy=<span class="hljs-string">&quot;http://192.168.3.108:10808&quot;</span></span>
<span class="line"><span class="x-cmd">or</span> </span>
<span class="line">http_proxy=<span class="hljs-string">&quot;http://192.168.3.108:10808&quot;</span></span>
<span class="line">https_proxy=<span class="hljs-string">&quot;http://192.168.3.108:10808&quot;</span></span></code></pre></div>
<p>将&quot;proxy_server_ip&quot;替换为实际的代理服务器IP地址，将&quot;proxy_server_port&quot;替换为代理服务器的端口号。如果代理服务器需要身份验证，可以在URL中添加用户名和密码。</p>
<p>3、保存并关闭文件，然后重新加载环境变量：source /etc/environment</p>
<p>4、配置APT软件包管理器使用代理服务器：sudo nano /etc/apt/apt.conf</p>
<p>5、添加以下行，将APT指令路由到代理服务器上：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:67"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">Acquire::http::Proxy &quot;http://proxy_server_ip:proxy_server_port/&quot;;</span>
<span class="line">Acquire::https::Proxy &quot;https://proxy_server_ip:proxy_server_port/&quot;;</span></code></pre></div>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:51"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">Acquire::http::Proxy &quot;http://192.168.3.108:10808&quot;</span>
<span class="line">Acquire::https::Proxy &quot;http://192.168.3.108:10808&quot;;</span></code></pre></div>
<p>同样，将&quot;proxy_server_ip&quot;替换为实际的代理服务器IP地址，将&quot;proxy_server_port&quot;替换为代理服务器的端口号。</p>
<p>6、保存并关闭文件。</p>
<p>7、测试连通性</p>
<p>curl -v <a href="http://www.google.com" target="_blank" rel="noopener">http://www.google.com</a></p>
]]></content:encoded><category>Debian</category><category>Linux</category></item>
<item><title>Debian13初始化</title><link>https://demo1.661366.xyz/blog/debian13-init/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/debian13-init/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>1、用户提权 问题：debian 未出现在 sudoers 文件中</description><content:encoded><![CDATA[<h2 id="1-用户提权">1、用户提权</h2>
<h3 id="问题-debian-未出现在-sudoers-文件中">问题：debian 未出现在 sudoers 文件中</h3>
<p>先通过普通用户登入，ssh连接或者Debian本地终端也行，输入以下命令：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:4"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">su -</span></code></pre></div>
<p>此时我们输入root账号的密码就不会提示不是 sudoers 文件了，但是这并不能解决问题，我们继续：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:6"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">visudo</span></code></pre></div>
<p>此时会打开一个文件，我们往下翻找到root ALL=(ALL:ALL) ALL 这行字符，在它的下一行我们输入一行文本：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:38"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">你当前用于提权的账号 ALL=(ALL:ALL) ALL</span></code></pre></div>
<p>如果你当前用于提权的账号是aaaa，那么实际文本内容就是：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:22"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">aaaa ALL=(ALL:ALL) ALL</span></code></pre></div>
<p>输入完毕后CTRL + X保存退出。</p>
<p>此时我们在通过sudo su提权，输入root密码就不会提示不是 sudoers 文件了。</p>
<h2 id="2-提示更换介质">2、提示更换介质</h2>
<p>更换介质:请把标有&quot;Debian GNU/Linux 13.1.0 Trixie _ Official amd64 DVD Binary-1 with firmware 20250906-10:24&quot; 的盘片插入驱动器“/media/cdrom/”再按「回车」键</p>
<p>nano /etc/apt/sources.list</p>
<p>然后注释掉第一行即可，左后sourch</p>
<figure class="media is-image" style="--ar:1.8213;--w:907px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-480.avif 480w, https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-800.avif 800w, https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-907.avif 907w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-907.webp" srcset="https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-480.webp 480w, https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-800.webp 800w, https://demo1.661366.xyz/assets/img/b336701a5ee571bf-b336701a5e-907.webp 907w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="907" height="498" alt="" loading="lazy" decoding="async"></picture></figure>
<h2 id="3-安装中文输入法-不要安装ibus的-会冲突">3、安装中文输入法（不要安装iBus的，会冲突）</h2>
<p><a href="https://rime.im/download" target="_blank" rel="noopener">https://rime.im/download</a></p>
<p>sudo apt-get install fcitx-rime</p>
<p>安装词库</p>
<p><a href="https://github.com/gaboolic/rime-frost" target="_blank" rel="noopener">https://github.com/gaboolic/rime-frost</a><br>下载完成，将文件解压到 /home/debian/.config/ibus/rime/ 即可</p>
<p>完成之后，按F4 选择白霜</p>
<p>您好，这是一个非常好的问题。对于 Debian 13 上的 KDE 桌面环境，我强烈推荐您安装 Fcitx5-rime。</p>
<p>下面我为您详细解释原因，并提供安装步骤。</p>
<h4 id="为什么在kde上选择-fcitx5-rime-而不是-ibus-rime">为什么在KDE上选择 Fcitx5-rime 而不是 IBus-rime？</h4>
<p>简单来说：Fcitx5 与 KDE (Qt) 的集成度和契合度远高于 IBus。</p>
<ol>
<li>桌面环境技术栈匹配：<ul>
<li>KDE Plasma 是基于 Qt 工具包构建的。</li>
<li>Fcitx5 是一个非常现代的输入法框架，它对 Qt5/6 的支持非常出色，可以完美地融入KDE的界面和设置中。</li>
<li>IBus 虽然也能在KDE下使用，但它主要是为 GNOME 等基于 GTK 工具包的桌面环境设计的。在KDE中使用IBus，有时会出现界面风格不统一、配置入口不直观、甚至是一些小Bug。</li>
</ul>
</li>
<li>外观和体验：<ul>
<li>使用Fcitx5，您的输入法图标、候选词窗口等界面元素可以和您的KDE主题（如Breeze）保持完全一致，看起来就像是系统原生的一部分。</li>
<li>IBus的界面是GTK风格的，在纯净的KDE环境下可能会显得有些突兀。</li>
</ul>
</li>
<li>配置便利性：<ul>
<li>安装 kde-config-fcitx5 这个包之后，Fcitx5的配置项会直接集成到KDE的“系统设置”中，您可以在一个统一的地方管理所有系统选项，非常方便。</li>
<li>IBus的配置通常需要通过它自己的独立程序 ibus-setup 来完成。</li>
</ul>
</li>
<li>现代化和性能：<ul>
<li>Fcitx5 是 Fcitx 框架的最新版本，代码更现代化，设计更模块化，并且对 Wayland（下一代显示服务器协议）的支持也通常更好。</li>
<li>它在性能和资源占用方面也表现优异。</li>
</ul>
</li>
</ol>
<p>总而言之，虽然两者都能用，但选择 Fcitx5-rime 能给您在KDE上带来“原汁原味”、无缝集成的最佳体验。</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>特性</th>
<th>Fcitx5</th>
<th>IBus</th>
</tr>
</thead>
<tbody><tr>
<td>推荐环境</td>
<td>KDE (Qt), GNOME, XFCE (通用性强)</td>
<td>GNOME (GTK), XFCE</td>
</tr>
<tr>
<td>KDE集成度</td>
<td>极高，可集成到系统设置，外观统一</td>
<td>一般，通常需要独立配置，外观可能不统一</td>
</tr>
<tr>
<td>现代化</td>
<td>是(Fcitx的最新版本，支持Wayland更好)</td>
<td>相对旧一些，但仍在积极维护</td>
</tr>
<tr>
<td>配置</td>
<td>图形化配置工具，可与KDE系统设置集成</td>
<td>独立的图形化配置工具ibus-setup</td>
</tr>
</tbody></table></div>
<hr>
<h4 id="如何在-debian-13-kde-上安装和配置-fcitx5-rime">如何在 Debian 13 KDE 上安装和配置 Fcitx5-rime</h4>
<p>请按照以下步骤操作，这会比安装IBus更顺畅。</p>
<h5>步骤一：安装 Fcitx5 相关软件包</h5>
<p>打开 Konsole (KDE的终端)，执行以下命令：</p>
<p>Bash</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:93"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">sudo</span> apt update</span>
<span class="line"><span class="hljs-built_in">sudo</span> apt install fcitx5 fcitx5-rime fcitx5-chinese-addons fcitx5-configtool kde-config-fcitx5</span></code></pre></div>
<ul>
<li>fcitx5: Fcitx5 框架核心。</li>
<li>fcitx5-rime: Rime 输入法引擎的 Fcitx5 版本。</li>
<li>fcitx5-chinese-addons: 一些中文相关的增强插件，比如云拼音、繁简转换等。</li>
<li>fcitx5-configtool: Fcitx5 的图形化配置工具（Qt界面）。</li>
<li>kde-config-fcitx5: （关键包） 将Fcitx5配置集成到KDE系统设置的模块。</li>
</ul>
<h5>步骤二：配置环境变量 (重要)</h5>
<p>为了让系统全局使用 Fcitx5，需要设置几个环境变量。最推荐的方法是编辑 /etc/environment 文件。</p>
<ol>
<li>用nano编辑器打开该文件：<br>Bash <code>sudo nano /etc/environment</code></li>
<li>在文件的末尾添加以下三行（用于兼容X11,不添加也行）： <code>GTK_IM_MODULE=fcitx QT_IM_MODULE=fcitx XMODIFIERS=@im=fcitx</code></li>
<li>按 Ctrl+X，然后按 Y，最后按 Enter 保存并退出。</li>
</ol>
<h5>步骤三：重启并配置输入法</h5>
<ol>
<li>重启电脑。这是让环境变量和新服务完全生效最简单可靠的方法。</li>
<li>重启后，Fcitx5 应该已经自动启动了。您会在屏幕右下角的系统托盘看到一个键盘图标。</li>
<li>配置输入法：<ul>
<li>右键点击系统托盘的键盘图标，选择“配置”。</li>
<li>或者，打开KDE的“系统设置” -&gt; “区域设置” -&gt; “输入法”。（因为安装了 kde-config-fcitx5，所以这里会有入口）</li>
<li>在打开的窗口中，您会看到一个“可用输入法”列表和一个“当前输入法”列表。</li>
<li>默认“当前输入法”里可能只有一个“键盘”选项。您需要在左侧“可用输入法”列表中找到 “Rime”，然后点击向右的箭头 &gt; 将它添加到右侧的“当前输入法”列表中。</li>
<li>您可以使用上下箭头调整输入法的顺序。</li>
</ul>
</li>
</ol>
<p>现在，您就可以使用 Ctrl+Space (默认切换快捷键) 来激活Rime输入法并开始打字了。</p>
<h5>步骤四：配置输入法皮肤</h5>
<ol>
<li><a href="https://github.com/topics/fcitx5-theme" target="_blank" rel="noopener">https://github.com/topics/fcitx5-theme</a><br>可下载皮肤<br>或<br>下载搜狗皮肤，然后进行转换<br><a href="https://github.com/RadND/ssfconv?tab=readme-ov-file" target="_blank" rel="noopener">https://github.com/RadND/ssfconv?tab=readme-ov-file</a></li>
<li><a href="https://ziheng5.github.io/2025/04/20/fcitx5-theme/" target="_blank" rel="noopener">https://ziheng5.github.io/2025/04/20/fcitx5-theme/</a></li>
</ol>
<p>比较推荐的：</p>
<p><a href="https://github.com/Reverier-Xu/Fluent-fcitx5" target="_blank" rel="noopener">https://github.com/Reverier-Xu/Fluent-fcitx5</a></p>
<p><a href="https://github.com/witt-bit/fcitx5-theme-macos12" target="_blank" rel="noopener">https://github.com/witt-bit/fcitx5-theme-macos12</a></p>
<p>下面上教程</p>
<blockquote>
<p>适用所有发行版</p>
</blockquote>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:66"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="x-cmd">git</span> <span class="hljs-built_in">clone</span> https://github.com/witt-bit/fcitx5-theme-macos12.git</span>
<span class="line"></span>
<span class="line"><span class="hljs-comment"># 深色</span></span>
<span class="line"><span class="hljs-built_in">cp</span> -r fcitx5-theme-macos12/macos12-dark /usr/share/fcitx5/themes/</span>
<span class="line"><span class="hljs-comment"># 亮色</span></span>
<span class="line"><span class="hljs-built_in">cp</span> -r fcitx5-theme-macos12/macos12-light /usr/share/fcitx5/themes/</span></code></pre></div>
]]></content:encoded><category>Debian</category><category>Linux</category></item>
<item><title>IDEA的Debugger启动速度优化</title><link>https://demo1.661366.xyz/blog/idea-debugger-startup/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/idea-debugger-startup/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>通过对比你提供的 Debug模式 和 正常运行（Run）模式 的启动命令，我发现了导致速度变慢的“罪魁祸首”。</description><content:encoded><![CDATA[<p>通过对比你提供的 Debug模式 和 正常运行（Run）模式 的启动命令，我发现了导致速度变慢的“罪魁祸首”。</p>
<p>核心原因：你开启了性能分析（Profiler）或者 Java Flight Recorder (JFR)。</p>
<h3 id="1-证据分析-the-smoking-gun">1. 证据分析 (The Smoking Gun)</h3>
<p>请看你 Debug 启动命令 中的这一行参数：</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:220"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">-agentpath:D:\SystemFiles\UserTemp\idea_libasyncProfiler_dll_temp_folder7\libasyncProfiler.dll=version,jfr,event=wall,interval=10ms,cstack=no,file=C:\Users\wzy\IdeaSnapshots\ManagementApplication_2025_12_24_170106.jfr...</span>
<span class="line"></span>
<span class="line"></span></code></pre></div>
<p>发生了什么：</p>
<ul>
<li>libasyncProfiler.dll: 这是 IntelliJ 的异步性能分析器。</li>
<li>interval=10ms: 它每 10 毫秒就会采样一次堆栈信息。</li>
<li>file=...jfr: 它正在将你的启动过程录制成一个巨大的性能快照文件。</li>
</ul>
<p>为什么这会导致变慢：<br>在 Debug 模式下，JVM 本身就已经因为 JDWP（调试协议）而关闭了一些优化。如果你在此基础上再挂载一个每 10ms 采样一次并频繁写入磁盘的 Profiler，CPU 和 I/O 开销会剧增，导致启动时间从 27 秒拖慢到 69 秒。</p>
<h4 id="1-1-我个人观点">1.1 我个人观点</h4>
<p>可能也和内存带宽相关，处理器主频。硬盘影响应该不大，因为现在的硬盘4K性能都差不多</p>
<hr>
<h3 id="2-解决方案">2. 解决方案</h3>
<p>请按顺序检查以下几处设置，将其关闭：</p>
<h4 id="方案一-检查-启动前-选项或配置-最可能的原因">方案一：检查“启动前”选项或配置 (最可能的原因)</h4>
<ol>
<li>点击顶部工具栏的项目名称下拉框，选择 &quot;Edit Configurations...&quot; (编辑配置)。</li>
<li>在右侧的选项卡中，查看 &quot;Logs&quot; 或 &quot;Startup/Connection&quot; 或者是 &quot;Profiling&quot; (取决于你的 IDEA 版本，2025.2 版本可能整合得更深)。</li>
<li>关键检查： 看看是否勾选了类似 &quot;Start with Java Flight Recorder&quot; (随 JFR 启动) 或者 &quot;Attach async profiler&quot; 的选项。如果有，取消勾选。</li>
</ol>
<h4 id="方案二-检查你点击的按钮">方案二：检查你点击的按钮</h4>
<p>你可能没有点击常规的“Debug”图标（那个绿色的小虫子），而是点击了旁边的 &quot;Debug with &#39;Profiler&#39;&quot; 或者 &quot;Profile &#39;Application&#39;&quot; 按钮。</p>
<ul>
<li>操作： 确保你只点击单纯的 Debug (绿色虫子) 图标。</li>
</ul>
<h4 id="方案三-检查断点窗口-隐形杀手">方案三：检查断点窗口（隐形杀手）</h4>
<p>虽然你说断点都取消了，但有时候“方法断点”或“异常断点”会隐身。</p>
<ol>
<li>按快捷键 Ctrl + Shift + F8 打开断点面板。</li>
<li>Java Method Breakpoints (方法断点)：这是性能杀手。如果有任何红色的菱形断点，必须删除。哪怕是静音的，它们也会导致 JVM 强制解释执行而不是 JIT 编译。</li>
<li>Java Exception Breakpoints (异常断点)：检查是否勾选了 &quot;Any Exception&quot;。Spring 启动时内部会抛出很多被捕获的异常，如果 Debugger 每次都去检查，速度会极慢。</li>
</ol>
<h4 id="方案四-关闭-async-stack-traces-异步堆栈捕获-实测这个好使">方案四：关闭 Async Stack Traces (异步堆栈捕获) （实测这个好使）</h4>
<p>你的日志中还有这一行：-javaagent:...captureAgent\debugger-agent.jar。这是 IDEA 用来在调试异步代码（如 Coroutines, Reactor, CompletableFutures）时显示完整堆栈的。这也会增加开销。</p>
<ol>
<li>打开 Settings (设置) -&gt; Build, Execution, Deployment -&gt; Debugger -&gt; Async Stack Traces。</li>
<li>尝试取消勾选 (Uncheck) &quot;Instrumenting agent&quot;。</li>
<li>如果不关键，也可以在 Debugger 设置里把 &quot;Coroutine&quot; 相关的调试支持关掉。</li>
</ol>
<hr>
<h3 id="3-总结">3. 总结</h3>
<p>你的 Debug 模式变慢不是因为代码问题，而是因为你实际上是在进行“全量性能分析+调试”。</p>
<p>立刻要做的一步：<br>请进入 Edit Configurations，确保没有勾选任何与 Profiler 或 Java Flight Recorder 相关的选项，然后只点击纯净的 Debug 图标 启动。</p>
<p>这样你的启动时间应该能回到 30-35 秒左右的正常 Debug 水平。</p>
<p>要关闭 Reactor Debug 模式（通常表现为日志中出现 Enabling stacktrace debugging via onOperatorDebug），可以通过以下几种方式排查和处理。</p>
<h3 id="1-检查代码中的显式开启">1. 检查代码中的显式开启</h3>
<p>这是最常见的原因。请在你的项目中全局搜索以下代码，找到后删除或注释掉：</p>
<p>Java</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:24"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">Hooks.onOperatorDebug();</span></code></pre></div>
<p>通常这行代码会出现在：</p>
<ul>
<li>应用的 main 方法中（SpringApplication.run 之前）。</li>
<li>某个 @Configuration 配置类中。</li>
<li>static 静态代码块中。</li>
</ul>
<p>注意：这个方法会极大地降低性能（捕获所有操作符的堆栈信息），严禁在生产环境（Production）中使用。</p>
<h3 id="2-检查-intellij-idea-设置-隐蔽的自动开启">2. 检查 IntelliJ IDEA 设置（隐蔽的自动开启）</h3>
<p>如果你使用的是 IntelliJ IDEA，并且安装了 Reactor 相关插件或开启了相关支持，IDE 可能会自动帮你开启 Debug 模式。</p>
<ul>
<li>操作步骤：<ol>
<li>打开设置：File -&gt; Settings (或 Preferences on macOS)。</li>
<li>导航到：Languages &amp; Frameworks -&gt; Reactive Streams。</li>
<li>取消勾选 Enable Reactor Debug mode。</li>
<li>如果在 Run Configuration（运行配置）中也有类似勾选，请一并取消。</li>
</ol>
</li>
</ul>
<figure class="media is-image" style="--ar:1.3471;--w:982px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-480.avif 480w, https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-800.avif 800w, https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-982.avif 982w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-982.webp" srcset="https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-480.webp 480w, https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-800.webp 800w, https://demo1.661366.xyz/assets/img/1d49cdfd10bf03d9-1d49cdfd10-982.webp 982w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="982" height="729" alt="" loading="lazy" decoding="async"></picture></figure>
<h3 id="3-检查-spring-boot-devtools">3. 检查 Spring Boot DevTools</h3>
<p>如果你的项目中引入了 spring-boot-devtools 依赖，它不仅提供热部署，有时也会为了开发方便自动调整 Reactor 的调试行为（取决于版本）。</p>
<ul>
<li>解决方案：<ul>
<li>如果是在生产环境，确保打包时排除了 DevTools。Maven 中通常设置为 &lt;optional&gt;true&lt;/optional&gt; 或在打包插件中配置排除。</li>
<li>在 application.properties 或 application.yml 中尝试显式关闭（部分旧版本有效）：</li>
</ul>
</li>
</ul>
<hr>
<h3 id="替代方案-生产环境如何调试">替代方案：生产环境如何调试？</h3>
<p>如果你是为了在生产环境排查问题，不要使用 Hooks.onOperatorDebug()。推荐使用以下低损耗方案：</p>
<ol>
<li>ReactorDebugAgent：使用 ReactorDebugAgent.init()，它是生产环境友好的，性能损耗极低。</li>
<li>Checkpoint：在关键的链路上使用 .checkpoint(&quot;my-checkpoint&quot;)，只会记录该特定位置的堆栈，几乎无性能影响。</li>
</ol>
]]></content:encoded><category>IDEA</category><category>Java</category></item>
<item><title>Maven配置文件</title><link>https://demo1.661366.xyz/blog/maven-settings/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/maven-settings/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>基本配置文件</description><content:encoded><![CDATA[<h2 id="基本配置文件">基本配置文件</h2>
<div class="codeblock has-lines is-long" style="--ln:3;--rows:290;--cols:129;--fold:18" data-lines="290"><div class="code-head"><span class="code-lang">XML</span><span class="code-side"></span></div><pre><code class="hljs language-xml"><span class="line"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">&lt;!--</span></span>
<span class="line"><span class="hljs-comment">Licensed to the Apache Software Foundation (ASF) under one</span></span>
<span class="line"><span class="hljs-comment">or more contributor license agreements.  See the NOTICE file</span></span>
<span class="line"><span class="hljs-comment">distributed with this work for additional information</span></span>
<span class="line"><span class="hljs-comment">regarding copyright ownership.  The ASF licenses this file</span></span>
<span class="line"><span class="hljs-comment">to you under the Apache License, Version 2.0 (the</span></span>
<span class="line"><span class="hljs-comment">&quot;License&quot;); you may not use this file except in compliance</span></span>
<span class="line"><span class="hljs-comment">with the License.  You may obtain a copy of the License at</span></span>
<span class="line"><span class="hljs-comment"></span></span>
<span class="line"><span class="hljs-comment">    http://www.apache.org/licenses/LICENSE-2.0</span></span>
<span class="line"><span class="hljs-comment"></span></span>
<span class="line"><span class="hljs-comment">Unless required by applicable law or agreed to in writing,</span></span>
<span class="line"><span class="hljs-comment">software distributed under the License is distributed on an</span></span>
<span class="line"><span class="hljs-comment">&quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span></span>
<span class="line"><span class="hljs-comment">KIND, either express or implied.  See the License for the</span></span>
<span class="line"><span class="hljs-comment">specific language governing permissions and limitations</span></span>
<span class="line"><span class="hljs-comment">under the License.</span></span>
<span class="line"><span class="hljs-comment">--&gt;</span></span>
<span class="line"></span>
<span class="line"><span class="hljs-comment">&lt;!--</span></span>
<span class="line"><span class="hljs-comment"> | This is the configuration file for Maven. It can be specified at two levels:</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |  1. User Level. This settings.xml file provides configuration for a single user,</span></span>
<span class="line"><span class="hljs-comment"> |                 and is normally provided in ${user.home}/.m2/settings.xml.</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |                 <span class="hljs-doctag">NOTE:</span> This location can be overridden with the CLI option:</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |                 -s /path/to/user/settings.xml</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |  2. Global Level. This settings.xml file provides configuration for all Maven</span></span>
<span class="line"><span class="hljs-comment"> |                 users on a machine (assuming they&#x27;re all using the same Maven</span></span>
<span class="line"><span class="hljs-comment"> |                 installation). It&#x27;s normally provided in</span></span>
<span class="line"><span class="hljs-comment"> |                 ${maven.conf}/settings.xml.</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |                 <span class="hljs-doctag">NOTE:</span> This location can be overridden with the CLI option:</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |                 -gs /path/to/global/settings.xml</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> | The sections in this sample file are intended to give you a running start at</span></span>
<span class="line"><span class="hljs-comment"> | getting the most out of your Maven installation. Where appropriate, the default</span></span>
<span class="line"><span class="hljs-comment"> | values (values used when the setting is not specified) are provided.</span></span>
<span class="line"><span class="hljs-comment"> |</span></span>
<span class="line"><span class="hljs-comment"> |--&gt;</span></span>
<span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">settings</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/SETTINGS/1.2.0&quot;</span></span></span>
<span class="line"><span class="hljs-tag">          <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span>
<span class="line"><span class="hljs-tag">          <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd&quot;</span>&gt;</span></span>
<span class="line">  <span class="hljs-comment">&lt;!-- localRepository</span></span>
<span class="line"><span class="hljs-comment">   | The path to the local repository maven will use to store artifacts.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | Default: ${user.home}/.m2/repository</span></span>
<span class="line"><span class="hljs-comment">  &lt;localRepository&gt;/path/to/local/repo&lt;/localRepository&gt;</span></span>
<span class="line"><span class="hljs-comment">  --&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">localRepository</span>&gt;</span>E:\apache-maven-repository<span class="hljs-tag">&lt;/<span class="hljs-name">localRepository</span>&gt;</span></span>
<span class="line">  <span class="hljs-comment">&lt;!-- interactiveMode</span></span>
<span class="line"><span class="hljs-comment">   | This will determine whether maven prompts you when it needs input. If set to false,</span></span>
<span class="line"><span class="hljs-comment">   | maven will use a sensible default value, perhaps based on some other setting, for</span></span>
<span class="line"><span class="hljs-comment">   | the parameter in question.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | Default: true</span></span>
<span class="line"><span class="hljs-comment">  &lt;interactiveMode&gt;true&lt;/interactiveMode&gt;</span></span>
<span class="line"><span class="hljs-comment">  --&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- offline</span></span>
<span class="line"><span class="hljs-comment">   | Determines whether maven should attempt to connect to the network when executing a build.</span></span>
<span class="line"><span class="hljs-comment">   | This will have an effect on artifact downloads, artifact deployment, and others.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | Default: false</span></span>
<span class="line"><span class="hljs-comment">  &lt;offline&gt;false&lt;/offline&gt;</span></span>
<span class="line"><span class="hljs-comment">  --&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- pluginGroups</span></span>
<span class="line"><span class="hljs-comment">   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.</span></span>
<span class="line"><span class="hljs-comment">   | when invoking a command line like &quot;mvn prefix:goal&quot;. Maven will automatically add the group identifiers</span></span>
<span class="line"><span class="hljs-comment">   | &quot;org.apache.maven.plugins&quot; and &quot;org.codehaus.mojo&quot; if these are not already contained in the list.</span></span>
<span class="line"><span class="hljs-comment">   |--&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">pluginGroups</span>&gt;</span></span>
<span class="line">    <span class="hljs-comment">&lt;!-- pluginGroup</span></span>
<span class="line"><span class="hljs-comment">     | Specifies a further group identifier to use for plugin lookup.</span></span>
<span class="line"><span class="hljs-comment">    &lt;pluginGroup&gt;com.your.plugins&lt;/pluginGroup&gt;</span></span>
<span class="line"><span class="hljs-comment">    --&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;/<span class="hljs-name">pluginGroups</span>&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- TODO Since when can proxies be selected as depicted? --&gt;</span></span>
<span class="line">  <span class="hljs-comment">&lt;!-- proxies</span></span>
<span class="line"><span class="hljs-comment">   | This is a list of proxies which can be used on this machine to connect to the network.</span></span>
<span class="line"><span class="hljs-comment">   | Unless otherwise specified (by system property or command-line switch), the first proxy</span></span>
<span class="line"><span class="hljs-comment">   | specification in this list marked as active will be used.</span></span>
<span class="line"><span class="hljs-comment">   |--&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">proxies</span>&gt;</span></span>
<span class="line">    <span class="hljs-comment">&lt;!-- proxy</span></span>
<span class="line"><span class="hljs-comment">     | Specification for one proxy, to be used in connecting to the network.</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">    &lt;proxy&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;optional&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;active&gt;true&lt;/active&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;protocol&gt;http&lt;/protocol&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;username&gt;proxyuser&lt;/username&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;password&gt;proxypass&lt;/password&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;host&gt;proxy.host.net&lt;/host&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;port&gt;80&lt;/port&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;nonProxyHosts&gt;local.net|some.host.com&lt;/nonProxyHosts&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/proxy&gt;</span></span>
<span class="line"><span class="hljs-comment">    --&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;/<span class="hljs-name">proxies</span>&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- servers</span></span>
<span class="line"><span class="hljs-comment">   | This is a list of authentication profiles, keyed by the server-id used within the system.</span></span>
<span class="line"><span class="hljs-comment">   | Authentication profiles can be used whenever maven must make a connection to a remote server.</span></span>
<span class="line"><span class="hljs-comment">   |--&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">servers</span>&gt;</span></span>
<span class="line">    <span class="hljs-comment">&lt;!-- server</span></span>
<span class="line"><span class="hljs-comment">     | Specifies the authentication information to use when connecting to a particular server, identified by</span></span>
<span class="line"><span class="hljs-comment">     | a unique name within the system (referred to by the &#x27;id&#x27; attribute below).</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">     | <span class="hljs-doctag">NOTE:</span> You should either specify username/password OR privateKey/passphrase, since these pairings are</span></span>
<span class="line"><span class="hljs-comment">     |       used together.</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">    &lt;server&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;deploymentRepo&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;username&gt;repouser&lt;/username&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;password&gt;repopwd&lt;/password&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/server&gt;</span></span>
<span class="line"><span class="hljs-comment">    --&gt;</span></span>
<span class="line"></span>
<span class="line">    <span class="hljs-comment">&lt;!-- Another sample, using keys to authenticate.</span></span>
<span class="line"><span class="hljs-comment">    &lt;server&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;siteServer&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;privateKey&gt;/path/to/private/key&lt;/privateKey&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;passphrase&gt;optional; leave empty if not used.&lt;/passphrase&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/server&gt;</span></span>
<span class="line"><span class="hljs-comment">    --&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;/<span class="hljs-name">servers</span>&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- mirrors</span></span>
<span class="line"><span class="hljs-comment">   | This is a list of mirrors to be used in downloading artifacts from remote repositories.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | It works like this: a POM may declare a repository to use in resolving certain artifacts.</span></span>
<span class="line"><span class="hljs-comment">   | However, this repository may have problems with heavy traffic at times, so people have mirrored</span></span>
<span class="line"><span class="hljs-comment">   | it to several places.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | That repository definition will have a unique id, so we can create a mirror reference for that</span></span>
<span class="line"><span class="hljs-comment">   | repository, to be used as an alternate download site. The mirror site will be the preferred</span></span>
<span class="line"><span class="hljs-comment">   | server for that repository.</span></span>
<span class="line"><span class="hljs-comment">   |--&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">mirrors</span>&gt;</span></span>
<span class="line">    <span class="hljs-comment">&lt;!-- mirror</span></span>
<span class="line"><span class="hljs-comment">     | Specifies a repository mirror site to use instead of a given repository. The repository that</span></span>
<span class="line"><span class="hljs-comment">     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used</span></span>
<span class="line"><span class="hljs-comment">     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">    &lt;mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;mirrorId&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;mirrorOf&gt;repositoryId&lt;/mirrorOf&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;name&gt;Human Readable Name for this Mirror.&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;url&gt;http://my.repository.com/repo/path&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">     --&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!--     &lt;mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;maven-default-http-blocker&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;mirrorOf&gt;external:http:*&lt;/mirrorOf&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;name&gt;Pseudo repository to mirror external repositories initially using HTTP.&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;url&gt;http://0.0.0.0/&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;blocked&gt;true&lt;/blocked&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/mirror&gt; --&gt;</span></span>
<span class="line"></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">mirror</span>&gt;</span>  </span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>Central<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span>  </span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>Central Repository<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span>  </span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">url</span>&gt;</span>https://repo.maven.apache.org/maven2/<span class="hljs-tag">&lt;/<span class="hljs-name">url</span>&gt;</span></span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">mirrorOf</span>&gt;</span>central<span class="hljs-tag">&lt;/<span class="hljs-name">mirrorOf</span>&gt;</span>      </span>
<span class="line">	<span class="hljs-tag">&lt;/<span class="hljs-name">mirror</span>&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-tag">&lt;/<span class="hljs-name">mirrors</span>&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- profiles</span></span>
<span class="line"><span class="hljs-comment">   | This is a list of profiles which can be activated in a variety of ways, and which can modify</span></span>
<span class="line"><span class="hljs-comment">   | the build process. Profiles provided in the settings.xml are intended to provide local machine-</span></span>
<span class="line"><span class="hljs-comment">   | specific paths and repository locations which allow the build to work in the local environment.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | For example, if you have an integration testing plugin - like cactus - that needs to know where</span></span>
<span class="line"><span class="hljs-comment">   | your Tomcat instance is installed, you can provide a variable here such that the variable is</span></span>
<span class="line"><span class="hljs-comment">   | dereferenced during the build process to configure the cactus plugin.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles</span></span>
<span class="line"><span class="hljs-comment">   | section of this document (settings.xml) - will be discussed later. Another way essentially</span></span>
<span class="line"><span class="hljs-comment">   | relies on the detection of a property, either matching a particular value for the property,</span></span>
<span class="line"><span class="hljs-comment">   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a</span></span>
<span class="line"><span class="hljs-comment">   | value of &#x27;1.4&#x27; might activate a profile when the build is executed on a JDK version of &#x27;1.4.2_07&#x27;.</span></span>
<span class="line"><span class="hljs-comment">   | Finally, the list of active profiles can be specified directly from the command line.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   | <span class="hljs-doctag">NOTE:</span> For profiles defined in the settings.xml, you are restricted to specifying only artifact</span></span>
<span class="line"><span class="hljs-comment">   |       repositories, plugin repositories, and free-form properties to be used as configuration</span></span>
<span class="line"><span class="hljs-comment">   |       variables for plugins in the POM.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">   |--&gt;</span></span>
<span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">profiles</span>&gt;</span></span>
<span class="line">    <span class="hljs-comment">&lt;!-- profile</span></span>
<span class="line"><span class="hljs-comment">     | Specifies a set of introductions to the build process, to be activated using one or more of the</span></span>
<span class="line"><span class="hljs-comment">     | mechanisms described above. For inheritance purposes, and to activate profiles via &lt;activatedProfiles/&gt;</span></span>
<span class="line"><span class="hljs-comment">     | or the command line, profiles have to have an ID that is unique.</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">     | An encouraged best practice for profile identification is to use a consistent naming convention</span></span>
<span class="line"><span class="hljs-comment">     | for profiles, such as &#x27;env-dev&#x27;, &#x27;env-test&#x27;, &#x27;env-production&#x27;, &#x27;user-jdcasey&#x27;, &#x27;user-brett&#x27;, etc.</span></span>
<span class="line"><span class="hljs-comment">     | This will make it more intuitive to understand what the set of introduced profiles is attempting</span></span>
<span class="line"><span class="hljs-comment">     | to accomplish, particularly when you only have a list of profile id&#x27;s for debug.</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.</span></span>
<span class="line"><span class="hljs-comment">    &lt;profile&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;jdk-1.4&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment"></span></span>
<span class="line"><span class="hljs-comment">      &lt;activation&gt;</span></span>
<span class="line"><span class="hljs-comment">        &lt;jdk&gt;1.4&lt;/jdk&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/activation&gt;</span></span>
<span class="line"><span class="hljs-comment"></span></span>
<span class="line"><span class="hljs-comment">      &lt;repositories&gt;</span></span>
<span class="line"><span class="hljs-comment">        &lt;repository&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;id&gt;jdk14&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;name&gt;Repository for JDK 1.4 builds&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;url&gt;http://www.myhost.com/maven/jdk14&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;layout&gt;default&lt;/layout&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;snapshotPolicy&gt;always&lt;/snapshotPolicy&gt;</span></span>
<span class="line"><span class="hljs-comment">        &lt;/repository&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/repositories&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/profile&gt;</span></span>
<span class="line"><span class="hljs-comment">    --&gt;</span></span>
<span class="line"></span>
<span class="line">    <span class="hljs-comment">&lt;!--</span></span>
<span class="line"><span class="hljs-comment">     | Here is another profile, activated by the property &#x27;target-env&#x27; with a value of &#x27;dev&#x27;, which</span></span>
<span class="line"><span class="hljs-comment">     | provides a specific path to the Tomcat instance. To use this, your plugin configuration might</span></span>
<span class="line"><span class="hljs-comment">     | hypothetically look like:</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">     | ...</span></span>
<span class="line"><span class="hljs-comment">     | &lt;plugin&gt;</span></span>
<span class="line"><span class="hljs-comment">     |   &lt;groupId&gt;org.myco.myplugins&lt;/groupId&gt;</span></span>
<span class="line"><span class="hljs-comment">     |   &lt;artifactId&gt;myplugin&lt;/artifactId&gt;</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">     |   &lt;configuration&gt;</span></span>
<span class="line"><span class="hljs-comment">     |     &lt;tomcatLocation&gt;${tomcatPath}&lt;/tomcatLocation&gt;</span></span>
<span class="line"><span class="hljs-comment">     |   &lt;/configuration&gt;</span></span>
<span class="line"><span class="hljs-comment">     | &lt;/plugin&gt;</span></span>
<span class="line"><span class="hljs-comment">     | ...</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">     | <span class="hljs-doctag">NOTE:</span> If you just wanted to inject this configuration whenever someone set &#x27;target-env&#x27; to</span></span>
<span class="line"><span class="hljs-comment">     |       anything, you could just leave off the &lt;value/&gt; inside the activation-property.</span></span>
<span class="line"><span class="hljs-comment">     |</span></span>
<span class="line"><span class="hljs-comment">    &lt;profile&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;id&gt;env-dev&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment"></span></span>
<span class="line"><span class="hljs-comment">      &lt;activation&gt;</span></span>
<span class="line"><span class="hljs-comment">        &lt;property&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;name&gt;target-env&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;value&gt;dev&lt;/value&gt;</span></span>
<span class="line"><span class="hljs-comment">        &lt;/property&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/activation&gt;</span></span>
<span class="line"><span class="hljs-comment"></span></span>
<span class="line"><span class="hljs-comment">      &lt;properties&gt;</span></span>
<span class="line"><span class="hljs-comment">        &lt;tomcatPath&gt;/path/to/tomcat/instance&lt;/tomcatPath&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/properties&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;/profile&gt;</span></span>
<span class="line"><span class="hljs-comment">    --&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!-- ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— --&gt;</span></span>
<span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">profile</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>jdk-17<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">activation</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">activeByDefault</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">activeByDefault</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">jdk</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">jdk</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">activation</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.compilerVersion</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.compilerVersion</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span></span>
<span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">profile</span>&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!-- —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— --&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-tag">&lt;/<span class="hljs-name">profiles</span>&gt;</span></span>
<span class="line"></span>
<span class="line">  <span class="hljs-comment">&lt;!-- activeProfiles</span></span>
<span class="line"><span class="hljs-comment">   | List of profiles that are active for all builds.</span></span>
<span class="line"><span class="hljs-comment">   |</span></span>
<span class="line"><span class="hljs-comment">  &lt;activeProfiles&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;activeProfile&gt;alwaysActiveProfile&lt;/activeProfile&gt;</span></span>
<span class="line"><span class="hljs-comment">    &lt;activeProfile&gt;anotherAlwaysActiveProfile&lt;/activeProfile&gt;</span></span>
<span class="line"><span class="hljs-comment">  &lt;/activeProfiles&gt;</span></span>
<span class="line"><span class="hljs-comment">  --&gt;</span></span>
<span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">settings</span>&gt;</span></span>
<span class="line"></span>
<span class="line"></span></code></pre></div>
<p>这个表示仓库位置</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:62"><div class="code-head"><span class="code-lang">XML</span><span class="code-side"></span></div><pre><code class="hljs language-xml"><span class="line"> <span class="hljs-tag">&lt;<span class="hljs-name">localRepository</span>&gt;</span>E:\apache-maven-repository<span class="hljs-tag">&lt;/<span class="hljs-name">localRepository</span>&gt;</span></span></code></pre></div>
<p>这个是下载的镜像，这是中央仓库的</p>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:56"><div class="code-head"><span class="code-lang">XML</span><span class="code-side"></span></div><pre><code class="hljs language-xml"><span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">mirror</span>&gt;</span>  </span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>Central<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span>  </span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>Central Repository<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span>  </span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">url</span>&gt;</span>https://repo.maven.apache.org/maven2/<span class="hljs-tag">&lt;/<span class="hljs-name">url</span>&gt;</span></span>
<span class="line">	    <span class="hljs-tag">&lt;<span class="hljs-name">mirrorOf</span>&gt;</span>central<span class="hljs-tag">&lt;/<span class="hljs-name">mirrorOf</span>&gt;</span>      </span>
<span class="line">	<span class="hljs-tag">&lt;/<span class="hljs-name">mirror</span>&gt;</span></span></code></pre></div>
<p>这个指定JDK版本，这里是17，jdk8是1.8</p>
<div class="codeblock has-lines" style="--ln:2;--rows:12;--cols:75"><div class="code-head"><span class="code-lang">XML</span><span class="code-side"></span></div><pre><code class="hljs language-xml"><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">profile</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>jdk-17<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">activation</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">activeByDefault</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">activeByDefault</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">jdk</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">jdk</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">activation</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span></span>
<span class="line">        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.compilerVersion</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.compilerVersion</span>&gt;</span></span>
<span class="line">    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span></span>
<span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">profile</span>&gt;</span></span></code></pre></div>
<p>下面的国内的镜像源地址，一般都选用阿里的镜像源</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:42;--cols:85;--fold:18" data-lines="42"><div class="code-head"><span class="code-lang">XML</span><span class="code-side"></span></div><pre><code class="hljs language-xml"><span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">mirrors</span>&gt;</span></span>
<span class="line">      <span class="hljs-comment">&lt;!-- 阿里云Maven仓库 --&gt;</span></span>
<span class="line">      <span class="hljs-tag">&lt;<span class="hljs-name">mirror</span>&gt;</span></span>
<span class="line">          <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>alimaven<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span></span>
<span class="line">          <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>aliyun maven<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span></span>
<span class="line">          <span class="hljs-tag">&lt;<span class="hljs-name">url</span>&gt;</span>https://maven.aliyun.com/repository/public<span class="hljs-tag">&lt;/<span class="hljs-name">url</span>&gt;</span></span>
<span class="line">          <span class="hljs-tag">&lt;<span class="hljs-name">mirrorOf</span>&gt;</span>central<span class="hljs-tag">&lt;/<span class="hljs-name">mirrorOf</span>&gt;</span></span>
<span class="line">      <span class="hljs-tag">&lt;/<span class="hljs-name">mirror</span>&gt;</span></span>
<span class="line"></span>
<span class="line">      <span class="hljs-comment">&lt;!-- 华为云Maven仓库 --&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!--       &lt;mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;id&gt;huaweicloud&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;name&gt;huaweicloud maven&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;url&gt;https://repo.huaweicloud.com/repository/maven/&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;mirrorOf&gt;central&lt;/mirrorOf&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/mirror&gt; --&gt;</span></span>
<span class="line"></span>
<span class="line">      <span class="hljs-comment">&lt;!-- 腾讯云Maven仓库 --&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!--       &lt;mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;id&gt;tencentcloud&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;name&gt;tencentcloud maven&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;url&gt;https://mirrors.cloud.tencent.com/nexus/repository/maven-public/&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;mirrorOf&gt;central&lt;/mirrorOf&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/mirror&gt; --&gt;</span></span>
<span class="line"></span>
<span class="line">      <span class="hljs-comment">&lt;!-- 开源中国Maven仓库 --&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!--       &lt;mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;id&gt;oschina&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;name&gt;oschina maven&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;url&gt;https://maven.oschina.net/content/groups/public/&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;mirrorOf&gt;central&lt;/mirrorOf&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/mirror&gt; --&gt;</span></span>
<span class="line"></span>
<span class="line">      <span class="hljs-comment">&lt;!-- 网易Maven仓库 --&gt;</span></span>
<span class="line"><span class="hljs-comment">&lt;!--       &lt;mirror&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;id&gt;netease&lt;/id&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;name&gt;netease maven&lt;/name&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;url&gt;https://mirrors.163.com/maven/repository/maven-public/&lt;/url&gt;</span></span>
<span class="line"><span class="hljs-comment">          &lt;mirrorOf&gt;central&lt;/mirrorOf&gt;</span></span>
<span class="line"><span class="hljs-comment">      &lt;/mirror&gt; --&gt;</span></span>
<span class="line">    </span>
<span class="line">  <span class="hljs-tag">&lt;/<span class="hljs-name">mirrors</span>&gt;</span></span></code></pre></div>
<h2 id="问题解决">问题解决</h2>
<h3 id="问题1-提示找不到依赖">问题1：提示找不到依赖</h3>
<p>解决方法：自己去maven仓库下载jar包，然后只想下面的命令，然后看看最后这个文件存储到哪里了，最后将这个文件，放到当前正在使用的仓库中</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:136"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">mvn install:install-file -Dfile=D:\okhttp-4.12.0.jar -DgroupId=com.squareup.okhttp3 -DartifactId=okhttp -Dversion=4.12.0 -Dpackaging=jar</span></code></pre></div>
<p><a href="assets/settings-1.8-20250327101552-vwc4946.xml">settings-1.8.xml</a></p>
<p><a href="assets/settings-17-20250327101601-jmqyjq7.xml">settings-17.xml</a></p>
<p><a href="assets/settings-21-20250327101605-6qev8zf.xml">settings-21.xml</a></p>
]]></content:encoded><category>Maven</category><category>Java</category></item>
<item><title>MybatisPlus使用</title><link>https://demo1.661366.xyz/blog/mybatis-plus-usage/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/mybatis-plus-usage/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>✅ 一、标准三层结构（MP 官方推荐）</description><content:encoded><![CDATA[<h2 id="一-标准三层结构-mp-官方推荐">✅ 一、标准三层结构（MP 官方推荐）</h2>
<p>假设你的实体是：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:28"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">TrainTransVolMonDetailEntity</span></code></pre></div>
<hr>
<h3 id="1-dao-层-mapper">1️⃣ Dao 层（Mapper）</h3>
<div class="codeblock has-lines" style="--ln:1;--rows:9;--cols:58"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">import com.baomidou.mybatisplus.core.mapper.BaseMapper;</span>
<span class="line">import org.apache.ibatis.annotations.Mapper;</span>
<span class="line"></span>
<span class="line">@Mapper</span>
<span class="line">public interface TrainTransVolMonDetailDao</span>
<span class="line">        extends BaseMapper&lt;TrainTransVolMonDetailEntity&gt; {</span>
<span class="line"></span>
<span class="line">    // 你自己的自定义SQL可以写这里</span>
<span class="line">}</span></code></pre></div>
<p>✔ 继承 BaseMapper&lt;T&gt;<br>✔ 自动拥有：</p>
<ul>
<li>insert</li>
<li>deleteById</li>
<li>updateById</li>
<li>selectById</li>
<li>selectList</li>
<li>selectPage</li>
</ul>
<p>不用写 XML 也能用</p>
<hr>
<h3 id="2-service-接口">2️⃣ Service 接口</h3>
<div class="codeblock has-lines" style="--ln:1;--rows:7;--cols:59"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">import com.baomidou.mybatisplus.extension.service.IService;</span>
<span class="line"></span>
<span class="line">public interface TrainTransVolMonDetailService</span>
<span class="line">        extends IService&lt;TrainTransVolMonDetailEntity&gt; {</span>
<span class="line"></span>
<span class="line">    // 你自定义的方法写这里</span>
<span class="line">}</span></code></pre></div>
<p>✔ 继承 IService&lt;T&gt;<br>✔ 自动拥有：</p>
<ul>
<li>save</li>
<li>saveBatch</li>
<li>removeById</li>
<li>updateById</li>
<li>list</li>
<li>page</li>
<li>getById</li>
</ul>
<hr>
<h3 id="3-serviceimpl">3️⃣ ServiceImpl</h3>
<div class="codeblock has-lines" style="--ln:2;--rows:10;--cols:84"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;</span>
<span class="line">import org.springframework.stereotype.Service;</span>
<span class="line"></span>
<span class="line">@Service</span>
<span class="line">public class TrainTransVolMonDetailServiceImpl</span>
<span class="line">        extends ServiceImpl&lt;TrainTransVolMonDetailDao, TrainTransVolMonDetailEntity&gt;</span>
<span class="line">        implements TrainTransVolMonDetailService {</span>
<span class="line"></span>
<span class="line">    // 自定义方法写这里</span>
<span class="line">}</span></code></pre></div>
<p>✔ 继承：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:27"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">ServiceImpl&lt;Mapper, Entity&gt;</span></code></pre></div>
<p>✔ 自动注入 mapper<br>✔ 自动拥有 saveBatch 等方法</p>
<hr>
<h2 id="结构总结">🎯 结构总结</h2>
<div class="table-wrap"><table>
<thead>
<tr>
<th>层</th>
<th>继承谁</th>
</tr>
</thead>
<tbody><tr>
<td>Dao</td>
<td>BaseMapper</td>
</tr>
<tr>
<td>Service</td>
<td>IService</td>
</tr>
<tr>
<td>ServiceImpl</td>
<td>ServiceImpl&lt;Dao, Entity&gt;</td>
</tr>
</tbody></table></div>
<hr>
<h2 id="这样你就可以直接用">🚀 这样你就可以直接用</h2>
<h3 id="批量插入-不用写-xml">批量插入（不用写 XML）</h3>
<div class="codeblock" style="--ln:1;--rows:1;--cols:27"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">this.saveBatch(list, 1000);</span></code></pre></div>
<h3 id="普通插入">普通插入</h3>
<div class="codeblock" style="--ln:1;--rows:1;--cols:18"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">this.save(entity);</span></code></pre></div>
<h3 id="根据条件查询">根据条件查询</h3>
<div class="codeblock has-lines" style="--ln:1;--rows:6;--cols:52"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">this.list(</span>
<span class="line">    new QueryWrapper&lt;TrainTransVolMonDetailEntity&gt;()</span>
<span class="line">        .ge(&quot;end_date&quot;, startTime)</span>
<span class="line">        .lt(&quot;end_date&quot;, endTime)</span>
<span class="line">        .like(&quot;shdw&quot;, shdw)</span>
<span class="line">);</span></code></pre></div>
<hr>
<h2 id="常见错误">⚠️ 常见错误</h2>
<h4 id="dao-不加-mapper">❌ Dao 不加 @Mapper</h4>
<p>会报找不到 Bean</p>
<h4 id="serviceimpl-不加-service">❌ ServiceImpl 不加 @Service</h4>
<p>Spring 不会扫描</p>
<h4 id="继承写反">❌ 继承写反</h4>
<p>一定是：</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:27"><div class="code-head"><span class="code-lang"></span><span class="code-side"></span></div><pre><code class="hljs"><span class="line">ServiceImpl&lt;Mapper, Entity&gt;</span></code></pre></div>
<p>不是反过来</p>
]]></content:encoded><category>Java</category><category>MyBatis-Plus</category></item>
<item><title>配置Nginx静态文件访问</title><link>https://demo1.661366.xyz/blog/nginx-static-files/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/nginx-static-files/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>这是一个非常经典也是非常重要的 Nginx 基础问题。弄清楚这两点，能帮你避免 80% 的 404 错误。</description><content:encoded><![CDATA[<div class="codeblock has-lines is-long" style="--ln:2;--rows:35;--cols:100;--fold:18" data-lines="35"><div class="code-head"><span class="code-lang">Nginx</span><span class="code-side"></span></div><pre><code class="hljs language-nginx"><span class="line">    <span class="hljs-comment"># 外网服务配置（监听10005端口）</span></span>
<span class="line">    <span class="hljs-section">server</span> {</span>
<span class="line">        <span class="hljs-attribute">listen</span>       <span class="hljs-number">10005</span>;</span>
<span class="line">        <span class="hljs-attribute">server_name</span>  <span class="hljs-number">60</span>.<span class="hljs-number">2</span>.xx.xx;</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># 前端Vue静态资源</span></span>
<span class="line">        <span class="hljs-section">location</span> /tos-web {</span>
<span class="line">            <span class="hljs-attribute">alias</span>   D:/kstos/web/apache-tomcat-<span class="hljs-number">8</span>.<span class="hljs-number">5</span>.<span class="hljs-number">85</span>/webapps/tos-web;  <span class="hljs-comment"># 替换为实际前端dist目录路径</span></span>
<span class="line">            <span class="hljs-attribute">index</span>  index.html index.htm;</span>
<span class="line">            <span class="hljs-attribute">try_files</span> <span class="hljs-variable">$uri</span> <span class="hljs-variable">$uri</span>/ /tos-web/index.html;</span>
<span class="line">        }</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># 后端API代理（外网）</span></span>
<span class="line">        <span class="hljs-section">location</span> /tos {</span>
<span class="line">            <span class="hljs-attribute">proxy_pass</span> http://60.2.xx.xx:10006/tos;</span>
<span class="line">            <span class="hljs-attribute">proxy_set_header</span> Host <span class="hljs-variable">$host</span>;</span>
<span class="line">            <span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable">$remote_addr</span>;</span>
<span class="line">            <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable">$proxy_add_x_forwarded_for</span>;</span>
<span class="line">            </span>
<span class="line">            <span class="hljs-comment"># WebSocket支持（如果需要）</span></span>
<span class="line">            <span class="hljs-attribute">proxy_http_version</span> <span class="hljs-number">1</span>.<span class="hljs-number">1</span>;</span>
<span class="line">            <span class="hljs-attribute">proxy_set_header</span> Upgrade <span class="hljs-variable">$http_upgrade</span>;</span>
<span class="line">            <span class="hljs-attribute">proxy_set_header</span> Connection <span class="hljs-string">&quot;upgrade&quot;</span>;</span>
<span class="line">        }</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># 静态文件</span></span>
<span class="line">        <span class="hljs-section">location</span> /images/ {</span>
<span class="line">			<span class="hljs-comment"># ！！！注意这里改成了 root，并且路径少了一层 /images/ ！！！</span></span>
<span class="line">            <span class="hljs-comment"># 原理：root 会把 &quot;location 后的 URL&quot; 自动拼接到 &quot;root 指定的路径&quot; 后面</span></span>
<span class="line">            <span class="hljs-comment"># 拼接逻辑：D:/kstos/files/ + /images/indexImg/gis.png</span></span>
<span class="line">            <span class="hljs-attribute">root</span> D:/kstos/files/;           <span class="hljs-comment"># 物理路径</span></span>
<span class="line">            <span class="hljs-attribute">autoindex</span> <span class="hljs-literal">off</span>;                  <span class="hljs-comment"># 禁止列出整个目录</span></span>
<span class="line">            <span class="hljs-attribute">allow</span> all;                      <span class="hljs-comment"># 允许所有人访问</span></span>
<span class="line">        }</span>
<span class="line">    }</span></code></pre></div>
<p>这是一个非常经典也是非常重要的 Nginx 基础问题。弄清楚这两点，能帮你避免 80% 的 404 错误。</p>
<p>我将其拆解为两个部分来详细讲解。</p>
<hr>
<h3 id="第一部分-location-后面加不加斜杠的区别">第一部分：location 后面加不加斜杠的区别</h3>
<p><code>location /images</code> 和 <code>location /images/</code> 的核心区别在于 匹配的精确度（前缀匹配 vs 目录匹配）。</p>
<h4 id="1-不加斜杠-location-images">1. 不加斜杠：location /images</h4>
<p>这是 普通前缀匹配。只要 URL 以 /images 开头，都会匹配上。</p>
<ul>
<li>配置：</li>
<li>匹配情况：<ul>
<li>http://.../images/1.jpg -&gt; ✅ 匹配</li>
<li>http://.../images/index.html -&gt; ✅ 匹配</li>
<li>http://.../images-old/1.jpg -&gt; ✅ 匹配！ (注意这里)</li>
<li>http://.../images_2023/1.jpg -&gt; ✅ 匹配！</li>
</ul>
</li>
</ul>
<p>潜在风险： 如果你只想匹配 images 目录，但不想匹配到 images-old 或 images_backup，不加斜杠就会导致误匹配，可能暴露你不希望公开的备份目录。</p>
<h4 id="2-加斜杠-location-images">2. 加斜杠：location /images/</h4>
<p>这是 目录匹配。它明确告诉 Nginx，这是一个文件夹。</p>
<ul>
<li>配置：</li>
<li>匹配情况：<ul>
<li>http://.../images/1.jpg -&gt; ✅ 匹配</li>
<li>http://.../images-old/1.jpg -&gt; ❌ 不匹配 (因为 /images-old 不是以 /images/ 开头的)</li>
</ul>
</li>
</ul>
<p>✅ 最佳实践：</p>
<p>如果你是在配置一个静态资源目录，强烈建议加上斜杠 /，这样更安全，也能减少不必要的歧义。</p>
<hr>
<h3 id="第二部分-root-和-alias-的区别">第二部分：root 和 alias 的区别</h3>
<p>这是最容易导致 404 的地方。简单来说：</p>
<ul>
<li>root 是 拼接（追加）。</li>
<li>alias 是 替换。</li>
</ul>
<h4 id="1-root-拼接模式">1. root (拼接模式)</h4>
<p>root 会把你请求的 URL 完整地拼接到 root 指定的路径后面。</p>
<ul>
<li>场景：你要访问 http://.../images/1.jpg</li>
<li>配置：</li>
<li>Nginx 寻找文件的物理路径计算公式：<br>root 路径 + 完整 URL<br>= /data/www + /images/1.jpg<br>= /data/www/images/1.jpg</li>
</ul>
<blockquote>
<p>特点： 你的硬盘上必须真实存在一个 images 文件夹，且在 /data/www 下面。硬盘结构必须和 URL 结构保持一致。</p>
</blockquote>
<h4 id="2-alias-替换模式">2. alias (替换模式)</h4>
<p>alias 会把 location 后面匹配到的部分 丢弃，用 alias 指定的路径来 替换 它。</p>
<ul>
<li>场景：你要访问 http://.../images/1.jpg</li>
<li>配置：</li>
<li>Nginx 寻找文件的物理路径计算公式：<br>alias 路径 + (完整 URL - location 部分)<br>= /data/www/photos/ + 1.jpg (去掉 /images/)<br>= /data/www/photos/1.jpg</li>
</ul>
<blockquote>
<p>特点： 硬盘上的文件夹名字叫 photos，但你想让用户通过 /images/ 来访问。它允许 URL 和硬盘目录名不一样。</p>
</blockquote>
<hr>
<h3 id="总结对比表">总结对比表</h3>
<div class="table-wrap"><table>
<thead>
<tr>
<th>特性</th>
<th>root (根路径)</th>
<th>alias (别名)</th>
</tr>
</thead>
<tbody><tr>
<td>逻辑</td>
<td>追加：root + URI</td>
<td>替换：alias + (URI - location)</td>
</tr>
<tr>
<td>硬盘目录要求</td>
<td>硬盘目录结构必须包含 location 定义的文件夹名</td>
<td>硬盘目录可以是任意名字，不需要和 location 一致</td>
</tr>
<tr>
<td>使用场景</td>
<td>绝大多数情况，尤其是目录结构规范时</td>
<td>当你需要把 URL 映射到一个完全不同名字的目录时</td>
</tr>
<tr>
<td>斜杠敏感度</td>
<td>不敏感</td>
<td>非常敏感：如果 location 有/，alias 末尾最好也带/</td>
</tr>
</tbody></table></div>
<p>结论：</p>
<p>虽然在这个例子里，算出来的最终路径是一模一样的，但 root 的容错率更高，不易出错。在 Windows 环境下，alias 有时会因为路径斜杠处理不当出 bug，而 root 逻辑简单粗暴，所以更推荐使用 root。</p>
<p>没问题。追求配置的简洁和高效是非常好的运维习惯，越冗长的配置越容易出错。</p>
<p>采用白名单机制后，我们可以直接删掉那堆防范脚本的黑名单代码，用两行代码完成更高级别的防御。</p>
<p>这是为你整理好的完整、纯净且包含图床/视频流优化的最终版本。你可以直接复制并替换你原来的 server 块：</p>
<div class="codeblock has-lines is-long" style="--ln:2;--rows:65;--cols:95;--fold:18" data-lines="65"><div class="code-head"><span class="code-lang">Nginx</span><span class="code-side"></span></div><pre><code class="hljs language-nginx"><span class="line"><span class="hljs-section">server</span> {</span>
<span class="line">    <span class="hljs-attribute">listen</span> <span class="hljs-number">443</span> ssl http2;</span>
<span class="line">    <span class="hljs-attribute">listen</span> [::]:<span class="hljs-number">443</span> ssl http2;</span>
<span class="line">    <span class="hljs-attribute">server_name</span> demo3.<span class="hljs-number">661366</span>.xyz;</span>
<span class="line"></span>
<span class="line">    <span class="hljs-comment"># SSL 证书配置</span></span>
<span class="line">    <span class="hljs-attribute">ssl_certificate</span> /etc/letsencrypt/live/<span class="hljs-number">661366</span>.xyz-<span class="hljs-number">0001</span>/fullchain.pem; <span class="hljs-comment"># managed by Certbot</span></span>
<span class="line">    <span class="hljs-attribute">ssl_certificate_key</span> /etc/letsencrypt/live/<span class="hljs-number">661366</span>.xyz-<span class="hljs-number">0001</span>/privkey.pem; <span class="hljs-comment"># managed by Certbot</span></span>
<span class="line">    <span class="hljs-attribute">include</span> /etc/letsencrypt/options-ssl-nginx.conf;</span>
<span class="line">    <span class="hljs-attribute">ssl_dhparam</span> /etc/letsencrypt/ssl-dhparams.pem;</span>
<span class="line"></span>
<span class="line">    <span class="hljs-comment"># 后端 API / 动态请求代理</span></span>
<span class="line">    <span class="hljs-section">location</span> / {</span>
<span class="line">        <span class="hljs-attribute">proxy_pass</span> http://localhost:9000;</span>
<span class="line">        <span class="hljs-attribute">proxy_set_header</span> Host <span class="hljs-variable">$host</span>;</span>
<span class="line">        <span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable">$remote_addr</span>;</span>
<span class="line">        <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable">$proxy_add_x_forwarded_for</span>;</span>
<span class="line">        <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-Proto <span class="hljs-variable">$scheme</span>;</span>
<span class="line">        <span class="hljs-attribute">proxy_set_header</span> Upgrade <span class="hljs-variable">$http_upgrade</span>;</span>
<span class="line">        <span class="hljs-attribute">proxy_set_header</span> Connection <span class="hljs-string">&quot;Upgrade&quot;</span>;</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    <span class="hljs-comment"># 静态媒体资源 (图片与视频)</span></span>
<span class="line">    <span class="hljs-section">location</span> /images/ {</span>
<span class="line">		<span class="hljs-comment"># 物理路径</span></span>
<span class="line">        <span class="hljs-attribute">alias</span> /home/images/tos/; </span>
<span class="line">		<span class="hljs-comment"># 禁止列出整个目录</span></span>
<span class="line">        <span class="hljs-attribute">autoindex</span> <span class="hljs-literal">off</span>;</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-comment"># 1. 核心安全：白名单过滤 (最简洁的防马方式)</span></span>
<span class="line">        <span class="hljs-comment"># 仅放行图片和视频后缀，其余请求直接 403 拒绝</span></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-attribute">if</span> (<span class="hljs-variable">$uri</span> !<span class="hljs-regexp">~* \.(jpg|jpeg|png|gif|webp|avif|mp4|webm|m3u8|ts|ico)$)</span> {</span>
<span class="line">            <span class="hljs-attribute">return</span> <span class="hljs-number">403</span>;</span>
<span class="line">        }</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-comment"># 2. 防盗链：保护你的服务器带宽</span></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-attribute">valid_referers</span> <span class="hljs-literal">none</span> <span class="hljs-literal">blocked</span> server_names *.<span class="hljs-number">661366</span>.xyz <span class="hljs-number">661366</span>.xyz;</span>
<span class="line">        <span class="hljs-attribute">if</span> (<span class="hljs-variable">$invalid_referer</span>) {</span>
<span class="line">            <span class="hljs-attribute">return</span> <span class="hljs-number">403</span>;</span>
<span class="line">        }</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-comment"># 3. 性能优化：大文件与视频拖拽支持</span></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-attribute">sendfile</span> <span class="hljs-literal">on</span>;</span>
<span class="line">        <span class="hljs-attribute">tcp_nopush</span> <span class="hljs-literal">on</span>;</span>
<span class="line">        mp4;</span>
<span class="line">        <span class="hljs-attribute">mp4_buffer_size</span> <span class="hljs-number">1m</span>;</span>
<span class="line">        <span class="hljs-attribute">mp4_max_buffer_size</span> <span class="hljs-number">5m</span>;</span>
<span class="line"></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-comment"># 4. 浏览器缓存与安全响应头</span></span>
<span class="line">        <span class="hljs-comment"># =========================================================</span></span>
<span class="line">        <span class="hljs-attribute">expires</span> <span class="hljs-number">30d</span>;</span>
<span class="line">        <span class="hljs-attribute">add_header</span> Cache-Control <span class="hljs-string">&quot;public, no-transform&quot;</span>;</span>
<span class="line">        <span class="hljs-attribute">add_header</span> X-Content-Type-Options nosniff;</span>
<span class="line">        </span>
<span class="line">        <span class="hljs-comment"># 允许跨域 (如果你的主站和这个域名不同，视频播放器会需要这个)</span></span>
<span class="line">        <span class="hljs-attribute">add_header</span> Access-Control-Allow-Origin *; </span>
<span class="line">    }</span>
<span class="line">}</span></code></pre></div>
<h3 id="为什么这个版本是-最高效-的">为什么这个版本是“最高效”的？</h3>
<ul>
<li>没有冗余逻辑： 把繁琐的“禁止 php、禁止 sh、禁止 jsp”等一长串黑名单，变成了一句干净利落的 if ($uri !~* ...)。非白名单后缀连 Nginx 处理的门槛都进不去。</li>
<li>按优先级排布： Nginx 每次处理请求都是从上到下执行。先把白名单和防盗链放在最前面，遇到恶意请求直接 return 403 中断处理，极其节省服务器资源。如果放在后面，Nginx 还要白白读取文件头信息。</li>
<li>媒体专属引擎： 开启了 mp4 和 sendfile，Nginx 会以流媒体服务器的工作模式来传输文件，极大提升视频的起播速度。</li>
</ul>
<p>替换完成后，记得在终端运行 nginx -t 测试一下。需要我帮你看看如何平滑重载（不中断现有连接）Nginx 吗？</p>
]]></content:encoded><category>Nginx</category></item>
<item><title>微软远程桌面登录失败（3389端口）</title><link>https://demo1.661366.xyz/blog/rdp-3389-login-failed/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/rdp-3389-login-failed/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>如果被连接电脑登录了微软账号，要使用登录账号的邮箱和密码进行登录</description><content:encoded><![CDATA[<p>如果被连接电脑登录了微软账号，要使用登录账号的邮箱和密码进行登录</p>
<p>输入正确的邮箱密码之后还是提示登录失败，则是电脑内存储的是微软账号的就密码，用win+L锁屏，点击忘记密码，登录微软账号即可</p>
<p>再次连接就行了</p>
<p>注意：这里要把对应用户添加进去</p>
<figure class="media is-image" style="--ar:1.3021;--w:1405px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-480.avif 480w, https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-800.avif 800w, https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-1200.avif 1200w, https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-1405.avif 1405w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-1200.webp" srcset="https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-480.webp 480w, https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-800.webp 800w, https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-1200.webp 1200w, https://demo1.661366.xyz/assets/img/fa7aecc7ecfb79a3-fa7aecc7ec-1405.webp 1405w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="1405" height="1079" alt="" fetchpriority="high" decoding="async"></picture></figure>
]]></content:encoded><category>Windows</category><category>排错</category></item>
<item><title>配置服务器HTTPS</title><link>https://demo1.661366.xyz/blog/server-https/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/server-https/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>好的，明白了。您之前尝试安装 epel-release 是为了安装 Certbot。在 Debian/Ubuntu 系统上，安装 Certbot 完全不需要 EPEL，并且有更直接、官方推荐的方法。</description><content:encoded><![CDATA[<p>好的，明白了。您之前尝试安装 epel-release 是为了安装 Certbot。在 Debian/Ubuntu 系统上，安装 Certbot 完全不需要 EPEL，并且有更直接、官方推荐的方法。</p>
<p>强烈推荐使用 Snap 来安装 Certbot，这是 Certbot 官方首选的安装方式。它可以确保您获得最新版本的 Certbot，并且能独立管理依赖，避免与系统其他软件包产生冲突。</p>
<p>以下是在您的 Debian/Ubuntu 系统上安装 Certbot 的详细步骤：</p>
<hr>
<h3 id="使用-snap-安装-certbot-官方推荐">使用 Snap 安装 Certbot (官方推荐)</h3>
<h4 id="第-1-步-准备-snap-环境">第 1 步：准备 Snap 环境</h4>
<ol>
<li>首先，确保您的系统包列表是最新的。<br>Bash <code>sudo apt update sudo apt upgrade -y</code></li>
<li>安装 snapd（如果您的系统还没有安装的话）。<br>Bash <code>sudo apt install snapd -y</code></li>
</ol>
<h4 id="第-2-步-移除旧的-certbot-包-重要">第 2 步：移除旧的 Certbot 包 (重要)</h4>
<p>如果您之前使用 apt 安装过 Certbot，必须先卸载它，以避免冲突。</p>
<p>Bash</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:27"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">sudo</span> apt-get remove certbot</span></code></pre></div>
<h4 id="第-3-步-使用-snap-安装-certbot">第 3 步：使用 Snap 安装 Certbot</h4>
<p>执行以下命令来安装 Certbot。</p>
<p>Bash</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:35"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">sudo</span> snap install --classic certbot</span></code></pre></div>
<ul>
<li>--classic 参数是必需的，它允许 Certbot 在您的系统上进行必要的更改（例如修改 Web 服务器配置）。</li>
</ul>
<h4 id="第-4-步-创建命令链接">第 4 步：创建命令链接</h4>
<p>为了让您能够直接运行 certbot 命令，需要创建一个符号链接。</p>
<p>Bash</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:45"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">sudo</span> <span class="hljs-built_in">ln</span> -s /snap/bin/certbot /usr/bin/certbot</span></code></pre></div>
<hr>
<h3 id="如何使用-certbot-获取证书">如何使用 Certbot 获取证书</h3>
<p>现在 Certbot 已经安装完成，您可以开始用它来为您的网站申请 SSL 证书了。</p>
<ul>
<li>如果您使用的是 Nginx：<br>Bash <code>sudo certbot --nginx</code> Certbot 会自动检测您的 Nginx 配置文件中的域名，引导您选择要为其启用 HTTPS 的域名，并自动配置好 Nginx。</li>
<li>如果您使用的是 Apache：<br>Bash <code>sudo certbot --apache</code> 与 Nginx 类似，Certbot 会自动检测和配置 Apache。</li>
<li>如果您没有使用 Nginx 或 Apache，或者想手动配置：<br>如果您只是想获取证书，然后手动配置您的 Web 服务器，可以运行以下命令：<br>Bash <code>sudo certbot certonly --standalone</code> 这个命令会临时启动一个小型的 Web 服务器来完成域名验证。请注意：在运行此命令之前，您需要停止正在使用 80 端口的任何服务（如 Nginx 或 Apache）。</li>
</ul>
<hr>
<h3 id="检查自动续订">检查自动续订</h3>
<p>通过 Snap 安装的 Certbot 会自动为您设置好证书的自动续订，您无需手动操作。</p>
<p>您可以通过以下命令来测试自动续订是否配置正确（这个命令不会真的续订证书，只是进行一次模拟演练）：</p>
<p>Bash</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:28"><div class="code-head"><span class="code-lang">Shell</span><span class="code-side"></span></div><pre><code class="hljs language-bash"><span class="line"><span class="hljs-built_in">sudo</span> certbot renew --dry-run</span></code></pre></div>
<p>如果测试成功，就说明一切准备就绪了。Certbot 会在证书过期前自动为您续订。</p>
]]></content:encoded><category>HTTPS</category><category>Nginx</category></item>
<item><title>Windows服务器导致的线程假死</title><link>https://demo1.661366.xyz/blog/windows-server-thread-hang/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/windows-server-thread-hang/</guid><pubDate>Fri, 03 Apr 2026 16:00:00 GMT</pubDate><description>问题描述：</description><content:encoded><![CDATA[<h2 id="问题描述">问题描述：</h2>
<p>有个很奇怪的问题，我在8084的后端，打了一个回车，瞬间出了很多日志，然后再去请求就通了</p>
<figure class="media is-image" style="--ar:1.6306;--w:830px"><picture><source type="image/avif" srcset="https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-480.avif 480w, https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-800.avif 800w, https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-830.avif 830w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)"><img src="https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-830.webp" srcset="https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-480.webp 480w, https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-800.webp 800w, https://demo1.661366.xyz/assets/img/4969194764d9d940-4969194764-830.webp 830w" sizes="(min-width: 800px) 720px, calc(100vw - 48px)" width="830" height="509" alt="" fetchpriority="high" decoding="async"></picture></figure>
<h2 id="解决办法">解决办法：</h2>
<p>这是一个非常经典且具有 Windows 特色的现象。你在控制台打一个回车（Enter）瞬间恢复，说明你的后端程序之前被 <strong>Windows 控制台的“快速编辑模式”（QuickEdit Mode）给挂起（Suspend）了</strong>。</p>
<h4 id="1-为什么打回车就通了">1. 为什么打回车就通了？</h4>
<p>这是 Windows 命令行窗口（CMD）的一个“坑”。</p>
<ul>
<li><strong>触发挂起</strong>：当 CMD 开启了“快速编辑模式”时，如果你不小心用鼠标点击了黑窗口的任何地方，或者触发了某些按键，Windows 会认为你要进行“文本选择”或“复制”操作 。</li>
<li><strong>阻塞进程</strong>：为了方便你选择文本，Windows 会<strong>立即暂停（Freeze）该窗口内运行的所有程序</strong> 。</li>
<li><strong>假死表现</strong>：此时程序不再处理任何请求，网络连接被挂起，日志停止滚动，CPU 占用极低。这就是你看到的“运行一天后假死”的真相——其实它只是被 Windows 强行“点穴”了。</li>
<li><strong>回车解锁</strong>：当你按下回车键时，你退出了“选择模式”，Windows 恢复了进程的运行 。之前堆积在缓冲区的所有日志瞬间喷涌而出，排队的网络请求也瞬间恢复了。</li>
</ul>
<h4 id="2-为什么会有-8082-的日志">2. 为什么会有 8082 的日志？</h4>
<p>你之前在 8084 的 Arthas 中看到了 <code>http-nio-8082-Acceptor</code> ，现在有了合理的解释：</p>
<ul>
<li>如果你的多个服务（8082, 8083, 8084）是放在同一个控制台启动的，或者你在 8084 的窗口误操作导致它挂起，且你的日志配置可能存在共享或路径混淆，挂起期间产生的跨进程干扰会导致这种标识错乱。</li>
<li>更有可能的是，当你点击窗口导致挂起时，该进程的线程上下文被冻结在了某一瞬间，导致 Arthas 读取到的线程名出现了缓存错乱。</li>
</ul>
<hr>
<h4 id="彻底解决方法-防止再次-误触-假死">🛠️ 彻底解决方法（防止再次“误触”假死）</h4>
<p>为了避免这种人为导致的“假死”，你必须关闭 CMD 的快速编辑模式：</p>
<h5>方案一：手动修改配置（针对当前窗口）</h5>
<ol>
<li>右键点击 8084 后端运行窗口的 <strong>标题栏</strong>。</li>
<li>选择 <strong>“属性” (Properties)</strong>。</li>
<li>在 <strong>“选项” (Options)</strong> 标签页中，取消勾选 <strong>“快速编辑模式” (Quick Edit Mode)</strong>。</li>
<li>点击确定。</li>
</ol>
]]></content:encoded><category>Windows</category><category>排错</category></item>
<item><title>数据源</title><link>https://demo1.661366.xyz/blog/datasource/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/datasource/</guid><pubDate>Sun, 30 Nov 2025 16:00:00 GMT</pubDate><description>该代码主要涉及两个数据源：主表 （本地库）和 视图 （外部库）。</description><content:encoded><![CDATA[<p>该代码主要涉及两个数据源：主表 <strong><code>t_dwh</code></strong> （本地库）和 视图 <strong><code>cfdbdgps.v_mass_num</code></strong> （外部库）。</p>
<p>以下是具体操作和完整的 SQL 语句：</p>
<h3 id="1-页面加载-查询数据-loaddata">1. 页面加载/查询数据 (<code>LoadData</code>)</h3>
<p>功能：查询未删除的垛位信息，并关联外部视图获取船名和货名。</p>
<p>数据源：t_dwh (主表), cfdbdgps.v_mass_num (关联表)</p>
<p>SQL语句：</p>
<p>SQL</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:108"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">select</span> a.f_id,a.f_dwh,a.f_zdcs,b.vessel_name,b.cargo_name,a.f_adduser,a.f_addtime,a.f_edituser,a.f_edittime </span>
<span class="line"><span class="hljs-keyword">from</span> t_dwh a </span>
<span class="line"><span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> cfdbdgps.v_mass_num b <span class="hljs-keyword">on</span> a.f_dwh<span class="hljs-operator">=</span>b.mass_num </span>
<span class="line"><span class="hljs-keyword">where</span> a.f_deltime <span class="hljs-keyword">is</span> <span class="hljs-keyword">null</span> </span>
<span class="line"><span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> a.f_dwh</span></code></pre></div>
<h3 id="2-添加数据-btnadd-click">2. 添加数据 (<code>btnAdd_Click</code>)</h3>
<p>功能：首先检查垛位号是否存在，如果不存在则插入新记录（使用序列生成ID）。</p>
<p>数据源：t_dwh</p>
<p>SQL语句 (查重)：</p>
<p>SQL</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:71"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">select</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">from</span> t_dwh <span class="hljs-keyword">where</span> f_deltime <span class="hljs-keyword">is</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">and</span> f_dwh<span class="hljs-operator">=</span><span class="hljs-string">&#x27;{TextBox1.Text}&#x27;</span></span></code></pre></div>
<p><strong>SQL语句 (插入)</strong>：</p>
<p>SQL</p>
<div class="codeblock has-lines" style="--ln:1;--rows:2;--cols:81"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">insert into</span> t_dwh (f_id,f_dwh,f_zdcs,f_adduser,f_addtime) </span>
<span class="line"><span class="hljs-keyword">values</span> (seq_t_dwh.nextval,<span class="hljs-string">&#x27;{0}&#x27;</span>,{<span class="hljs-number">1</span>},<span class="hljs-string">&#x27;{2}&#x27;</span>,to_date(<span class="hljs-string">&#x27;{3}&#x27;</span>,<span class="hljs-string">&#x27;yyyy-mm-dd hh24:mi:ss&#x27;</span>))</span></code></pre></div>
<h3 id="3-修改数据-btnamend-click">3. 修改数据 (<code>btnAmend_Click</code>)</h3>
<p>功能：修改选中的记录。修改前会检查新的垛位号是否与其他记录冲突（排除自身ID）。</p>
<p>数据源：t_dwh</p>
<p>SQL语句 (查重)：</p>
<p>SQL</p>
<div class="codeblock" style="--ln:1;--rows:1;--cols:111"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">select</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">from</span> t_dwh <span class="hljs-keyword">where</span> f_deltime <span class="hljs-keyword">is</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">and</span> f_dwh<span class="hljs-operator">=</span><span class="hljs-string">&#x27;{TextBox1.Text}&#x27;</span> <span class="hljs-keyword">and</span> f_id<span class="hljs-operator">!=</span>{list.Items[k].Cells[<span class="hljs-number">2</span>].Text}</span></code></pre></div>
<p><strong>SQL语句 (更新)</strong>：</p>
<p>SQL</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:94"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">update</span> t_dwh </span>
<span class="line"><span class="hljs-keyword">set</span> f_dwh<span class="hljs-operator">=</span><span class="hljs-string">&#x27;{0}&#x27;</span>,f_zdcs<span class="hljs-operator">=</span>{<span class="hljs-number">1</span>},F_EDITUSER<span class="hljs-operator">=</span><span class="hljs-string">&#x27;{2}&#x27;</span>,F_EDITTIME<span class="hljs-operator">=</span>to_date(<span class="hljs-string">&#x27;{3}&#x27;</span>,<span class="hljs-string">&#x27;yyyy-mm-dd hh24:mi:ss&#x27;</span>) </span>
<span class="line"><span class="hljs-keyword">where</span> f_id<span class="hljs-operator">=</span>{<span class="hljs-number">4</span>}</span></code></pre></div>
<h3 id="4-删除数据-btndel-click">4. 删除数据 (<code>btnDel_Click</code>)</h3>
<p>功能：逻辑删除（软删除），更新删除人和删除时间，不物理删除数据。</p>
<p>数据源：t_dwh</p>
<p>SQL语句：</p>
<p>SQL</p>
<div class="codeblock has-lines" style="--ln:1;--rows:3;--cols:69"><div class="code-head"><span class="code-lang">SQL</span><span class="code-side"></span></div><pre><code class="hljs language-sql"><span class="line"><span class="hljs-keyword">update</span> t_dwh </span>
<span class="line"><span class="hljs-keyword">set</span> F_DELUSER<span class="hljs-operator">=</span><span class="hljs-string">&#x27;{0}&#x27;</span>,F_DELTIME<span class="hljs-operator">=</span>to_date(<span class="hljs-string">&#x27;{1}&#x27;</span>,<span class="hljs-string">&#x27;yyyy-mm-dd hh24:mi:ss&#x27;</span>) </span>
<span class="line"><span class="hljs-keyword">where</span> f_id<span class="hljs-operator">=</span>{<span class="hljs-number">2</span>}</span></code></pre></div>
]]></content:encoded><category>Java</category></item>
<item><title>历史作业接口文档</title><link>https://demo1.661366.xyz/blog/history-job-api-doc/</link><guid isPermaLink="true">https://demo1.661366.xyz/blog/history-job-api-doc/</guid><pubDate>Sun, 02 Nov 2025 16:00:00 GMT</pubDate><description>📈 作业历史数据 API 文档 基础路径 (Base Path)</description><content:encoded><![CDATA[<h2 id="作业历史数据-api-文档">📈 作业历史数据 API 文档</h2>
<h4 id="基础路径-base-path">基础路径 (Base Path)</h4>
<p>所有接口的基础路径为：<code>/ysuwx/job-history</code></p>
<hr>
<h3 id="1-获取所有历史数据-周-月-年">1. 获取所有历史数据 (周/月/年)</h3>
<p>此接口用于获取所有作业类型（卸船、疏港、装车、精混）的<strong>周、月、年</strong>三个维度的完整历史统计数据，用于页面的默认展示。</p>
<ul>
<li><strong>接口：</strong> <code>GET /all</code></li>
<li><strong>别名：</strong> <code>GET /test</code></li>
<li><strong>请求参数：</strong> 无</li>
</ul>
<h4 id="响应-success-200">响应 (Success 200)</h4>
<p>返回 <code>R.ok()</code> 包装的 <code>data</code> 对象，<code>data</code> 字段的结构为 <code>JobHistoryDto</code>。</p>
<p>JSON</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:21"><div class="code-head"><span class="code-lang">JSON</span><span class="code-side"></span></div><pre><code class="hljs language-json"><span class="line"><span class="hljs-punctuation">{</span></span>
<span class="line">    <span class="hljs-attr">&quot;msg&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;success&quot;</span><span class="hljs-punctuation">,</span></span>
<span class="line">    <span class="hljs-attr">&quot;code&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span></span>
<span class="line">    <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> ... <span class="hljs-punctuation">}</span></span>
<span class="line"><span class="hljs-punctuation">}</span></span></code></pre></div>
<h4 id="data-对象详解"><code>data</code> 对象详解</h4>
<p><code>data</code> 对象包含四大模块，每个模块都有周、月、年数据：</p>
<h5>1. 卸船数据 (xc)</h5>
<div class="table-wrap"><table>
<thead>
<tr>
<th><strong>字段</strong></th>
<th><strong>类型</strong></th>
<th><strong>说明</strong></th>
<th><strong>示例值</strong></th>
</tr>
</thead>
<tbody><tr>
<td><code>xcTotal</code></td>
<td>BigDecimal</td>
<td>卸船: 周总作业量 (单位: 万吨)</td>
<td><code>104.98</code></td>
</tr>
<tr>
<td><code>xcWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>卸船: 近7天X轴 (日期 &quot;MM-dd&quot;)</td>
<td><code>[&quot;10-28&quot;, ...]</code></td>
</tr>
<tr>
<td><code>xcWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>卸船: 近7天Y轴 (作业量, 吨)</td>
<td><code>[174147, ...]</code></td>
</tr>
<tr>
<td><code>xcMonthXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>卸船: 近5月X轴 (日期 &quot;yyyy-MM&quot;)</td>
<td><code>[&quot;2025-07&quot;, ...]</code></td>
</tr>
<tr>
<td><code>xcMonthData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>卸船: 近5月Y轴 (作业量, 吨)</td>
<td><code>[196243, ...]</code></td>
</tr>
<tr>
<td><code>xcMonthTotal</code></td>
<td>BigDecimal</td>
<td>卸船: 月总作业量 (单位: 万吨)</td>
<td><code>10182987</code></td>
</tr>
<tr>
<td><code>xcYearXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>卸船: 近5年X轴 (年份 &quot;yyyy&quot;)</td>
<td><code>[&quot;2021&quot;, ...]</code></td>
</tr>
<tr>
<td><code>xcYearData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>卸船: 近5年Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>xcYearTotal</code></td>
<td>BigDecimal</td>
<td>卸船: 年总作业量 (单位: 万吨)</td>
<td><code>10182987</code></td>
</tr>
</tbody></table></div>
<h5>2. 疏港数据 (sg)</h5>
<div class="table-wrap"><table>
<thead>
<tr>
<th><strong>字段</strong></th>
<th><strong>类型</strong></th>
<th><strong>说明</strong></th>
<th><strong>示例值</strong></th>
</tr>
</thead>
<tbody><tr>
<td><code>sgTotal</code></td>
<td>BigDecimal</td>
<td>疏港: 周总作业量 (单位: 万吨)</td>
<td><code>34.8</code></td>
</tr>
<tr>
<td><code>sgWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>疏港: 近7天X轴 (日期 &quot;MM-dd&quot;)</td>
<td><code>[&quot;10-28&quot;, ...]</code></td>
</tr>
<tr>
<td><code>sgWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>疏港: 近7天Y轴 (作业量, 吨)</td>
<td><code>[44599.9, ...]</code></td>
</tr>
<tr>
<td><code>sgMonthXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>疏港: 近5月X轴 (日期 &quot;yyyy-MM&quot;)</td>
<td><code>[&quot;2025-07&quot;, ...]</code></td>
</tr>
<tr>
<td><code>sgMonthData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>疏港: 近5月Y轴 (作业量, 吨)</td>
<td><code>[1365099.85, ...]</code></td>
</tr>
<tr>
<td><code>sgMonthTotal</code></td>
<td>BigDecimal</td>
<td>疏港: 月总作业量 (单位: 万吨)</td>
<td><code>7274917.55</code></td>
</tr>
<tr>
<td><code>sgYearXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>疏港: 近5年X轴 (年份 &quot;yyyy&quot;)</td>
<td><code>[&quot;2021&quot;, ...]</code></td>
</tr>
<tr>
<td><code>sgYearData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>疏港: 近5年Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>sgYearTotal</code></td>
<td>BigDecimal</td>
<td>疏港: 年总作业量 (单位: 万吨)</td>
<td><code>15564728.9</code></td>
</tr>
</tbody></table></div>
<h5>3. 装车数据 (zc)</h5>
<div class="table-wrap"><table>
<thead>
<tr>
<th><strong>字段</strong></th>
<th><strong>类型</strong></th>
<th><strong>说明</strong></th>
<th><strong>示例值</strong></th>
</tr>
</thead>
<tbody><tr>
<td><code>zcTotal</code></td>
<td>BigDecimal</td>
<td>装车: 周总作业量 (单位: 万吨)</td>
<td><code>0</code></td>
</tr>
<tr>
<td><code>zcWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>装车: 近7天X轴 (日期 &quot;MM-dd&quot;)</td>
<td><code>[&quot;10-28&quot;, ...]</code></td>
</tr>
<tr>
<td><code>zcWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>装车: 近7天Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>zcMonthXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>装车: 近5月X轴 (日期 &quot;yyyy-MM&quot;)</td>
<td><code>[&quot;2025-07&quot;, ...]</code></td>
</tr>
<tr>
<td><code>zcMonthData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>装车: 近5月Y轴 (作业量, 吨)</td>
<td><code>[13749.62, ...]</code></td>
</tr>
<tr>
<td><code>zcMonthTotal</code></td>
<td>BigDecimal</td>
<td>装车: 月总作业量 (单位: 万吨)</td>
<td><code>58934.54</code></td>
</tr>
<tr>
<td><code>zcYearXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>装车: 近5年X轴 (年份 &quot;yyyy&quot;)</td>
<td><code>[&quot;2021&quot;, ...]</code></td>
</tr>
<tr>
<td><code>zcYearData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>装车: 近5年Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>zcYearTotal</code></td>
<td>BigDecimal</td>
<td>装车: 年总作业量 (单位: 万吨)</td>
<td><code>58934.54</code></td>
</tr>
</tbody></table></div>
<h5>4. 精混数据 (jh)</h5>
<div class="table-wrap"><table>
<thead>
<tr>
<th><strong>字段</strong></th>
<th><strong>类型</strong></th>
<th><strong>说明</strong></th>
<th><strong>示例值</strong></th>
</tr>
</thead>
<tbody><tr>
<td><code>jhTotal</code></td>
<td>BigDecimal</td>
<td>精混: 周总作业量 (单位: 万吨)</td>
<td><code>0</code></td>
</tr>
<tr>
<td><code>jhWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>精混: 近7天X轴 (日期 &quot;MM-dd&quot;)</td>
<td><code>[&quot;10-28&quot;, ...]</code></td>
</tr>
<tr>
<td><code>jhWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>精混: 近7天Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>jhMonthXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>精混: 近5月X轴 (日期 &quot;yyyy-MM&quot;)</td>
<td><code>[&quot;2025-07&quot;, ...]</code></td>
</tr>
<tr>
<td><code>jhMonthData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>精混: 近5月Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>jhMonthTotal</code></td>
<td>BigDecimal</td>
<td>精混: 月总作业量 (单位: 万吨)</td>
<td><code>1537266.19</code></td>
</tr>
<tr>
<td><code>jhYearXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>精混: 近5年X轴 (年份 &quot;yyyy&quot;)</td>
<td><code>[&quot;2021&quot;, ...]</code></td>
</tr>
<tr>
<td><code>jhYearData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>精混: 近5年Y轴 (作业量, 吨)</td>
<td><code>[0, ...]</code></td>
</tr>
<tr>
<td><code>jhYearTotal</code></td>
<td>BigDecimal</td>
<td>精混: 年总作业量 (单位: 万吨)</td>
<td><code>1537266.19</code></td>
</tr>
</tbody></table></div>
<hr>
<h3 id="2-按自定义时间范围查询数据">2. 按自定义时间范围查询数据</h3>
<p>此接口用于按<strong>自定义时间范围</strong>（例如 &quot;2025-10-15&quot; 到 &quot;2025-11-03&quot;）查询所有作业类型的<strong>日度</strong>统计数据。</p>
<ul>
<li><strong>接口：</strong> <code>GET /by-time</code></li>
<li><strong>请求参数：</strong> (Query)</li>
</ul>
<div class="table-wrap"><table>
<thead>
<tr>
<th><strong>参数</strong></th>
<th><strong>类型</strong></th>
<th><strong>是否必填</strong></th>
<th><strong>说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td><code>startTime</code></td>
<td>String</td>
<td><strong>是</strong></td>
<td>开始时间, 格式: &quot;yyyy-MM-dd&quot;</td>
</tr>
<tr>
<td><code>endTime</code></td>
<td>String</td>
<td><strong>是</strong></td>
<td>结束时间, 格式: &quot;yyyy-MM-dd&quot;</td>
</tr>
</tbody></table></div>
<h4 id="响应-success-200-2">响应 (Success 200)</h4>
<p>返回 <code>R.ok()</code> 包装的 <code>data</code> 对象。</p>
<p><strong>重要提示：</strong> 此接口的 <code>data</code> 对象是一个<strong>部分填充</strong>的 <code>JobHistoryDto</code>。</p>
<ul>
<li>它<strong>只会</strong>填充 <code>...Total</code>、<code>...WeekXAxis</code> 和 <code>...WeekData</code> 字段。</li>
<li><code>...Week...</code> 字段在此处被复用，<strong>用于存储自定义时间范围的日度数据</strong>。</li>
<li>所有 <code>...Month...</code> 和 <code>...Year...</code> 相关的字段将<strong>始终为 <code>null</code></strong>。</li>
</ul>
<p>JSON</p>
<div class="codeblock has-lines" style="--ln:1;--rows:5;--cols:21"><div class="code-head"><span class="code-lang">JSON</span><span class="code-side"></span></div><pre><code class="hljs language-json"><span class="line"><span class="hljs-punctuation">{</span></span>
<span class="line">    <span class="hljs-attr">&quot;msg&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;success&quot;</span><span class="hljs-punctuation">,</span></span>
<span class="line">    <span class="hljs-attr">&quot;code&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span></span>
<span class="line">    <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> ... <span class="hljs-punctuation">}</span></span>
<span class="line"><span class="hljs-punctuation">}</span></span></code></pre></div>
<h4 id="data-对象详解-by-time-模式"><code>data</code> 对象详解 (by-time 模式)</h4>
<div class="table-wrap"><table>
<thead>
<tr>
<th><strong>字段</strong></th>
<th><strong>类型</strong></th>
<th><strong>说明</strong></th>
<th><strong>示例值</strong></th>
</tr>
</thead>
<tbody><tr>
<td><code>xcTotal</code></td>
<td>BigDecimal</td>
<td>卸船: <strong>范围内</strong>总作业量</td>
<td><code>2707719</code></td>
</tr>
<tr>
<td><code>xcWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>卸船: <strong>范围内</strong>X轴 (日期 &quot;yyyy-MM-dd&quot;)</td>
<td><code>[&quot;2025-10-16&quot;, ...]</code></td>
</tr>
<tr>
<td><code>xcWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>卸船: <strong>范围内</strong>Y轴 (作业量, 吨)</td>
<td><code>[192716, ...]</code></td>
</tr>
<tr>
<td><code>sgTotal</code></td>
<td>BigDecimal</td>
<td>疏港: <strong>范围内</strong>总作业量</td>
<td><code>900906.90</code></td>
</tr>
<tr>
<td><code>sgWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>疏港: <strong>范围内</strong>X轴 (日期 &quot;yyyy-MM-dd&quot;)</td>
<td><code>[&quot;2025-10-15&quot;, ...]</code></td>
</tr>
<tr>
<td><code>sgWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>疏港: <strong>范围内</strong>Y轴 (作业量, 吨)</td>
<td><code>[46315.7, ...]</code></td>
</tr>
<tr>
<td><code>zcTotal</code></td>
<td>BigDecimal</td>
<td>装车: <strong>范围内</strong>总作业量</td>
<td><code>0</code></td>
</tr>
<tr>
<td><code>zcWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>装车: <strong>范围内</strong>X轴 (日期 &quot;yyyy-MM-dd&quot;)</td>
<td><code>[]</code></td>
</tr>
<tr>
<td><code>zcWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>装车: <strong>范围内</strong>Y轴 (作业量, 吨)</td>
<td><code>[]</code></td>
</tr>
<tr>
<td><code>jhTotal</code></td>
<td>BigDecimal</td>
<td>精混: <strong>范围内</strong>总作业量</td>
<td><code>54071</code></td>
</tr>
<tr>
<td><code>jhWeekXAxis</code></td>
<td>List&lt;String&gt;</td>
<td>精混: <strong>范围内</strong>X轴 (日期 &quot;yyyy-MM-dd&quot;)</td>
<td><code>[&quot;2025-10-15&quot;, ...]</code></td>
</tr>
<tr>
<td><code>jhWeekData</code></td>
<td>List&lt;BigDecimal&gt;</td>
<td>精混: <strong>范围内</strong>Y轴 (作业量, 吨)</td>
<td><code>[9271, ...]</code></td>
</tr>
<tr>
<td><code>...Month...</code></td>
<td>-</td>
<td>(所有月度字段)</td>
<td><code>null</code></td>
</tr>
<tr>
<td><code>...Year...</code></td>
<td>-</td>
<td>(所有年度字段)</td>
<td><code>null</code></td>
</tr>
</tbody></table></div>
<h4 id="响应-error-500">响应 (Error 500)</h4>
<p>如果请求的时间范围不合法（例如超过30天限制，该限制在 <code>JobHistoryServiceImpl.java</code> 中定义），将返回错误信息。</p>
<p>JSON</p>
<div class="codeblock has-lines" style="--ln:1;--rows:4;--cols:65"><div class="code-head"><span class="code-lang">JSON</span><span class="code-side"></span></div><pre><code class="hljs language-json"><span class="line"><span class="hljs-punctuation">{</span></span>
<span class="line">    <span class="hljs-attr">&quot;msg&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;查询时间范围不能超过30天，当前查询范围：35天（...）&quot;</span><span class="hljs-punctuation">,</span></span>
<span class="line">    <span class="hljs-attr">&quot;code&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">500</span></span>
<span class="line"><span class="hljs-punctuation">}</span></span></code></pre></div>
]]></content:encoded><category>接口文档</category></item>
</channel>
</rss>
