<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Quan Zhuo's Blog</title>
    <description>我的 Github Pages</description>
    <link>http://quanzhuo.github.io/</link>
    <atom:link href="http://quanzhuo.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 08 May 2022 03:08:46 +0000</pubDate>
    <lastBuildDate>Sun, 08 May 2022 03:08:46 +0000</lastBuildDate>
    <generator>Jekyll v3.9.2</generator>
    
      <item>
        <title>Android Studio导入Framework.jar</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#描述&quot; id=&quot;markdown-toc-描述&quot;&gt;描述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#解决办法&quot; id=&quot;markdown-toc-解决办法&quot;&gt;解决办法&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-更改模块级的配置文件&quot; id=&quot;markdown-toc-1-更改模块级的配置文件&quot;&gt;1. 更改模块级的配置文件&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-更改项目级别配置文件&quot; id=&quot;markdown-toc-2-更改项目级别配置文件&quot;&gt;2. 更改项目级别配置文件&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;描述&quot;&gt;描述&lt;/h2&gt;

&lt;p&gt;很多时候在开发系统 App 的时候需要用到 Android Framework 中的接口，此时就需要导入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;framework.jar&lt;/code&gt;。
由于默认情况下，Android Studio 在解析类依赖的时候会将 Android Sdk 放到最前面。如果 Sdk
中也有你要用到的类，但是你要用到的方法却是隐藏的，这就会导致 Android Studio 报找不到方法的错误。&lt;/p&gt;

&lt;h2 id=&quot;解决办法&quot;&gt;解决办法&lt;/h2&gt;

&lt;p&gt;打开 Android Studio 工程模块目录下的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;module-name&amp;gt;.iml&lt;/code&gt;，如果模块名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app&lt;/code&gt;，默认情况下
该文件为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app.iml&lt;/code&gt;。 打开该文件，找到以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;orderEntry type=&quot;library&quot;&lt;/code&gt; 开头的行，可以发现
第一个就是 Android Sdk。我们只需要将该行移动到最后面即可。但是这样做等你清理项目之后改动应该
会丢失，我们可以更改 build.gradle 使系统自动在build之前完成该任务。&lt;/p&gt;

&lt;h3 id=&quot;1-更改模块级的配置文件&quot;&gt;1. 更改模块级的配置文件&lt;/h3&gt;

&lt;p&gt;更改模块级配置文件，让 gradle 在开始编译之前，自动将 Android Sdk 移动到最后面。在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/build.gradle&lt;/code&gt;
的最外层添加如下代码：&lt;/p&gt;

&lt;div class=&quot;language-groovy highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;preBuild&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;doLast&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imlFile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;project&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;.iml&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Change '&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;project&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'.iml order'&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parsedXml&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XmlParser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imlFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jdkNode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parsedXml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;orderEntry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;find&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'@type'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jdk'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parsedXml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdkNode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sdkString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Android API &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compileSdkVersion&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;android-&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot; Platform&quot;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parsedXml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'orderEntry'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'type'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jdk'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jdkName'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sdkString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jdkType'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Android SDK'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;groovy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;xml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;XmlUtil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parsedXml&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FileOutputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imlFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileNotFoundException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// nop, iml not found&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;截图如下：
&lt;img src=&quot;/asserts/images/android/android-studio-import-framework/prebuild.png&quot; alt=&quot;截图&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;2-更改项目级别配置文件&quot;&gt;2. 更改项目级别配置文件&lt;/h3&gt;

&lt;p&gt;在项目级别的 build.gradle 里面配置提高编译时 lib 库的引用优先级，如果有多个 jar 包就用分号隔开。&lt;/p&gt;

&lt;div class=&quot;language-groovy highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;gradle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;projectsEvaluated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;JavaCompile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;compilerArgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'-Xbootclasspath/p:app\\libs\\framework.jar'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;截图如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/asserts/images/android/android-studio-import-framework/improve-framework.png&quot; alt=&quot;截图&quot; /&gt;&lt;/p&gt;

&lt;p&gt;更改之后再次编译，所有标红的地方都会自动消失。&lt;/p&gt;
</description>
        <pubDate>Mon, 29 Apr 2019 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2019/04/29/android-studio-import-framework/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2019/04/29/android-studio-import-framework/</guid>
        
        <category>Android</category>
        
        <category>Studio</category>
        
        
        <category>Android</category>
        
      </item>
    
      <item>
        <title>Devhelp 不显示代码的解决办法</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#解决办法&quot; id=&quot;markdown-toc-解决办法&quot;&gt;解决办法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Devhelp 是 Linux 系统上一个十分有用的开发者文档查看器。但是默认安装的 Devhelp 存在一个 bug
导致无法显示例子程序的代码。在本该显示代码的地方会显示&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FIXME: MISSING XINCLUDE CONTENT
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;解决办法&quot;&gt;解决办法&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;首先查看已安装的 libgtk-3-dev 包的版本&lt;/p&gt;

    &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; dpkg &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; libgtk-3-dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;例如我的机器上的 gtk 版本为 3.18.9&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;到 &lt;a href=&quot;https://developer.gnome.org/gtk3/&quot;&gt;gnome 开发者网站&lt;/a&gt; 上去下载对应版本的文档&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;删除默认安装的 gtk 开发文档&lt;/p&gt;

    &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /usr/share/doc/libgtk-3-doc/gtk3/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将下载下来的文档解压&lt;/p&gt;

    &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;xzvf gtk3-html-3.18.9.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将解压出来的内容复制到默认文档路径下&lt;/p&gt;

    &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; gtk3-html-3.18.9/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /usr/share/doc/libgtk-3-doc/gtk3/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;然后重新打开 Devhelp 就可以看到例子程序中的源代码了。&lt;/p&gt;
</description>
        <pubDate>Mon, 26 Jun 2017 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2017/06/26/devhelp/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2017/06/26/devhelp/</guid>
        
        
        <category>Linux</category>
        
      </item>
    
      <item>
        <title>使用 rsync 同步文件夹</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#rsync-简介&quot; id=&quot;markdown-toc-rsync-简介&quot;&gt;rsync 简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#语法&quot; id=&quot;markdown-toc-语法&quot;&gt;语法&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#选项&quot; id=&quot;markdown-toc-选项&quot;&gt;选项&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#用法示例&quot; id=&quot;markdown-toc-用法示例&quot;&gt;用法示例：&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#更多信息&quot; id=&quot;markdown-toc-更多信息&quot;&gt;更多信息&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;很多时候我们需要两个文件夹的内容保持完全一致。比如说在 Android 平台开发中，当上游厂商
release 出新的版本的代码的时候，就需要合并新的代码到我们的代码仓库中。对于一两个文件的
对比，使用任何方法都可以。但是对于 Android 源码来说，加上仓库一共有 50 ~ 60 GB 的代码，
上百万的文件，此时就需要选取合适的工具。本文介绍怎么使用 rsync 命令来自动对比并 Merge
代码。&lt;/p&gt;

&lt;h2 id=&quot;rsync-简介&quot;&gt;rsync 简介&lt;/h2&gt;

&lt;p&gt;下面是 rsync 的 Manual Page 中的简介。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;rsync - a fast, versatile, remote (and local) file-copying tool&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;上面的一句话简单明了的概括了 rsync 的功能特性：快速，功能强大，远程复制，以及本地复制。
rsync 可以说是一个功能强大的复制工具。本文我们只会介绍一些使用 rsync 在本地 Merge 代码
时会用到的选项。&lt;/p&gt;

&lt;h2 id=&quot;语法&quot;&gt;语法&lt;/h2&gt;

&lt;p&gt;rsync 在本地 Merge 两个文件夹的语法如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; rsync [OPTION...] SRC... DEST
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;选项&quot;&gt;选项&lt;/h2&gt;

&lt;p&gt;下面是一些合并代码会用到的选项&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v, --verbose&lt;/code&gt;：详尽的输出&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-i, --itemize-changes&lt;/code&gt;: 输出文件变化的总结&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-C, --cvs-exclude&lt;/code&gt;：按照版本控制系统的模式自动忽略文件&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-c, --checksum&lt;/code&gt;：比较校验和，而不是时间和大小&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-n, --dry-run&lt;/code&gt;：只打印出差异，而不进行 Merge&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--delete&lt;/code&gt;：删除只在 dest 中存在的文件&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--exclude&lt;/code&gt;：跳过某些文件或者文件夹&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--no-times&lt;/code&gt;：如果两个文件只是时间不同，则不改变 dest 的时间&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--no-perms&lt;/code&gt;：如果两个文件只是权限不同，则不改变 dest 的权限&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;用法示例&quot;&gt;用法示例：&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;rsync -av -C --exclude=.repo/ --exclude=.git/ -c -i --no-times --no-perms --delete src/ dest/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;更多信息&quot;&gt;更多信息&lt;/h2&gt;

&lt;p&gt;上面只介绍了 rsync 全部功能的一小部分，更多的用法请查看 rsync 的官方手册。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;man rsync
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Thu, 09 Feb 2017 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2017/02/09/use-rsync/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2017/02/09/use-rsync/</guid>
        
        
        <category>tools</category>
        
      </item>
    
      <item>
        <title>Android 镜像的解包</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#systemimg&quot; id=&quot;markdown-toc-systemimg&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#userdataimg&quot; id=&quot;markdown-toc-userdataimg&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;userdata.img&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ramdiskimg&quot; id=&quot;markdown-toc-ramdiskimg&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ramdisk.img&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以 AOSP 为例，一次典型的构建会生成以下 image （镜像文件）：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt;，
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ramdisk.img&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;userdata.img&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cache.img&lt;/code&gt;，这些 image 一起构成了 Android 系统。下
面将分别介绍这些 image 如何解包。&lt;/p&gt;

&lt;h2 id=&quot;systemimg&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;system image 是安卓系统中最大的一个镜像文件，里面包含了所有的 app，二进制可执行文件，
库文件，字体，配置文件，安卓框架核心库等等。AP 部分除了 kernel 之外，基本上都包含在
system.image 中。&lt;/p&gt;

&lt;p&gt;system.img 文件是由文件夹 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out/target/product/$PRODUCT/system&lt;/code&gt; 下面的所有文件打包而
来的。要想解包 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt; 文件，可以先用 file 命令看一下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt; 的格式：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;file system.img
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出显示：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;system.img: Linux rev 1.0 ext4 filesystem data, UUID=da594c53-9beb-f85c-85c5-cedf76546f7a,
volume name “system” (needs journal recovery) (extents) (large files)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;可见 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt; 是 ext4 文件系统，那么就可以直接使用 mount 指令挂载之。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mkdir system-img
sudo mount system.img system-img
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果使用 file 查看 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system.img&lt;/code&gt; 的输出不为 ext4 文件系统，而类似于下面：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Android sparse image, version: 1.0, Total of 393216 4096-byte output blocks
in 1765 input chunks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Android sparse image 和 ext4 文件系统之间可以通过 simg2img 工具相互转换：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;simg2img &amp;lt;sparse-img-file&amp;gt; &amp;lt;ext4-fs&amp;gt;
simg2img &amp;lt;ext4-fs&amp;gt; &amp;lt;sparse-img-file&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;转换为 ext4 格式之后再 mount 即可。&lt;/p&gt;

&lt;h2 id=&quot;userdataimg&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;userdata.img&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;userdata.img 是基于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out/target/product/$PRODUCT/data&lt;/code&gt; 文件夹下面的内容生成的。解包
userdata.img 与 解包 system.img 的方法一致，不再赘述。&lt;/p&gt;

&lt;h2 id=&quot;ramdiskimg&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ramdisk.img&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;ramdisk.img 是基于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out/target/product/$PRODUCT/root&lt;/code&gt; 文件夹下面的内容制作的。root
文件夹是 linux 系统的根文件系统。按照以下步骤可以解包。&lt;/p&gt;

&lt;p&gt;首先仍然使用 file 查看类型：&lt;/p&gt;

&lt;p&gt;file ramdisk.img&lt;/p&gt;

&lt;p&gt;得到如下输出：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;ramdisk.img: gzip compressed data, from Unix&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;可见是经过 gzip 压缩的数据，那么就先解压。因为 ramdisk.img 并不是以 gzip 格式压缩档常
见的后缀 .gz 结尾的，所以，我们需要给 gzip 传递一个 -S 参数：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gzip -S .img -d ramdisk.img
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后检查得到的 ramdisk 文件的类型：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;file ramdisk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;ramdisk 现在是一个 cpio 档：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;ramdisk: ASCII cpio archive (SVR4 with no CRC)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;cpio 档使用下面的命令可以得到其内容：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cpio -i -F ramdisk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Thu, 19 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2017/01/19/android-images/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2017/01/19/android-images/</guid>
        
        
        <category>Android</category>
        
      </item>
    
      <item>
        <title>jack server 总结</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#setup-for-mac-os&quot; id=&quot;markdown-toc-setup-for-mac-os&quot;&gt;Setup for Mac OS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#installing-the-server&quot; id=&quot;markdown-toc-installing-the-server&quot;&gt;Installing the server&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#starting-the-server&quot; id=&quot;markdown-toc-starting-the-server&quot;&gt;Starting the server&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#client-info&quot; id=&quot;markdown-toc-client-info&quot;&gt;Client info&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#jack-settings-file&quot; id=&quot;markdown-toc-jack-settings-file&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.jack-settings&lt;/code&gt; file&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#server-info&quot; id=&quot;markdown-toc-server-info&quot;&gt;Server info&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#configproperties-file&quot; id=&quot;markdown-toc-configproperties-file&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.properties&lt;/code&gt; file&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#server-logs&quot; id=&quot;markdown-toc-server-logs&quot;&gt;Server logs&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#admin&quot; id=&quot;markdown-toc-admin&quot;&gt;Admin&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#transitioning-from-server-11-eg-marshmallow-to-server-13-eg-n&quot; id=&quot;markdown-toc-transitioning-from-server-11-eg-marshmallow-to-server-13-eg-n&quot;&gt;Transitioning from server 1.1 (e.g. Marshmallow) to server 1.3 (e.g. N)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#troubleshooting&quot; id=&quot;markdown-toc-troubleshooting&quot;&gt;Troubleshooting&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#if-compilation-fails-on-no-jack-server-running&quot; id=&quot;markdown-toc-if-compilation-fails-on-no-jack-server-running&quot;&gt;If compilation fails on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;No Jack server running&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#if-your-computer-becomes-unresponsive-during-compilation&quot; id=&quot;markdown-toc-if-your-computer-becomes-unresponsive-during-compilation&quot;&gt;If your computer becomes unresponsive during compilation:&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#if-you-experience-jack-compilations-failing-on-out-of-memory-error&quot; id=&quot;markdown-toc-if-you-experience-jack-compilations-failing-on-out-of-memory-error&quot;&gt;If you experience Jack compilations failing on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Out of memory error.&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#if-you-have-trouble-starting-the-server&quot; id=&quot;markdown-toc-if-you-have-trouble-starting-the-server&quot;&gt;If you have trouble starting the server&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#if-your-commands-fails-on&quot; id=&quot;markdown-toc-if-your-commands-fails-on&quot;&gt;If your commands fails on&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#if-your-compilation-gets-stuck-without-any-progress&quot; id=&quot;markdown-toc-if-your-compilation-gets-stuck-without-any-progress&quot;&gt;If your compilation gets stuck without any progress&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This documentation describes usage of Jack server 1.3-a11. The Jack server’s
goal is to handle a pool of Jack compiler instances in order to limit memory
usage and benefit from already warm instances.&lt;/p&gt;

&lt;h2 id=&quot;setup-for-mac-os&quot;&gt;Setup for Mac OS&lt;/h2&gt;

&lt;p&gt;Jack server requires initial setup on Mac OS:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Install MacPorts from &lt;a href=&quot;http://www.macports.org/install.php&quot;&gt;macports.org&lt;/a&gt;
  Make sure that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/local/bin appears&lt;/code&gt; in your path before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/bin&lt;/code&gt;.
  If not, please add the following to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bash_profile&lt;/code&gt; file (If you do
  not have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bash_profile&lt;/code&gt; file in your home directory, create one):&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  $ export PATH=/opt/local/bin:$PATH
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Get curl package from MacPorts:&lt;/p&gt;

    &lt;p&gt;$ POSIXLY_CORRECT=1 sudo port install curl +ssl&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;installing-the-server&quot;&gt;Installing the server&lt;/h2&gt;

&lt;p&gt;Jack server is automatically installed and started by Android build. If you need
to do the installation manually use:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ jack-admin install-server &amp;lt;launcher.jar&amp;gt; &amp;lt;server.jar&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;starting-the-server&quot;&gt;Starting the server&lt;/h2&gt;

&lt;p&gt;You may need to start the Jack server manually. Use&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jack-admin start-server.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;client-info&quot;&gt;Client info&lt;/h2&gt;

&lt;p&gt;The client is a bash script simply named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack&lt;/code&gt;. It can be configured in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-settings&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;jack-settings-file&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.jack-settings&lt;/code&gt; file&lt;/h3&gt;

&lt;p&gt;This file contains script shell variables:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_HOST&lt;/code&gt;: IP address of the server. by default: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_HOST=localhost&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_SERVICE&lt;/code&gt;: Server service TCP port number. Needs to match the
  service port number defined in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt; on the
  server host (See &lt;strong&gt;Server info&lt;/strong&gt; below).by default: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_SERVICE=8076&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_ADMIN&lt;/code&gt;: Server admin TCP port number. Needs to match the admin
  port number defined in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt; on the server
  host (See &lt;strong&gt;Server info&lt;/strong&gt; below). by default: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_ADMIN=8077&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SETTING_VERSION&lt;/code&gt;: Internal, do not modify.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;server-info&quot;&gt;Server info&lt;/h2&gt;

&lt;p&gt;The server is composed of 2 jars named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-server.jar&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-launcher.jar&lt;/code&gt;.
Check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Admin&lt;/code&gt; section to know how to install and administrate the Jack server.
The server can also be configured in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;configproperties-file&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.properties&lt;/code&gt; file&lt;/h3&gt;

&lt;p&gt;It contains Jack server configuration properties. Modifications to those settings
are taken into account after restarting the server. Description with default
values follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.max-service=&amp;lt;number&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Maximum number of simultaneous Jack tasks. Default is 4.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.max-jars-size=&amp;lt;size-in-bytes&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Maximum size for Jars, in bytes. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; means no limit. Default is 100 MiB.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.time-out=&amp;lt;time-in-seconds&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Time out delay before Jack gets to sleep. When Jack sleeps, its memory
  usage is reduced, but it is slower to wake up. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; means “do not sleep”.
  Default is 2 weeks.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.service.port=&amp;lt;port-number&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Server service TCP port number. Default is 8076. Needs to match the service
  port defined in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-settings&lt;/code&gt; on the client host (See Client section).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.admin.port=&amp;lt;port-number&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Server admin TCP port number. Default is 8077. Needs to match the service
  port defined in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-settings&lt;/code&gt; on the client host (See Client section).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.config.version=&amp;lt;version&amp;gt;&lt;/code&gt;: Internal, do not modify.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;server-logs&quot;&gt;Server logs&lt;/h3&gt;

&lt;p&gt;Server logs can be found by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin server-log&lt;/code&gt;. Default location is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/logs/&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;admin&quot;&gt;Admin&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin&lt;/code&gt; bash script allows to install and administrate the Jack server.
Here are some commands:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin help&lt;/code&gt;: Print help.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin install-server jack-launcher.jar jack-server.jar&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Install the Jack server.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin uninstall-server&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Uninstall the Jack server and all components. You should ensure that the
  Jack server is not running before uninstalling.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin list jack&lt;/code&gt;: List installed versions for Jack.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin update jack jack.jar&lt;/code&gt;: Install or update a Jack jar.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin start-server&lt;/code&gt;: Start the server.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin stop-server&lt;/code&gt;: Stop the server after the last compilation is complete.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin kill-server&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Kill the server process immediately, interrupting abruptly ongoing compilations.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin list-server&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;List Jack server processes.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin server-stat&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Print various info about the server and the host.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin server-log&lt;/code&gt;: Print log pattern.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ jack-admin dump-report&lt;/code&gt;: Produce a report file that can be used to file a bug.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;transitioning-from-server-11-eg-marshmallow-to-server-13-eg-n&quot;&gt;Transitioning from server 1.1 (e.g. Marshmallow) to server 1.3 (e.g. N)&lt;/h2&gt;

&lt;p&gt;The old Jack server used a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack&lt;/code&gt; configuration file. It has now replaced
by a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-settings&lt;/code&gt; and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt;.
If those new files do not exist, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin start-server&lt;/code&gt; and they will be
created. If you had custom settings in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack&lt;/code&gt;, here’s how to adapt those.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_SERVICE=XXXX&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Replace with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_SERVICE=XXXX&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-settings&lt;/code&gt; and
  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.service.port=XXXX&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_ADMIN=YYYY&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Replace with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_PORT_ADMIN=YYYY&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-settings&lt;/code&gt; and
  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.admin.port=YYYY&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_NB_COMPILE=N&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;Replace with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.max-service=N&lt;/code&gt; in
  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_TIMEOUT=ZZ&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;You can replace with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack.server.time-out=ZZ&lt;/code&gt;, but it is recommended to
  keep the default setting of “7200” (2 hours).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other settings in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack&lt;/code&gt; configuration file do not need to be copied.
You should still keep your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack&lt;/code&gt; configuration file for the old Jack
server because both server versions can run simultaneously.&lt;/p&gt;

&lt;h2 id=&quot;troubleshooting&quot;&gt;Troubleshooting&lt;/h2&gt;

&lt;p&gt;Below you’ll find some ways to solve some troubleshooting. If you don’t find a
solution, file a bug and attach the file produced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin dump-report&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;if-compilation-fails-on-no-jack-server-running&quot;&gt;If compilation fails on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;No Jack server running&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;See &lt;a href=&quot;#starting-the-server&quot;&gt;Starting the server&lt;/a&gt; above.&lt;/p&gt;

&lt;h3 id=&quot;if-your-computer-becomes-unresponsive-during-compilation&quot;&gt;If your computer becomes unresponsive during compilation:&lt;/h3&gt;

&lt;p&gt;You can improve the situation by reducing the number of jack simultaneous
compilations by editing your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt; and changing
jack.server.max-service to a lower value and then restarting the server.&lt;/p&gt;

&lt;h3 id=&quot;if-you-experience-jack-compilations-failing-on-out-of-memory-error&quot;&gt;If you experience Jack compilations failing on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Out of memory error.&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;You can improve the situation by reducing the number of jack simultaneous
compilations by editing your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.jack-server/config.properties&lt;/code&gt; and changing
jack.server.max-service to a lower value and then restarting the server. If this
is not enough, you may change the arguments used to start the server jvm and
force a greater maximum Java heap size (“-Xmx”):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Stop the server using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin stop-server&lt;/code&gt;, then:&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If you start the server manually:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  JACK_SERVER_VM_ARGUMENTS=&quot;-Xmx2g -Dfile.encoding=UTF-8 -XX:+TieredCompilation
  jack-admin start-server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If you use the jack server in the android tree then&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  export ANDROID_JACK_VM_ARGS=&quot;-Xmx2g -Dfile.encoding=UTF-8 -XX:+TieredCompilation&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and restart your build command.&lt;/p&gt;

&lt;h3 id=&quot;if-you-have-trouble-starting-the-server&quot;&gt;If you have trouble starting the server&lt;/h3&gt;

&lt;p&gt;This may mean that TCP ports are already in use on your computer. You can try
modifying the ports both in your client and server configurations. See the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Server info&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client info&lt;/code&gt; sections. If it doesn’t solve the problem,
please report and give us additional information by:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Attaching your compilation log.&lt;/li&gt;
  &lt;li&gt;Attaching the file produced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin dump-report&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;if-your-commands-fails-on&quot;&gt;If your commands fails on&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Failed to contact Jack server: Problem reading&amp;lt;your home&amp;gt;/.jack-server/client.pem&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This may mean that your server never managed to start, see
&lt;a href=&quot;#if-you-have-trouble-starting-the-server&quot;&gt;If you have trouble starting the server&lt;/a&gt;
above.&lt;/p&gt;

&lt;h3 id=&quot;if-your-compilation-gets-stuck-without-any-progress&quot;&gt;If your compilation gets stuck without any progress&lt;/h3&gt;

&lt;p&gt;Please report and give us additional information by attaching the file produced by
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin dump-report&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then restart the server by issuing commands:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jack-admin kill-server; jack-admin start-server&lt;/code&gt;&lt;/p&gt;

</description>
        <pubDate>Mon, 09 Jan 2017 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2017/01/09/jackserver-issues/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2017/01/09/jackserver-issues/</guid>
        
        <category>build</category>
        
        
        <category>Android</category>
        
      </item>
    
      <item>
        <title>Git 版本及版本范围表示法</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#指定-revision&quot; id=&quot;markdown-toc-指定-revision&quot;&gt;指定 revision&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#指定范围&quot; id=&quot;markdown-toc-指定范围&quot;&gt;指定范围&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;很多 Git 命令都使用 revision（修订版本）作为参数。根据不同的命令，有时候 revision 参
数代表一个特定的提交，有时候代表某一个提交可以追踪到的所有的父提交（比如 git log）。在
后面的这种情况中，一般也可以提供一个版本范围作为参数。另外，有的命令（例如 git show）
使用 revision 参数代表其他对象，如 blobs 或者 tree 对象。&lt;/p&gt;

&lt;h2 id=&quot;指定-revision&quot;&gt;指定 revision&lt;/h2&gt;

&lt;p&gt;一个 revision 参数 &lt;em&gt;&amp;lt;rev&amp;gt;&lt;/em&gt; 在通常情况下（但并不是所有情况下）代表一个提交对象（commit）。
使用一种称为 扩展 SHA-1（extended SHA-1）的语法。下面是各种各样表示 revision 的语法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;sha1&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dae86e1950b1277e545cee180551750029cfe735, dae86e&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;完整的 SHA-1 ID（40 字节的十六进制字符串），或者一个在仓库中可以唯一代表本次提交的
  前导子串都可以指代本次提交。在该例中，由于前导子串 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dae86e&lt;/code&gt; 在仓库中是唯一的，所以可以
  使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dae86e&lt;/code&gt; 来代表 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dae86e1950b1277e545cee180551750029cfe735&lt;/code&gt;。如果 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dae86e&lt;/code&gt;
  在仓库中不是唯一的，只需要再多加几个字符，使之在仓库中唯一即可。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;describeOutput&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1.7.4.2-679-g3bee7fb&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;git describe 命令的输出：包含一个 tag，后跟一个连字符，一个提交数目，一个连字符，
  一个字符 g，最后是一个简写的对象ID。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;refname&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master, heads/master, refs/heads/master&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;符号引用。例如：master 通常代表 refs/heads/master 引用指向的提交对象。如果你的仓
 库中同时存在 heads/master 和 tags/master，要想代表 master 分支，可以显式指定为：
 heads/master。如果一个引用名出现了二义性，那么 Git 按照下面的规则处理，使用找到的
 第一个匹配。&lt;/p&gt;

    &lt;ol&gt;
      &lt;li&gt;如果存在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$GIT_DIR/&amp;lt;refname&amp;gt;&lt;/code&gt;，则使用之&lt;/li&gt;
      &lt;li&gt;否则，查找 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refs/&amp;lt;refname&amp;gt;&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;否则，查找 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refs/tags/&amp;lt;refname&amp;gt;&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;否则，查找 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refs/heads/&amp;lt;refname&amp;gt;&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;否则，查找 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refs/remotes/&amp;lt;refname&amp;gt;&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;否则，查找 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refs/remotes&amp;lt;refname&amp;gt;/HEAD&lt;/code&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;：一个单独的 @ 代表 HEAD&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;refname&amp;gt;@{&amp;lt;date&amp;gt;}&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master@{yesterday}, HEAD@{5 minutes ago}&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;一个引用名，后跟一个 @ 符号，再后面是一个括在大括号里面的日期（例如：{yesterday}，
  {1 month 2 weeks 3 days 1 hour 1 second ago} 或者 {1979-02-26 18:30:00}）代表
  该引用在该段时间之前的值。这种用法只能用于引用上面，不能用于一个确切的 commit ID，
  并且该引用必须在 $GIT_DIR/logs/&lt;ref&gt; 中存在有 log 记录。注意，这种用法只会查看你
  的本地仓库在给定时间点的状态。如果你要查看在某一时间段内的状态，请使用 --since
  和 --until 参数。&lt;/ref&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;refname&amp;gt;@{&amp;lt;n&amp;gt;}&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master@{1}&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;一个引用名后面跟着一个 @ 后缀，然后是括在大括号中的一个数字，例如（{1}，{15}）。
  可以代表该引用之前的第 n 个值。例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master@{1}&lt;/code&gt; 代表 master 之前的值。而
  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master@{5}&lt;/code&gt; 则代表 master 之前第 5 次的值。这种用法只能用于引用上，并且该引用
  必须具有引用日志（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$GIT_DIR/logs/&amp;lt;refname&amp;gt;&lt;/code&gt;）。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@{&amp;lt;n&amp;gt;}&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@{1}&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;如果省略了引用部分，则代表当前分支。例如：如果你当前在分支 blabla 上，那么 @{1}
  就和 blabla@{1} 是一个意思。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@{-&amp;lt;n&amp;gt;}&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@{-1}&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;@{-&lt;n&gt;}， 这种表示法，代表之前第 n 次检出的分支或者提交。
qiandao&lt;/n&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;^&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HEAD^, v1.5.1^0&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;在一个 revision 后面加上一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt; 后缀可以用来指代 revision 的直接父提交。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&amp;lt;n&amp;gt;&lt;/code&gt;
  代表第 n 个父提交（例如， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;rev\&amp;gt;^&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;rev\&amp;gt;^1&lt;/code&gt; 是等同的）。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;rev\&amp;gt;^0&lt;/code&gt; 代表
  rev 自己。当 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;rev\&amp;gt;&lt;/code&gt; 是一个 tag 的名字时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;rev\&amp;gt;^0&lt;/code&gt; 表示该 tag 对象对应的提交
  对象。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;~&amp;lt;n&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master~3&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;在一个 revision 后面加上一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&amp;lt;n&amp;gt;&lt;/code&gt; 后缀代表 revision 的第 n 个祖先提交。主要用户当
  &amp;lt;rev&amp;gt; 有多个父提交的情况。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:/&amp;lt;text&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:/fix nasty bug&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;一个冒号，后跟一个斜杠，在后面是一串文本。这串文本会被当做正则表达式对待。整个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:/&amp;lt;text&amp;gt;&lt;/code&gt;
  代表提交信息匹配正则表达式的最近一次提交。正则表达式可匹配提交信息的任何部分。如果要匹配
  提交信息的开头，可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:/^foo&lt;/code&gt;。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;:&amp;lt;path&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HEAD:README, :README, master:./README&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&amp;lt;n&amp;gt;:&amp;lt;path&amp;gt;&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:0:README, :README&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;下面是一个图示，展示了各个概念：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
         A
                       
A =      = A^0
B = A^   = A^1     = A~1
C = A^2  = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;指定范围&quot;&gt;指定范围&lt;/h2&gt;

&lt;p&gt;那些需要遍历历史提交的命令（如 git log）会对一系列的提交进行操作，而不是仅仅针对某一次提交。
对于这些命令来说，如果只给它们提供一个 revision 参数，那么意思是指代沿着该 revision 的父提
交链可以追踪到的所有提交。&lt;/p&gt;

&lt;p&gt;如果要排除从某一次提交可以追踪到的提交，只需要在该提交的前面加上前缀 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;。例如：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^r1 r2&lt;/code&gt; 指代从 r2 可以追踪到的所有提交但是不包含从 r1 可以追踪到的所有提交。这种表示
方法非常常见，因此对于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^r1 r2&lt;/code&gt; 来说有一个简写表示法：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r1..r2&lt;/code&gt;（注意：&lt;strong&gt;r1 与 r2 之间只有
两个英文句点&lt;/strong&gt;）。可以省略一个 revision，省略的 revision 默认是 HEAD。&lt;/p&gt;

&lt;p&gt;相似的表示法：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r1...r2&lt;/code&gt; 可以叫做 r1 和 r2 之间的差集（数学），就是指代从 r1 可以追踪到的提
交或者是从 r2 可以追踪到的提交，但是不包含从 r1 和 r2 都可以追踪到的。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;&lt;/code&gt;：代表从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;&lt;/code&gt; 可以追踪到的提交（rev 的所有祖先）。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&amp;lt;rev&amp;gt;&lt;/code&gt;：不包含从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;&lt;/code&gt; 可以追踪到的提交。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev1&amp;gt;..&amp;lt;rev2&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;包含从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev2&amp;gt;&lt;/code&gt; 可以追踪到的所有提交，但是不包含从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev1&amp;gt;&lt;/code&gt; 可以追踪到的提交。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev1&amp;gt;...&amp;lt;rev2&amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;包含从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev1&amp;gt;&lt;/code&gt; 或者 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev2&amp;gt;&lt;/code&gt; 可以追踪到的提交，但是不包含从两者都可以追踪到的提
  交。这和上一种范围表示法容易混淆。上面的是两个点，而这个是三个点。举例说明，在上面
  的图示中，D..F 表示的提交范围只有F，而 D…F，表示的提交范围有 D 和 F。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;^@&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HEAD^@&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;代表 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;&lt;/code&gt; 所有的父提交，也就是从它的父提交可以追踪到的所有提交，但是不包含它
 自己。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;rev&amp;gt;^!&lt;/code&gt;, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HEAD^!&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;下面是一些例子：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;D                G H D
D F              G H I J D F
^G D             H D
^D B             E I J F B
B..C             C
B...C            G H D E B C
^D B C           E I J F B C
C                I J F C
C^@              I J F
C^!              C
F^! D            G H D F
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Wed, 28 Dec 2016 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2016/12/28/git-revisions/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2016/12/28/git-revisions/</guid>
        
        <category>Git</category>
        
        
        <category>VCS</category>
        
      </item>
    
      <item>
        <title>Windows 系统上的 alias</title>
        <description>
&lt;p&gt;我们知道，Linux 系统上的 bash 内建命令 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias&lt;/code&gt; 可以用来创建命令别名。命令别名给工作
带来了极大的方便。那么 Windows 系统上是否也有这种便利呢？ Windows 提供了一个内建的
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doskey&lt;/code&gt; 命令可以达到和 Linux 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias&lt;/code&gt; 一样的效果。&lt;/p&gt;

&lt;p&gt;这是 doskey 的语法：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;C:\Users\Administrator&amp;gt;doskey /?
编辑命令行，重新调用 Windows 命令，并创建宏。

DOSKEY [/REINSTALL] [/LISTSIZE=size] [/MACROS[:ALL | :exename]]
  [/HISTORY] [/INSERT | /OVERSTRIKE] [/EXENAME=exename] [/MACROFILE=filename]
  [macroname=[text]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果你安装了 Gow （GNU on Windows，是一个工具包，安装之后可以提供 100 多个 GNU 
Linux 上面常用的命令，是 cygwin 的一个轻量级的替代品），想让 ls 命令像在 linux 上面
一样，输出时自动以色彩区分文件类型（其实在 linux 上，默认色彩输出也是通过 alias 实现
的），你可以在 cmd 中执行以下指令：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;doskey ls=ls --color $*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样你在该 cmd 窗口中就可以得到默认的色彩输出了。但是如果重新打开一个 cmd 窗口，刚才的
设置就失效了。可以在你的 PATH 环境变量的位置之一创建一个 bat 文件,然后通过写入注册表
来让每次打开一个 cmd 窗口时都自动执行该 bat 文件。这跟 linux 上面 non-login shell 的
初始化文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; 的原理是一模一样的。&lt;/p&gt;

&lt;p&gt;例如，假设在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c:\Users\Administrator\bat&lt;/code&gt; 文件夹（该路径须要加入 PATH 环境变量）下
面有一个批处理文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias.bat&lt;/code&gt;，在里面加入了一些 alias：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@echo off
doskey ls=ls --color=always $*
doskey ll=ls -lh --color=always $*
doskey grep=grep --color=always $*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后打开注册表，定位到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HKEY_CURRENT_USER\Software\Microsoft\Command Processor\&lt;/code&gt;，
添加一个字符串，命名为 Autorun，将它的值设置为
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c:\Users\Administrator\bat\alias.bat&lt;/code&gt;，这样以后每次打开 cmd 窗口就可以立即使用
 这些命令别名了。&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;http://superuser.com/questions/55351/how-to-change-the-default-color-preference-for-ls-on-windows&quot;&gt;superuser&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Tue, 20 Dec 2016 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2016/12/20/alias-on-windows/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2016/12/20/alias-on-windows/</guid>
        
        
        <category>sys-admin</category>
        
      </item>
    
      <item>
        <title>AWK 语言</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot; id=&quot;markdown-toc-概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#编程结构&quot; id=&quot;markdown-toc-编程结构&quot;&gt;编程结构&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#数据类型转换和比较&quot; id=&quot;markdown-toc-数据类型转换和比较&quot;&gt;数据类型，转换和比较&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#正则表达式&quot; id=&quot;markdown-toc-正则表达式&quot;&gt;正则表达式&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#记录和域&quot; id=&quot;markdown-toc-记录和域&quot;&gt;记录和域&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#表达式和操作符&quot; id=&quot;markdown-toc-表达式和操作符&quot;&gt;表达式和操作符&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#数组&quot; id=&quot;markdown-toc-数组&quot;&gt;数组&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#内建变量&quot; id=&quot;markdown-toc-内建变量&quot;&gt;内建变量&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#内建函数&quot; id=&quot;markdown-toc-内建函数&quot;&gt;内建函数&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#字符串函数&quot; id=&quot;markdown-toc-字符串函数&quot;&gt;字符串函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#数学函数&quot; id=&quot;markdown-toc-数学函数&quot;&gt;数学函数&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#输入和输出&quot; id=&quot;markdown-toc-输入和输出&quot;&gt;输入和输出&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#用户定义函数&quot; id=&quot;markdown-toc-用户定义函数&quot;&gt;用户定义函数&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#分隔字符串记录和文件&quot; id=&quot;markdown-toc-分隔字符串记录和文件&quot;&gt;分隔字符串，记录和文件&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#多行记录&quot; id=&quot;markdown-toc-多行记录&quot;&gt;多行记录&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#程序执行流程&quot; id=&quot;markdown-toc-程序执行流程&quot;&gt;程序执行流程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#例子&quot; id=&quot;markdown-toc-例子&quot;&gt;例子&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;awk 是处理文本的利器，使用 awk 可以完成很多复杂的文本处理工作。awk 工具提供了编程语言
中具有的程序流程控制，字符串处理，随机数生成，数学运算等功能。因此 awk 被称之为一门
语言，可见 awk 工具的强大。’awk’ 这个名字是取它最初的三位开发者名字的首字母而来。awk
可以在 Linux，OSx，Windows 上使用。&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;awk 程序由一系列的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pattern {action}&lt;/code&gt; 和函数定义组成。较短的 awk 程序直接在命令行中
输入，通常程序文本被括在单引号内，以免被 shell 特殊解释；如果程序较长，可以通过 -f 选
项在文件中读取。要处理的文本名直接在命令行中读取，如果在命令行中没有提供文件名，则处理
标准输入。输入被根据 RS 变量划分为记录（records）。RS 变量默认等于 “\n”，因此，记录和
行是同义词。每一个记录都要和每一个 pattern 尝试进行匹配。如果匹配，pattern 后面的
{action} 就被执行。&lt;/p&gt;

&lt;h2 id=&quot;编程结构&quot;&gt;编程结构&lt;/h2&gt;

&lt;p&gt;AWK 程序可以是一系列的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pattern {action}&lt;/code&gt; 对，和用户函数定义。 awk 语言是面向行的，
每一个 action 必须开始于 pattern 所在的那一行。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pattern {action}&lt;/code&gt; 对可以跨越多行。&lt;/p&gt;

&lt;p&gt;pattern 可以是：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;BEGIN
END
expression
expression , expression
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;pattern 和 {action} 两个中可以忽略一个。如果没有提供 {action}，默认为 { print }。如
果忽略了 pattern，就认为是匹配的。 BEGIN 和 END 这两个 pattern 必须提供一个 action。&lt;/p&gt;

&lt;p&gt;语句由换行符，分号，或者换行符和分号分隔。语句块，比如说 actions 或者循环体需要使用大
括号括起来，就像在 C 语言中一样。语句块中的最后一条语句后面不需要跟分隔符。空白行没有
意义；空语句由分号结尾。较长的语句可以通过反斜线使之书写在多行。如果一条语句在逗号，左
大括号，&amp;amp;&amp;amp;，||，do，else，或者 if，while，for 语句，函数定义的右小括号之后换行，那么
可以不使用反斜线。注释以 # 开始一直持续到行尾，但是不包含换行符。&lt;/p&gt;

&lt;p&gt;以下的语句控制语句块中的程序流。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if ( expr ) statement
if ( expr ) statement else statement
while ( expr ) statement
do statement while ( expr )
for ( opt_expr ; opt_expr ; opt_expr ) statement
for ( var in array ) statement
continue
break
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;数据类型转换和比较&quot;&gt;数据类型，转换和比较&lt;/h2&gt;

&lt;p&gt;有两种基本数据类型：数字和字符串。数值常量可以是整数（像 -2），小数（像 1.08），或者使用
科学计数法表示，比如 -1.1e4 或者 .28E-3。所有的数值表示和计算在内部都使用浮点数。举个
例子，表达式 0.2e2 == 20 为 true，true 是由 1.0 表示的。&lt;/p&gt;

&lt;p&gt;字符串常量由双引号括起来。例如：”This is a string with a newline at the end.\n”&lt;/p&gt;

&lt;p&gt;通过对换行符进行转义，字符串可以跨越多行。awk 接受以下转义序列：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;\\        \
\&quot;        &quot;
\a        警报，ASCII：7
\b        退格符，ASCII：8
\t        制表符，ASCII：9
\n        换行符，ASCII：10
\v        垂直制表符，ASCII：11
\f        进纸符号，ASCII：12
\r        回车，ASCII：13
\ddd      一到三个八进制数字，代表 ASCII 字符：ddd
\xhh      一个或者两个十六进制数字，代表 ASCII 字符：hh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;实际上有三种数据类型。第三种是数字和字符串类型，该类型的值同时具有一个数字值和一个字符串值。
用户定义变量只有当第一次被引用的时候才会被创建，会被初始化为 “null”。如果是数字和字符串
类型，那么它具有数字值 0 ，具有字符串值 ““，&lt;/p&gt;

&lt;p&gt;表达式的类型是由它所处的上下文决定的.如果需要，就会发生自动类型转换。举个例子：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;y = x + 2; z = x &quot;hello&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;y 将会是数字类型。如果 x 不是数字，那么 x 中读取到的值将会被转化为数字，然后加上 2 ，
赋给 y。z 将会是字符串类型，x 如果不是字符串类型，将会被转化为字符串类型然后和 “hello”
字符串连接起来赋给 z。&lt;strong&gt;请注意&lt;/strong&gt;：x 的类型以及值并没有发生变化。 字符串表达式转化为数
字按照 c 库函数 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atof&lt;/code&gt; 的规则进行。&lt;/p&gt;

&lt;p&gt;在一个需要布尔值的上下文上，例如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if ( expr ) statement&lt;/code&gt;，如果 expr 是一个字符串表达
式，只有在 expr 为空串的时候，才为 false，否则为 true；如果 expr 是数字值，只有当
expr 为 0 时，转换为 false，否则转换为 true。&lt;/p&gt;

&lt;h2 id=&quot;正则表达式&quot;&gt;正则表达式&lt;/h2&gt;

&lt;p&gt;在 AWK 语言中，记录，域和字符串经常被测试是否匹配一个正则表达式。正则表达式被两个斜线
‘/’ 括起来。对于下面的 AWK 表达式&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;expr ~ /r/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果 expr 匹配 r，那么其值为 1，如果不匹配，其值为 0。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; 为匹配操作符， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!~&lt;/code&gt; 为不匹
配操作符。作为 pattern-action 对，&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/r/ { action }   and   $0 ~ /r/ { action }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;是等价的，对于每一个匹配 r 的输入记录，都将执行 action。实际上。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/r/&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0 ~ /r/&lt;/code&gt;
是等价的，除非 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/r/&lt;/code&gt; 位于匹配操作符的右边，或者需要给内建函数传递一个正则表达式参数。其
实这样也很好理解，一个函数需要一个正则表达式参数，你总不能给它传递一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$0 ~ /r/&lt;/code&gt; 吧。&lt;/p&gt;

&lt;p&gt;AWK 使用和 egrep 一致的扩展正则表达式语法。在正则表达式中具有特殊意义的元字符有以下几个：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;^ $ . [ ] | ( ) * + ?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Regular expressions are built up from characters as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;：匹配非元字符 c&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\c&lt;/code&gt;：如果 \c 是一个转义字符，就按照转义字符解释。否则，解释为字符 c&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;：匹配任何字符（包括换行符）&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;：匹配字符串的开头&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;：匹配字符串的结尾&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[c1c2c3...]&lt;/code&gt;：匹配方括号中的任意一个字符&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[^c1c2c3...]&lt;/code&gt;：匹配不在方括号中的任意一个字符&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regular expressions are built up from other regular expressions as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r1r2&lt;/code&gt;：当 r2 跟在 r1 后面的时候才匹配&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r1 | r2&lt;/code&gt;：r1 和 r2 只要存在一个就匹配&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r*&lt;/code&gt;：r 重复任意次&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r+&lt;/code&gt;：r 重复一次或多次&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r?&lt;/code&gt;：r 重复0次或一次&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(r)&lt;/code&gt;：匹配 r，提供分组的功能&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;举个例子，下面的两个正则表达式分别匹配 AWK 的标识符和 数字常量。注意 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; 如果用做小数点
必须被转义，因为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; 是一个元字符。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/^[_a-zA-Z][_a-zA-Z0-9]*$/
/^[-+]?([0-9]+\.?|\.[0-9])[0-9]*([eE][-+]?[0-9]+)?$/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;操作符 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!~&lt;/code&gt; 的右边可以使用任何正则表达式，正则表达式也可以转递给需要正则表达式
作为参数的内建函数。如果需要，正则表达式会被转换为字符串，但是任然被解释为正则表达式。例如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;BEGIN { identifier = &quot;[_a-zA-Z][_a-zA-Z0-9]*&quot; }
$0 ~ &quot;^&quot; identifier
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以上语句会打印出任何以 AWK 标识符开头的行。&lt;/p&gt;

&lt;p&gt;mawk 会识别空正则表达式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;//&lt;/code&gt;，它会匹配空字符串。因此，该正则表达式会在一个字符串的开头，
任意字符中间，以及结尾匹配。例如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo  abc | mawk { gsub(//, &quot;X&quot;) ; print }
XaXbXcX
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;记录和域&quot;&gt;记录和域&lt;/h2&gt;

&lt;p&gt;记录每次读入一个，存储在变量 $0 中。记录会被划分为域，并且分别赋值给变量 $1，$2，…
$NF。内建变量 NF 等于划分的域的数目，NR 和 FNR 加 1。大于 $NF 的域被设置为 ““。&lt;/p&gt;

&lt;p&gt;给 $0 赋值，会导致域和 NF 被重新计算。给 NF 或者一个域赋值或导致 S0 被重新构建。如果
给大于 NF 的域赋值，会增加 NF，并且导致 S0 被重新构建。&lt;/p&gt;

&lt;p&gt;域中存储的数据类型为字符串，除非整个域都是数字格式，此时该域的数据类型为 “数字和字符串”
例如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo 24 24E |
awk '{ print($1&amp;gt;100, $1&amp;gt;&quot;100&quot;, $2&amp;gt;100, $2&amp;gt;&quot;100&quot;) }'
0 1 1 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在上面的例子中，$0 和 $2 是字符串，$1 是数字和字符串类型。print 语句中，第一个比较是
数值比较，第二个是字符串比较，第三个是字符串比较（100 会被转化为 “100”），最后一个也是
字符串比较。&lt;/p&gt;

&lt;p&gt;域的索引并不是常量。任何 awk 语言中的合法表达式都可以放在 $ 后面来引用一个域。表达式的
值指定了域的索引。如果表达式的值是字符串，则会被转化为数字。例如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;awk '{for(i=1;i&amp;lt;=NF;i++)
print $i }'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面的代码将文件中每一行的每一个域打印在一行上。&lt;/p&gt;

&lt;h2 id=&quot;表达式和操作符&quot;&gt;表达式和操作符&lt;/h2&gt;

&lt;p&gt;AWK 表达式的语法和 c 语言很相似。主要的表达式有：数字常量，字符串常量，变量，域，数组和
函数调用。变量，数组或者函数的名字，称之为标识符。标识符由字母，数字，下划线组成，但是不能
以数字打头。使用变量不用声明；变量在它们被首次引用的时候自动创建，并被初始化为 null。&lt;/p&gt;

&lt;p&gt;表达式由以下操作符组成，操作符按照优先级地址排列&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;赋值运算符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=  +=  -=  *=  /=  %=  ^=&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;条件运算符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?  :&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;逻辑或：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;逻辑与：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;数组关系运算符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;in&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;匹配：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~   !~&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;关系操作符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;  &amp;gt;   &amp;lt;=  &amp;gt;=  ==  !=&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;连接操作符：(no explicit operator)&lt;/li&gt;
  &lt;li&gt;加法：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+  -&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;乘法：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*  /  %&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;一元操作符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+  -&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;逻辑非：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;乘方：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;前后自增和自减：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++ --&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;域引用操作符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;赋值运算符，条件运算符，乘方运算符是从右向左结合。其它的运算符从左到右结合。所有的表达式
都可以使用括号括起来。&lt;/p&gt;

&lt;h2 id=&quot;数组&quot;&gt;数组&lt;/h2&gt;

&lt;p&gt;AWK 提供有一维数组。数组元素通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;array[expr]&lt;/code&gt; 表达。 expr 在内部会被转换成字符串类型。
例如，A[1] 和 A[“1”] 引用同一个数字元素，并且真正的索引是 “1”。由字符串索引的数组被称为
关联数组。数组最初是空的；数组元素当被访问的时候才会存在。如果数组 array 中存在元素 expr，
也即元素 array[expr] 存在，那么表达式 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expr in array&lt;/code&gt; 的值为 1，如果不存在，那么这个表
达式的值为 0。&lt;/p&gt;

&lt;p&gt;有一种形式的 for 语句可以遍历数组的每一个索引&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for ( var in array ) statement
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以上语句会将 var 设置为 array 的每一个索引，然后执行语句 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;statement&lt;/code&gt;。不过要注意，数组
索引被遍历的顺序是未定义的。&lt;/p&gt;

&lt;p&gt;语句 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delete array[expr]&lt;/code&gt;，会删除元素 array[expr]，造成元素 array[expr] 不复存在。
mawk 支持一个扩展语法：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delete array&lt;/code&gt;，会删除数组 array 的所有的元素。&lt;/p&gt;

&lt;h2 id=&quot;内建变量&quot;&gt;内建变量&lt;/h2&gt;

&lt;p&gt;下面的这些变量是内建于 AWK 的，并且在程序执行之前就会被初始化。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ARGC&lt;/code&gt;：命令行参数的个数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ARGV&lt;/code&gt;：由命令行参数组成的数组，索引从 0 到 ARGC-1&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CONVFMT&lt;/code&gt;：内部将数组转化为字符串的格式字符，默认是 “%.6g”&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENVIRON&lt;/code&gt;：由环境变量组成的关联数组。环境变量 var=value，在该数组中表示为：
  EVVIRON[var] = value&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FILENAME&lt;/code&gt;：当前正在处理的输入文件的名字&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FNR&lt;/code&gt;：当前输入文件的当前记录（行）编号，该变量跟 NR 变量的区别是，每当读取一个新的文件
时，该变量都将被重置。而无论读取多少个输入文件，NR 变量都不会被重置。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FS&lt;/code&gt;：域分隔符，默认为：空格，水平制表符，换行符。可被重新指定为一个正则表达式。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NF&lt;/code&gt;：一条记录中域的个数，对域的引用如果大于该数字，得到空字符串（”“）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NR&lt;/code&gt;：当前的记录索引，从 1 开始。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OFMT&lt;/code&gt;：打印数字的格式，默认为 “%.6g”&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OFS&lt;/code&gt;：输出域分隔符，默认为 “ “&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ORS&lt;/code&gt;：输出记录分隔符，默认为 “\n”&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RLENGTH&lt;/code&gt;：length set by the last call to the built-in function, match().&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RS&lt;/code&gt;：记录分隔符，默认为 “\n”，如果你想要改变该变量，一般在与 BEGIN 配对的 {action}
  中更改，例如：&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  awk 'BEGIN { RS = &quot;u&quot;}; { print $0 }' mail-list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;记录分隔符也可以是一个正则表达式。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RSTART&lt;/code&gt;：index set by the last call to match().&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUBSEP&lt;/code&gt;：used to build multiple array subscripts, initially = “\034”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;内建函数&quot;&gt;内建函数&lt;/h2&gt;

&lt;h3 id=&quot;字符串函数&quot;&gt;字符串函数&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gsub(r,s,t)  gsub(r,s)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;全局替换，变量 t 中任何一处匹配正则表达式 r 的地方都将被替换为字符换 s。返回替换的
  次数。如果省略了 t，使用 $0。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index(s,t)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;如果 t 是 s 的子串，则返回 t 在 s 中开始的位置，否则，返回 0。s 中第一个字符的位置
  是 1。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length(s)&lt;/code&gt;：返回字符串 s 的长度&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;match(s,r)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;返回字符串 s 中正则表达式 r 的第一次最长匹配的索引。如果不匹配，返回 0。作为一个副
  作用，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RSTART&lt;/code&gt; 被设置为该函数的返回值。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RLENGTH&lt;/code&gt; 被设置为匹配的长度，如果没有匹配，
  设置为 -1。如果匹配了空串，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RLENGTH&lt;/code&gt; 设置为 0。如果匹配在字符串的最前面，返回 1。
  如果匹配在最后面，返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length(s) + 1&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;split(s,A,r)  split(s,A)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;字符串 s 按照正则表达式 r 被切割为多个子字符串，将这些子字符串放在数组 A 中。返回
  子字符串的数目。如果省略了 r，则使用变量 FS。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sprintf(format,expr-list)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;根据格式化字符串 format，由参数 expr-list 构造一个新的字符串，并返回该字符串。该函
  数类似于 c 语言中标准输入输出库中的 printf 函数。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sub(r,s,t)  sub(r,s)&lt;/code&gt;：单次替换，跟 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gsub()&lt;/code&gt; 相同，只不过只替换一次。&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;substr(s,i,n)  substr(s,i)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;返回 s 的一个子串，该子串开始于索引 i，长度为 n。如果省略了 n，s 中从索引 i 开始的
  余下部分都被返回。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tolower(s)&lt;/code&gt;：复制 s 一份，并将 s 中的所有大写字母改为小写，返回 s&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toupper(s)&lt;/code&gt;：复制 s 一份，并将 s 中的所有小写字母改为大写，返回 s&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;数学函数&quot;&gt;数学函数&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atan2(y,x)&lt;/code&gt;：反正切函数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cos(x)&lt;/code&gt;：余弦函数，x 是弧度&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exp(x)&lt;/code&gt;：乘方函数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int(x)&lt;/code&gt;：去掉 x 的小数部分&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log(x)&lt;/code&gt;：以 e 为底的对数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rand()&lt;/code&gt;：返回 0 和 1 之间的一个随机数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sin(x)&lt;/code&gt;：正弦函数，x 是弧度&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sqrt(x)&lt;/code&gt;：返回 x 的平方根&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srand(expr)  srand()&lt;/code&gt;：
  为随机数生成器做种，如果省略了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expr&lt;/code&gt;，则使用当前时钟，并且返回之前的种子。mawk 会
  在启动的时候自动使用时钟为随机数生成器做种，因此没有使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srand()&lt;/code&gt; 函数的必要。
  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srand()&lt;/code&gt; 在需要重复随机数序列的时候很有用。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;输入和输出&quot;&gt;输入和输出&lt;/h2&gt;

&lt;p&gt;有两个输出语句， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf&lt;/code&gt;。它们的用法如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt;：打印 $0 ORS 到标准输出&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print expr1, expr2, ..., exprn&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;打印 expr1 OFS expr2 OFS … exprn ORS 到标准输出。数字表达式会根据 OFMT 转换为
  字符串&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf format, expr-list&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;等同于 c 库函数 printf，根据 format 格式化字符串，将 expr-list 打印到标准输出。
  接受所有的 ANSI C 格式字符：%c, %d, %e, %E, %f, %g, %G, %i, %o, %s, %u, %x, 
  %X 和 %%，以及转换修饰符 h 和 l。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;printf 函数和 print 函数的参数列表可以括在小括号中。&lt;/p&gt;

&lt;h2 id=&quot;用户定义函数&quot;&gt;用户定义函数&lt;/h2&gt;

&lt;p&gt;用户定义函数的语法是：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function name( args ) { statements }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;函数体中可以包含一个返回语句，但是返回语句并不是必须的。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;return opt_expr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;函数调用可以是嵌套的或者是递归的。函数参数如果是数组，按引用传递；如果是其他表达式则按值
传递。额外的函数参数是局部变量，并且被初始化为 null。例如，下面的函数 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;csplit(s, A)&lt;/code&gt; 
将 s 中的每一个字符放到数组 A 中，返回s 的长度：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function csplit(s, A,     n, i)
{
    n = length(s)
    for(i = 1; i &amp;lt;= n; i++) A[i] = substr(s, i, 1)
    return n
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;函数定义中，在函数参数和其他的局部变量之间放入额外的空格是一种约定俗成的用法。函数可以在
定义之前使用。注意，函数名和后面的小括号必须是挨着的，以免造成其它的问题。&lt;/p&gt;

&lt;h2 id=&quot;分隔字符串记录和文件&quot;&gt;分隔字符串，记录和文件&lt;/h2&gt;

&lt;p&gt;AWK 使用和 split() 函数相同的算法来将字符串切分为数组。&lt;/p&gt;

&lt;h2 id=&quot;多行记录&quot;&gt;多行记录&lt;/h2&gt;

&lt;p&gt;由于 mawk 将 RS 解释为一个正则表达式，因此很容易存在多行记录。如果你设置 RS = “\n\n+”，
记录分隔符就是一个或者多个空行。同时，如果 FS = “ “（默认情况下），换行符就是被当做空格，
从而成为域分隔符。&lt;/p&gt;

&lt;h2 id=&quot;程序执行流程&quot;&gt;程序执行流程&lt;/h2&gt;

&lt;p&gt;该节描述 AWK 程序的执行流程。首先，内置变量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ARGC&lt;/code&gt; 被设置为传递给程序的命令行参数的个数。
其中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ARGC[0]&lt;/code&gt; 是 AWK 解释器的名字，ARGV[1] 到 ARGV[ARGC-1] 保存着余下的命令行参数，
不过不包括选项和程序源代码。例如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mawk -f prog v=1 A t=hello B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;经过解析之后：ARGC = 5；ARGV[0] = “mawk”；ARGV[1] = “v=1”；ARGV[2] = “A”；
ARGV[3] = “t=hello”；ARGV[4] = “B”。&lt;/p&gt;

&lt;p&gt;接下来，每一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEGIN&lt;/code&gt; 代码块儿被按照顺序执行。如果程序只包含了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEGIN&lt;/code&gt; 代码块儿，则执行 
完&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEGIN&lt;/code&gt; 块儿之后，程序执行终止。否则，会打开一个输入流，继续往下执行。如果 ARGC 等于 1，
输入流被设置为标准输入，否则会检查命令行参数 ARGV[1] …  ARGV[ARGC-1] 去寻找文件参数。&lt;/p&gt;

&lt;p&gt;命令行参数分为 3 种：文件参数，赋值参数和空参数 ““。赋值参数具有这种格式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var=string&lt;/code&gt;。
如果 ARGV[i] 是一个文件参数，且该文件是空，则被跳过；如果是一个赋值参数，则赋值生效。&lt;/p&gt;

&lt;p&gt;一旦打开了一个输入流，每一个输入的记录都会和每一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pattern&lt;/code&gt; 进行匹配，如果匹配成功，相应
的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{action}&lt;/code&gt; 就被执行。如果你的 expression 值为真，那么该 pattern 就被匹配。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEGIN&lt;/code&gt;
pattern 在任何输入被读入之前就匹配。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; 在所有输入都被读入之后才会被匹配。对于 pattern
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expr1,expr2&lt;/code&gt;，matches every record between the match of  expr1  and  the  match&lt;br /&gt;
expr2 inclusively.&lt;/p&gt;

&lt;p&gt;如果一个文件处理完，则会检查命令行看是否还有其它的文件，如果有，则继续读取其它文件中的记录。
如果没有，则 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; pattern 会被匹配，相应的语句被执行。&lt;/p&gt;

&lt;p&gt;在上面的例子中，赋值 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v=1&lt;/code&gt; 在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEGIN&lt;/code&gt; 序列被执行完之后发生，v 中的值类型为字符串和数字。
然后会从文件 A 中读取输入。文件 A 读取完之后，会设置 t=hello，然后读取 B 中的内容。当 B
中的内容处理完毕之后， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; 序列被执行。&lt;/p&gt;

&lt;p&gt;程序流可以被下面的语句改变&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;next
exit  opt_expr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next&lt;/code&gt; 会导致结束处理当前的记录，并立即读取下一条记录。类似于 c 语言中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continue&lt;/code&gt; 
语句。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit&lt;/code&gt; 语句会导致立即执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; 序列。如果没有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; 序列，则程序立即终止。 可选
的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;opt_expr&lt;/code&gt; 会设置程序的退出码。&lt;/p&gt;

&lt;h2 id=&quot;例子&quot;&gt;例子&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;模拟 cat 命令&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; print &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;模拟 wc 命令&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; chars +&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; length&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; + 1
   words +&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; NF &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
   END &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;print NR, words, chars&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
        <pubDate>Mon, 19 Dec 2016 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2016/12/19/awk-language/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2016/12/19/awk-language/</guid>
        
        <category>awk</category>
        
        
        <category>Linux</category>
        
      </item>
    
      <item>
        <title>使用 mediainfo 获取音视频文件头信息</title>
        <description>
&lt;p&gt;最近一直使用喜马拉雅FM听《易中天品三国》，易中天讲的真是精彩。今天洗脚的时候想用电脑听，就把下
载到手机上的文件复制到计算机上了。可是发现下载下来的文件名全都是 40 Byte 的 SHA1 字串。无法
找到确定的某一集。&lt;/p&gt;

&lt;p&gt;就想写个小工具读取一下音频文件的头信息，将文件重命名一下。经过一番搜索发现了两个专门用于读取音
视频文件头信息的 C++ 库：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://taglib.github.io&quot;&gt;taglib&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://mediaarea.net/ZH-CN/MediaInfo&quot;&gt;mediainfo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这两个库都提供有 c 语言的接口。本来准备写个简单的 C 程序的。后来发现 mediainfo 这个工具提供
有一个 GUI 版本（mediainfo-gui）和一个 CLI 版本（mediainfo）的工具，还有开发
库（libmediainfo-dev）和文档（libmediainfo-doc）。在 Ubuntu 的官方仓库中
可以直接安装：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt install mediainfo-gui mediainfo 
sudo apt install libmediainfo-dev libmediainfo-doc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;GUI 版本的工具可以直接打开某一个音频文件或者一个包含音频文件的文件夹，就可以显示文件的头信息：
专辑，作者，标题，采样率等等各种你需要的信息。&lt;/p&gt;

&lt;p&gt;命令行的工具接受一些选项，可以获得你需要的信息。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mediainfo [-Options...] FileName1 [Filename2...]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;mediainfo 0af013591ebd7d3fc144250528a20f2c  
General  
Complete name                            : 0af013591ebd7d3fc144250528a20f2c  
Format                                   : MPEG-4  
Format profile                           : Apple audio with iTunes info  
Codec ID                                 : M4A  &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;isom/iso2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  
File size                                : 6.22 MiB  
Duration                                 : 35mn 52s  
Overall bit rate mode                    : Constant  
Overall bit rate                         : 24.3 Kbps  
Album                                    : 百家讲坛_《易中天品三国》  
Track name                               : 0af013591ebd7d3fc144250528a20f2c  
Performer                                : 易中天  
Recorded &lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;                            : 2006  
Encoded &lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;                             : UTC 1904-01-01 00:00:00  
Tagged &lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;                              : UTC 1904-01-01 00:00:00  
Writing application                      : Lavf56.18.101  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;既然有了命令行工具，那么通过 bash 脚本可以快速的解决问题，就在终端中敲了下面的一段命令：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;fname &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;do
    &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;mediainfo &lt;span class=&quot;nv&quot;&gt;$fname&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Track name&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;:&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f2&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $1}'&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;rename &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$fname&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; to &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$title&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$fname&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$title&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;解决了问题。&lt;/p&gt;

&lt;p&gt;喜马拉雅FM打的推广口号：“让做家务变得津津有味“，我发现这个真的是一点儿也不夸张，一边听广播，我
这么懒的人，洗衣服也不觉得枯燥了…&lt;/p&gt;
</description>
        <pubDate>Sat, 17 Dec 2016 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2016/12/17/mediainfo-tool/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2016/12/17/mediainfo-tool/</guid>
        
        <category>mediainfo</category>
        
        
        <category>tools</category>
        
      </item>
    
      <item>
        <title>在 Linux 上使用 Group 和 ACL 进行团队工作</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#添加用户组&quot; id=&quot;markdown-toc-添加用户组&quot;&gt;添加用户组&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#添加用户&quot; id=&quot;markdown-toc-添加用户&quot;&gt;添加用户&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#建立共享文件夹&quot; id=&quot;markdown-toc-建立共享文件夹&quot;&gt;建立共享文件夹&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#设置-acl权限&quot; id=&quot;markdown-toc-设置-acl权限&quot;&gt;设置 ACL权限&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#添加到-sudo-用户组&quot; id=&quot;markdown-toc-添加到-sudo-用户组&quot;&gt;添加到 sudo 用户组&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目前我们的工作站上大都是多人共享同一账号。大家共享一套全局的环境变量，在使用 git 的时
候，如果一个人配置了全局的个人信息，提交代码时容易造成混淆。很不方便，有时候还会导致一些
奇怪的问题。可以在工作站上为每人创建一个账号，个人完全可以按照自己的喜好去配置环境。&lt;/p&gt;

&lt;h2 id=&quot;添加用户组&quot;&gt;添加用户组&lt;/h2&gt;

&lt;p&gt;多个账号之间的协作通过用户组实现，先创建一个所有人共享的用户组：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo addgroup &amp;lt;group-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;添加用户&quot;&gt;添加用户&lt;/h2&gt;

&lt;p&gt;然后创建多个需要的用户：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo adduser --ingroup &amp;lt;group-name&amp;gt; &amp;lt;user-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;通过以上两个步骤，系统上已经有了多个属于同一个用户组的账号。每个账号都有自己的家目录：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/&amp;lt;user-name&amp;gt;&lt;/code&gt;。自己家目录中的文件，同一用户组中的用户只具有读权限。这也正是我们
想要的。&lt;/p&gt;

&lt;p&gt;在不更改 umask 的情况下，个人默认的创建文件夹权限为 755, 创建文件权限为 644。每一个用
户主目录下面的文件，别人是没有写权限的。这样一来，如果我们想要和他人共享一份代码的时候
（比如协作打patch），就需要使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod -R&lt;/code&gt; 去手动更改权限了。一是这个操作很慢，二是大
部分情况下，当你需要使用 chmod -R 命令的时候，都说明了你的权限配置不太合适。我们可以通
过 ACL（访问控制列表）的设置来更改大家默认的文件夹以及文件的创建权限。&lt;/p&gt;

&lt;h2 id=&quot;建立共享文件夹&quot;&gt;建立共享文件夹&lt;/h2&gt;

&lt;p&gt;创建一个大家共享的文件夹，该文件夹最好位于一个大分区之上。比如我的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home&lt;/code&gt; 挂载点上挂了
一个 2T 的硬盘，我就在 /home 下创建了一个共享文件夹。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo mkdir /home/share
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;经过上面的命令 /home/share 变成了 root 用户的，把它改为你自己的&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo chown &amp;lt;your-user-name&amp;gt;:&amp;lt;your-group&amp;gt; /home/share
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;设置-acl权限&quot;&gt;设置 ACL权限&lt;/h2&gt;

&lt;p&gt;下面改变 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/share&lt;/code&gt; 目录下的默认创建权限&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chmod g+s /home/share
setfacl -d -m g::rwx /home/share
setfacl -d -m o::rw /home/share
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;通过上面的设置，同一用户组的用户在 /home/share 目录下创建的文件的默认权限为 664，创建
的文件夹的默认权限为 775。大家就可以进行协作了。由于大家是使用不同的账号登录的，有各自
的环境变量，相互之间是不会影响的。&lt;/p&gt;

&lt;h2 id=&quot;添加到-sudo-用户组&quot;&gt;添加到 sudo 用户组&lt;/h2&gt;

&lt;p&gt;默认情况下，ubuntu 系统只赋予了安装系统的用户 sudo 权限。使用 adduser 命令添加的用户
是不能执行 sudo 命令的。不过因为安装的软件是系统级的，所有的用户都可以使用管理员安装的
软件。因此这一步是可选的。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo adduser &amp;lt;user-name&amp;gt; sudo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Mon, 12 Dec 2016 00:00:00 +0000</pubDate>
        <link>http://quanzhuo.github.io/2016/12/12/group-and-acl/</link>
        <guid isPermaLink="true">http://quanzhuo.github.io/2016/12/12/group-and-acl/</guid>
        
        
        <category>sys-admin</category>
        
      </item>
    
  </channel>
</rss>
