0%

我们在后台开发的时候,经常会碰到数据导出。我们一般都是试用xls格式进行导出。但是有个问题,就是使用类库对内存的占用特别的大。结果稍微复杂一些的数据就会导致内存溢出。

那么应该如何解决呢?

其实换个思路。也就是说,如果格式不复杂的话,我们可以导出csv格式的数据。

PHP已经内置了对这个格式的处理。其中使用的函数就是:

其实使用方法很简单我就不用写范例了。就把php的示例搬过来吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);
?>

输出的内容如下:

1
2
3
4
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""

上面这种是写入文件的,评论第一的说的是如何直接进行输出。他的原理是写入到php的输出中。

1
2
3
4
5
<?php
$out = fopen('php://output', 'w');
fputcsv($out, array('this','is some', 'csv "stuff", you know.'));
fclose($out);
?>

这么一看的话,导出xls是不是就简单了很多呢。

使用ubuntu时间长了经常会使用sodo,发现这样非常麻烦,那如何让sudo免密码呢?

其实方法很简单。首先执行以下命令:

1
sudo visudo

我这边是使用nano打开了一个配置文件,然后找到下面这行:

1
%sudo   ALL=(ALL:ALL) ALL

在最后ALL的前面加上NOPASSWD:就可以了。i加入是这样的:

1
%sudo   ALL=(ALL:ALL) NOPASSWD:ALL

然后同时按住Ctrl+X,退出保存就好了。

其实在我们学习使用jQuery的过程中,我们经常是这样使用的:

1
2
$("html").css();
$.get("/abc", function(){})

发现特别好用,而我在前段时间处理webview的时候,也多次使用到js,并且对于使用原生JS感到稍有不便。所以想到将其封装成类库,然后供以后进行重复使用或者优化。

那么如何去实现如何jQuery的方法呢。

其实代码很简单,我也是门外汉,记录下来也是为我自己做一个学习的方式,希望如果有更好的方式可以跟我说。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function gc() {
return new gc.fn.abc();
}

gc.fn = gc.prototype = {
abc: function () {
this.__proto__ = gc.fn;
return this;
},
de: function () {
console.log('def');
}
}
gc.aaa = function () {
console.log('aaa');
}

gc();
gc().de();
gc.aaa();

URL提供了一种对任意的一种互联网资源定位的手段。但是这些资源是可以通过不同的方案来(比如,HTTP,FTP,SMTP)进行访问的,因此URL的语法会随着方案的不同而不同。

实际上,大部分的URL都遵循通用的URL,而且不少的URL方案的风格和语法都有不少的重叠。

大部分的URL方案的URL语法都建立在这个由9个部分构成的通用格式上:

1
<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>

URL最主要的三个部分是方案(scheme)、主机(host)和路径(path)。

组件 描述 默认值
方案 访问服务器以获取资源时要使用哪种协议
用户 某些方案访问资源时需要用户名 匿名
密码 用户名后面可能要包含的密码,中间由冒号(:)分割 <E-mail地址>
主机 资源宿主服务器的主机名或点分IP地址
端口 资源宿主服务器正在监听的端口号。很多方案都有默认端口号(HTTP默认端口号为80) 每个方案特有
路径 服务器资源的本地名,由一个斜杠(/)将其与前面的URL组件分隔开来,路径组件的语法是与服务器和方案有关的
参数 某些方案会有这个组件来指定输入参数。参数名为名/值对。URL中可以包含多个参数字段,它们相互之间以及与路径的其余部分之间用分号(;)分隔
查询 某些方案会用这个组件传递参数以激活应用程序(比如数据库、公告板、搜索引擎以及互联网网关)。查询组件的内容没有通用格式。用字符“?”将其与URL的其余部分分割开来。
片段 一小片或一部分资源的名称。引用对象时,不会讲frag字段传入服务器hegel字段是在客户端内部使用的。通过字符“#”来将其与URL的其余部分分割开来。

在我们使用Git开发项目的时候,可能经常会碰到个人和公司开发的项目都在一台机器上的情况。不管你们有没有,反正我是碰到了。因为公司有公司自己分配的邮箱,而我自己喜欢用自己的邮箱开发自己的项目。这样可能会导致邮箱混用的情况。

比如我们之前设置的命令是:

1
2
git config --global user.name &quot;aaa&quot;
git config --global user.email &quot;a@b.com&quot;

这样的话,就会像是上面说的不同的项目使用一个邮箱。

但是如果我应该如何为不同的项目设置不同的用户呢。其实很简单,就是把命令中的--global给去掉就好了。因为--global代表的就是全局化的意思。命令如下:

1
2
git config user.name &quot;aaa&quot;
git config user.email &quot;a@b.com&quot;

是不是很简单?

前言

公司APP的文章详情,之前是将所有的HTML内容全部从接口中返回,然后APP的webview将其载入到内中,然后渲染并展示出来。

但是这个速度太慢了。所以我们对其进行优化。修改后的加载流程如下:

  1. 将HTML模板和CSS,JS资源全部缓存到本地。
  2. Webview首先加载模板,然后JS调用原生接口请求动态内容。
  3. APP获取到资讯后调用JS接口,然后进行页面渲染。

这是修改后的执行流程。目前加载的过程提升了好几倍不止。

如果有机会会发出对比视频。

开发过程中碰到的坑

VasSonic

领导给推荐了这个:VasSonic 。但是实际的测试效果并不是特别明显,所以我们直接放弃了这个方法。

对于字符串中存在单引号,安卓不能正确传值

原因在于安卓调用js方法的方式。

在安卓中,调用的方式为:

1
webview.loadUrl(&quot;javascript:returnData(&#039;&quot;+content+&quot;&#039;)&quot;)

比如content的值为aa'a,那么替换值之后实际调用的代码为:

1
webview.loadUrl(&quot;javascript:returnData(&#039;aa&#039;a&#039;)&quot;);

其实仔细一看,就是需要对单引号进行转义。所以我的解决方案是在服务器端对单引号转义成HTML实体。这样传值和显示都会是正常的。

渲染的文章只显示图片不显示文字

原因是JS端获取宽度是使用的是clientWidth,对于像是我们这种设计的,有时候获取到的宽度为0,所以这个时候我们只能使用window.innerWidth获取宽度更加靠谱。

字符串中存在特殊字符,导致调用JS方法失败

其实最开始也不知道这个是什么字符。后来通过一点点排查,发现这个特殊的换行符。具体的解决方法我已经在这里进行记录:点击这里;

应用调用JS方法接收返回数据

因为安卓的原因,不知道为啥不不能接收数组,所以我将其转为json字符串。但是安卓说左右会多出两个双引号。

解决办法是安卓自己想办法将双引号给去掉了。

懒加载失效

在文章显示的时候,我增加了懒加载,使用的jquery_lazyload。但是实际在执行过程中,发现有几篇文章总是懒加载执行不成功。这个我没有找到原因,没办法,使用settimeout方法设置了延迟函数,延迟100毫秒再去执行懒加载。直接解决。

总结

其实我做这一套解决方案的时候,不仅仅是和安卓配合,还有跟IOS配合,有时候总是会出现安卓可以,IOS不行,或者反过来IOS可以,安卓不行的情况。

对于这种情况,需要学会具体分析。学会使用远程调试。这样才能更好的排查其中的问题。

我目前在做的webview渲染优化的事情。

目前碰到一个问题,就是说安卓APP请求服务器返回的字符串,然后调用参数传递内容。

但是在执行的时候,总是报错。

有一个不可见的字符,在编辑器中识别不出来,执行之后也看不到,但是查看上一条执行的命令的时候就可以看到一个红点。

然后使用unicode进行解析,发现unicode是\u2028。

经过百度发现了解决办法。

最后解决的办法很简单:

1
2
3
4
5
6
7
8
9
10
11
/**
* 替换字符串 处理Zp(\u2028)段落分隔符,Zl(\u2029)行分隔符
*
* @param $value
*/

function filterWord(&amp;$value) {
if (is_string($value)) {
$value = preg_replace(&#039;/[\p{Zp}\p{Zl}]+/u&#039;,&#039;&#039;,$value);
}
}

参考链接:

运行如下命令:

1
sudo tzselect

然后选择亚洲Asia,继续选择中国China,最后选择北京Beijing。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
baoguoxiao@bogon:~/windows/lumen$ sudo tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1

The following information has been given:

China
Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Selected time is now: Thu May 17 14:25:45 CST 2018.
Universal Time is now: Thu May 17 06:25:45 UTC 2018.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai

然后创建时区软链

1
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

现在看下时间,是不是已经正常了呢。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
class Node {
private $data;
private $next;

public function getData() {
return $this->data;
}

public function setData($data) {
$this->data = $data;
return true;
}

public function getNext() {
return $this->next;
}

public function setNext($next) {
$this->next = $next;
return true;
}
}

/**
* 链表类
*/
class Link {
private $size = 0;
private $first;
private $last;

/**
* 获取链表长度
*/
public function getLength() {
return $this->size;
}

/**
* 链表中插入第一个元素的时候,头和尾部都是同一个元素
*/
public function oneNode(string $element) {
$this->first = new Node;
$this->first->setData($element);
$this->last = $this->first;
}

/**
* 当只有一个元素的时候,删除$fist和$last
*/
public function clear() {
$this->first = $this->last = null;
}

/**
* 头节点插入
*/
public function addHead(string $element) {
if ($this->size == 0) {
$this->oneNode($element);
} else {
$node = new Node;
$node->setData($element);
$node->setNext($this->first);
$this->first = $node;
}
$this->size++;
return true;
}

/**
* 尾节点插入
*/
public function addTail(string $element) {
if ($this->size == 0) {
$this->oneNode($element);
} else {
$node = new Node();
$node->setData($element);
$this->last->setNext($node);
$this->last = $node;
}

$this->size++;
}

/**
* 中间节点插入
*/
public function add(int $index, string $element) {
if ($index <= $this->size) {
if ($this->size == 0) {
oneNode($element);
} elseif ($index == 0) {
$this->addHead($element);
} elseif ($index == $this->size) {
$this->addTail($element);
} else {
$tmp = $this->get($index - 1);
$node = new Node;
$node->setData($element);
$node->setNext($tmp->getNext());
$tmp->setNext($node);
}
$this->size++;
} else {
throw new \Exception("插入位置无效或超出链表长度");
}
}

/**
* 获取指定位置的元素
*/
public function get(int $index) {
$tmp = $this->first;
for ($i = 0; $i < $index - 1; $i++) {
$tmp = $tmp->getNext();
}
return $tmp;
}

/**
* 删除头节点
*/
public function deleteFirst() {
if ($this->size == 0) {
throw new \Exception("空链表,无元素可删除");
} elseif ($this->size == 1) {
$this->clear();
} else {
$tmp = $this->first;
$this->first = $tmp->getNext();
$this->size--;
}
}

/**
* 删除尾节点
*/
public function deleteLast() {
if ($this->size == 0) {
throw new \Exception("空链表,无元素可删除");
} elseif ($this->size == 1) {
$this->clear();
} else {
$tmp = $this->get($this->size - 1);
$tmp->setNext(null);
$this->size--;
}
}

/**
* 删除指定节点
*/
public function deleteIndex(int $index) {
if ($this->size == 0) {
throw new \Exception("空链表,无元素可删除");
} elseif ($this->size == 1) {
$this->clear();
} else {
$tmp = $this->get($index - 1);
$tmp->setNext($tmp->getNext()->getNext());
$this->size--;
}
}

/**
* 反转节点
*/
public function receve() {
if ($this->size < 2) {
return true;
} else {
$tmp = $this->first;
$last = $tmp;
$next = $this->first->getNext();
for($i = 0; $i < $this->size - 1; $i++) {
$nextNext = $next->getNext();
$next->setNext($tmp);
$tmp = $next;
$next = $nextNext;
}
$last->setNext(null);
$this->first = $tmp;
return true;
}
}

/**
* 打印现有的所有元素
*/
public function printLink() {
$tmp = $this->first;
if(is_null($tmp)) {
return false;
}
echo $tmp->getData();
while(!is_null($tmp->getNext())) {
$tmp = $tmp->getNext();
echo "->" . $tmp->getData();
}
echo "\n";
}
}

$link = new Link();
$link->addHead("1");
$link->printLink(); // 1

$link->addHead("5");
$link->printLink(); // 5->1

$link->addTail("9");
$link->printLink(); // 5->1->9

$link->addTail("7");
$link->printLink(); // 5->1->9->7

$link->add(3, "8");
$link->printLink(); // 5->1->9->8->7

print_r("链表长度:" . $link->getLength() . "\n");

$link->deleteFirst();
$link->printLink();

$link->deleteLast();
$link->printLink();

$link->deleteIndex(1);
$link->printLink();

print_r("链表长度:" . $link->getLength() . "\n");

这两天让安卓将json转成字符串传递给我,结果在传递中文时,PHP不能正常解析。

中间想过先把中文转成unicode。但是\u会变成\\u,还是有问题,无奈之下,只好自己上场,写了一个JAVA脚本提供给安卓了。

以下是所有的JAVA代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import net.sf.json.JSONObject;
import java.lang.Character.UnicodeBlock;

public class Main {

public static void main(String[] args) {
JSONObject json = new JSONObject();
json.put("name", "包裹小");
String result = json.toString();
System.out.println(result);

result = jsonParse(result);

System.out.println(result);
}

public static String jsonParse(String jsonStr) {
char[] myBuffer = jsonStr.toCharArray();

StringBuffer sb = new StringBuffer();
for (int i = 0; i < jsonStr.length(); i++) {
UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
// 判断是否是中日韩文字
if (ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
char c = jsonStr.charAt(i);
sb.append("\\u");
int j = (c >>>8); //取出高8位
String tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF); //取出低8位
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
} else {
sb.append(myBuffer[i]);
}
}

return sb.toString();
}
}

我们公司在微信认证服务号上开发一个小游戏。里面有个功能是用户分享到其他地方时使用自定义的标题和图标。我就这个功能记录一下中间遇到的坑。

首先,我因为之前没有开发过微信分享的功能,这个时候首先要读取一下微信的分享文档

为了方便开发,我这边是使用的微信的公众测试号。具体的申请地址可点击这里:mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

开发环境

JS-SDK使用权限需要签名,也就是wx.ready中的signature值,所以该值只能在服务器端实现签名的逻辑。我想了一个办法,就是该JS文件使用PHP生成,然后前端使用script元素引入该URL。这样就解决了签名需要在服务器端生成的问题。

具体到了签名生成的时候。appid和timestamp和nonceStr都好说。直接填写或者生成就好了。但是生成的signature这个值我中间碰到三个坑。第一个坑文章中正确介绍的签名的字符串的格式是这样的:

1
jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW×tamp=1414587457&url=http://mp.weixin.qq.com?params=value

但是我这边是使用的数组。然后先排序然后使用http_build_query方法生成的参与签名的字符串。这样就不对了。所以对于该参与校验的值只能进行赋值。类似这样:

1
jsapi_ticket={$ticker}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}

直接对其中的值进行赋值,进行签名即可。

下面说第二个坑。比如我们的活动地址是https://m.abc.com/Activity/index 但是在微信中访问的时候,会在后面自定义添加参数。比如有时候地址会变成这样:https://m.abc.com/Activity/index.html?from=singlemessage&isappinstalled=0。对于这种签名的规则,微信是需要把这种自带参数也带入签名。

所以我想了这么一个办法。就是说把该JS文件携带URL地址请求我,我这边获取URL,并将URL加入到签名中。这样的话,前端将引入脚本写死就不好了。所以我和前端一块将加载js脚本给换成了下面这样:

1
2
3
4
5
6
<script type="text/javascript">
var script =document.createElement("script");
var url = window.location.href;
script.src="/Static/wxShare?url=" + encodeURIComponent(url);
document.getElementsByTagName(&#039;body&#039;)[0].appendChild(script);
</script>

第三个坑,获取jsapi_ticket票据的值是JAVA实现的。但是在获取的时候并不能获取成功,经过跟JAVA沟通,发现是之前开发设置的测试的appid已经过期了。所以,失效了,重新设置了一下,就正常了。

至此,开发联调就结束了。下面就提测进行测试阶段了。

测试环境。

测试环境一直分享失败。然后开启debug模式。发现也是提示sign值不可用。排查这个问题,遇到了两个坑。

第一个坑,是请求JAVA的服务器获取jsapi_ticket票据,提示操作失败。经过JAVA排查,发现是少设置了一个参数。好了,经过修改,能够正常获取jsapi_ticket了。

第二个坑,则是测试这边换了JAVA配置中的appid的值的配置,结果access_token因为缓存起来了,结果还是存的之前的appid的access_token。接着获取的jsapi_ticket也是根据之前的access_token进行获取的。所以一直不成功,后来经过清除缓存。然后测试就ok了。

好了。其实这个并没有按照教程的形式来介绍一下我的开发工作。主要是把我中间遇到的坑给解释一下。

  1. 研究对象统称为元素。把一些元素组成的总体叫做集合(简称为集)。
  2. 给定的集合必须是可确定的。也就是说给定一个集合,那么任何一个元素在不在这个集合中就确定了。
  3. 一个集合中的元素是互为不同的。也就是说,集合中的元素是不重复出现的。
  4. 只要构成两个集合的元素是相同的,那么这两个集合就是相等的。
  5. 如果a是集合A的元素,就说a属于集合A,记作a∈A。如果a不是集合a中的元素,那就a不属于集合A,记作a∉A。
  6. 全体非负整数组成的集合称为非负数整数集(或自然数集),记作N。
  7. 所有正整数组成的集合称为正整数集,记作N*或N+。
  8. 全体整数组成的集合称为整数集,记作Z。
  9. 全体有理数组成的集合称为有理数集,记作Q。
  10. 全体实数组合的集合称为实数集,记作R。
  11. 列举法指的是将远远一一列举出来,并用花括号“{}”括起来表示集合的方法叫做列举法。例如:小于10的所有自然数组成的集合是{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}。
  12. 对于使用列举法列举不完的,我们可以使用描述法。比如x-7<3的解集我们可以使用D={x∈R|x<10}。
  13. 如果集合A中任意一个元素都是集合B中的元素,那么这两个集合有包含关系,成集合A为集合B的子集。记作A⊆B(或B⊇A),读作“A含于B”(或者“B包含A”)。
  14. 在数学中,经常用在平台上封闭曲线的内部代表集合的图,被称为Venn图。
  15. 如果集合A是集合B的子集,且结合B是集合A的子集,辞职集合A与集合B是相等的,那么集合A和集合B相等,记作A=B。
  16. 如果集合A⊆B,但是存在元素x∈B,且x∉A,我们称集合A是集合B的真子集,记作A⊊B(或B⊋A)。
  17. 不包含任何元素的集合叫做空集,记作Ø,并规定,空集是任何集合的子集。
  18. 由所有属于集合A或属于集合B的元素组成的集合,称为集合A与B的并集,记作A∪B,读作“A并B”,即A∪B={x|x∈A,或x∈B}。
  19. 由属于集合A且属于集合B的所有元素组成的集合,称为A与B的交集,记作A∩B(读作“A交B”),即A∩B={x|x∈A,且x∈B}。
  20. 如果一个集合含有所研究问题中设计的所有元素,那么就称为这个集合为全集,通常记作U。
  21. 由全集U中不属于结合A的所有元素组成的集合称为集合A相对于全集U的补集,简称为集合A的补集,记作CuA。即CuA={x|x∈U, 且x∉A}。

docker学习笔记之三 容器

现在我们根据教程创建一个容器。

使用Dockerfile定义一个容器

首先我们先创建一个文件夹。我创建的文件夹的路径如下:

1
2
$ pwd
/home/baoguoxiao/docker

我们使用cd命令进入到该文件夹。然后我们创建一个叫做Dockerfile的文件。把以下内容复制并粘贴到刚才创建的文件中并保存。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 使用官方的python作为父镜像
FROM python:2.7-slim

# 设置工作目录为 /app
WORKDIR /app

# 复制当前文件夹的内容到容器的 /app目录
ADD . /app

# 安装在requirements.txt中指定的所必须的包
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# 定义80端口对容器外可用
EXPOSE 80

# 定义环境变量
ENV NAME World

# 当容器启动时运行 app.py
CMD ["python", "app.py"]

这个文件提到了我们两个没有创建的文件,requirements.txtapp.py,下面我将会给出两个文件的内容,将以下内容复制并粘贴到相应的文件中,并且这两个文件和Dockerfile一样,属于同级目录。

requirements.txt

1
2
Flask
Redis

app.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"

html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)

可以看到我们安装了Flask和Redis库,但是我们并没有安装Redis数据库,所以我们期望在完成的时候能够提示错误信息。

构建应用程序

我们准备要构建我们的程序了,首先我们要保证我们处于原来的目录。现在我们看下我们目前的目录的内容:

1
2
$ ls
app.py Dockerfile requirements.txt

现在我们执行命令,它会创建一个镜像。使用**-t**设定一个更加友好的名字:

1
docker build -t friendlyhello .

现在我们查看一下我们的镜像列表:

1
2
3
4
5
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest bc24eaee5b6d 10 minutes ago 148MB
python 2.7-slim 4fd30fc83117 8 weeks ago 138MB
hello-world latest f2a91732366c 2 months ago 1.85kB

运行APP

运行APP,我们使用机器的4000端口跟发布的容器的80端口进行映射:

1
docker run -p 4000:80 friendlyhello

启动之后,我们就可以访问 http://localhost:4000 来访问了。

我们这里是使用真实系统的4000端口来映射到我们生成的镜像的80端口。

Ctrl+C强制退出。

现在我们用后台守护进程运行程序。

1
docker run -d -p 4000:80 friendlyhello

现在我们使用以下命令查看目前正在运行的容器。

1
2
3
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27d94a4c5481 friendlyhello "python app.py" 7 minutes ago Up 7 minutes 0.0.0.0:4000->80/tcp vigilant_easley

现在我们将我们运行的docker停止。

1
docker container stop 27d94a4c5481

27d94a4c5481是我运行的容器ID。每台机器的容器ID是不同,在运行此命令时请自主替换相关容器ID。

分享镜像

首先我们需要执行以下命令进行登陆,期间会要求输入docker ID以及密码,如果没有账号,可点击此处进行账号注册。

首先我们推荐设定一个tag,这样才能方便对镜像进行管理。首先我们先看看语法格式:

1
docker tag image username/repository:tag

例如:

1
docker tag mowangjuanzi/get-started:part2

然后我们将这个镜像进行上传:

1
docker push mowangjuanzi/get-started:part2

命令总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker build -t friendlyhello .  # 使用目录的Dockerfile文件创建镜像
docker run -p 4000:80 friendlyhello # 运行"friendlyname"映射端口4000到80
docker run -d -p 4000:80 friendlyhello # 同上,但是是后台运行模式
docker container ls # 列出所有运行的容器
docker container ls -a # 列出所有容器,甚至那些不运行的
docker container stop <hash> # 优雅的停止指定容器
docker container kill <hash> # 强制关闭指定容器
docker container rm <hash> # 从这个机器中移除指定容器
docker container rm $(docker container ls -a -q) # 移除全部容器
docker image ls -a # 列出这个机器中所有的镜像
docker image rm <image id> # 从这个机器中移除指定镜像
docker image rm $(docker image ls -a -q) # 从这个机器中移除所有镜像
docker login # 使用你的docker证书进行登陆
docker tag <image> username/repository:tag # tag要上传到存储库的<image>
docker push username/repository:tag # 上传标记的镜像到docker光放存储库
docker run username/repository:tag # 从存储库运行镜像

docker学习笔记之二 起步和入门

测试Docker版本

1
2
$ docker --version
Docker version 17.12.0-ce, build c97c6d6

运行docker version(不带**–)或者docker info**来获取docker安装的更多信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ sudo docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:11:14 2017
OS/Arch: linux/amd64

Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:09:47 2017
OS/Arch: linux/amd64
Experimental: false

测试已安装的Docker

如果运行简单的Docker 镜像(hello-world)来测试你的安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

列出已经下载到你的机器中的hello-world镜像:

1
2
3
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 2 months ago 1.85kB

列出由镜像产生的hello-world容器,当显示消息后就会退出。如果它在运行中,你不需要使用**–all**选项:

1
2
3
4
$ docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65fbaf599670 hello-world "/hello" 23 minutes ago Exited (0) 23 minutes ago silly_mclean
419dce2f6ec9 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago eloquent_visvesvaraya

命令总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
## 列出 Docker CLI 命令
docker
docker container --help

## 显示 Docker 版本和信息
docker --version
docker version
docker info

## 执行 Docker 镜像
docker run hello-world

## 列出 Docker 镜像
docker image ls

## 理出 Docker 容器 (running, all, all in quiet mode)
docker container ls
docker container ls -all
docker container ls -a -q

系统要求

安装docker需要以下版本系统的64位系统:

  • Artful 17.10 (Docker CE 17.11 Edge and higher only)
  • Zesty 17.04
  • Xenial 16.04 (LTS)
  • Trusty 14.04 (LTS)

卸载老版本

老版本的docker叫做docker或者docker-engine,如果安装了,就使用以下命令进行卸载:

1
sudo apt-get remove docker docker-engine docker.io

设置使用源安装

  • 更新源:
1
sudo apt-get update
  • 使用apt使用HTTPS安装包
1
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  • 添加docker官方GPG密钥:
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • 使用下列命令设置stable
1
sudo add-apt-repository  &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot;

安装 docker cc(社区版)

  • 更新索引。
1
sudo apt-get update
  • 安装
1
sudo apt-get install docker-ce
  • 验证安装
1
sudo docker run hello-world

这个命令会下载一个测试镜像,然后在容器中运行它。当容器运行时,它会输出一个消息并退出。

卸载Docker CE

  • 卸载Docker CE包
1
sudo apt-get purge docker-ce
  • 镜像,容器,卷或者自定义配置文件不会自动移除。要删除所有的镜像,容器和卷,请执行以下命令:
1
sudo rm -rf /var/lib/docker

安装之后

sudo执行docker命令,防止出现以下权限不足的错误提示:

1
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.35/version: dial unix /var/run/docker.sock: connect: permission denied

执行命令如下:

1
2
sudo gpasswd -a ${USER} docker
newgrp - docker

我现在有一个 thinkphp 文件夹,我需要将文件夹下面所有的 .class.php 修改为 .php

我的执行方法是:

1
find ./thinkphp2 -name *.class.php | awk -F "class." '{print "mv "$0 " " $1$2}' | sh

这句话的作用就是首先我们先找出所有文件夹里面后缀名为**.class.php**的文件,执行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
baoguoxiao@192:~/windows$ find ./thinkphp2 -name *.class.php
./thinkphp2/ThinkPHP/Library/Behavior/AgentCheckBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/BorisBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/BrowserCheckBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/CheckActionRouteBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/CheckLangBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/ChromeShowPageTraceBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/CronRunBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/FireShowPageTraceBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/RobotCheckBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/ShowRuntimeBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/TokenBuildBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/UpgradeNoticeBehavior.class.php
./thinkphp2/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php
./thinkphp2/ThinkPHP/Library/Org/Net/Http.class.php
./thinkphp2/ThinkPHP/Library/Org/Net/IpLocation.class.php
./thinkphp2/ThinkPHP/Library/Org/Util/ArrayList.class.php
./thinkphp2/ThinkPHP/Library/Org/Util/CodeSwitch.class.php
./thinkphp2/ThinkPHP/Library/Org/Util/Date.class.php
./thinkphp2/ThinkPHP/Library/Org/Util/Rbac.class.php
./thinkphp2/ThinkPHP/Library/Org/Util/Stack.class.php
./thinkphp2/ThinkPHP/Library/Org/Util/String.class.php
./thinkphp2/ThinkPHP/Library/Think/App.class.php
./thinkphp2/ThinkPHP/Library/Think/Auth.class.php
./thinkphp2/ThinkPHP/Library/Think/Behavior.class.php
./thinkphp2/ThinkPHP/Library/Think/Build.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Apachenote.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Apc.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Db.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Eaccelerator.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/File.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcache.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcached.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcachesae.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Shmop.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Sqlite.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Wincache.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Xcache.class.php
./thinkphp2/ThinkPHP/Library/Think/Cache.class.php
./thinkphp2/ThinkPHP/Library/Think/Controller/HproseController.class.php
./thinkphp2/ThinkPHP/Library/Think/Controller/JsonRpcController.class.php
./thinkphp2/ThinkPHP/Library/Think/Controller/RestController.class.php
./thinkphp2/ThinkPHP/Library/Think/Controller/RpcController.class.php
./thinkphp2/ThinkPHP/Library/Think/Controller/YarController.class.php
./thinkphp2/ThinkPHP/Library/Think/Controller.class.php
./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Base64.class.php
./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Crypt.class.php
./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Des.class.php
./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Think.class.php
./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Xxtea.class.php
./thinkphp2/ThinkPHP/Library/Think/Crypt.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Firebird.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Mysql.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Oracle.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Pgsql.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Sqlite.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Sqlsrv.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Driver.class.php
./thinkphp2/ThinkPHP/Library/Think/Db/Lite.class.php
./thinkphp2/ThinkPHP/Library/Think/Db.class.php
./thinkphp2/ThinkPHP/Library/Think/Dispatcher.class.php
./thinkphp2/ThinkPHP/Library/Think/Exception.class.php
./thinkphp2/ThinkPHP/Library/Think/Hook.class.php
./thinkphp2/ThinkPHP/Library/Think/Image/Driver/Gd.class.php
./thinkphp2/ThinkPHP/Library/Think/Image/Driver/GIF.class.php
./thinkphp2/ThinkPHP/Library/Think/Image/Driver/Imagick.class.php
./thinkphp2/ThinkPHP/Library/Think/Image.class.php
./thinkphp2/ThinkPHP/Library/Think/Log/Driver/File.class.php
./thinkphp2/ThinkPHP/Library/Think/Log/Driver/Sae.class.php
./thinkphp2/ThinkPHP/Library/Think/Log.class.php
./thinkphp2/ThinkPHP/Library/Think/Model/AdvModel.class.php
./thinkphp2/ThinkPHP/Library/Think/Model/MergeModel.class.php
./thinkphp2/ThinkPHP/Library/Think/Model/MongoModel.class.php
./thinkphp2/ThinkPHP/Library/Think/Model/RelationModel.class.php
./thinkphp2/ThinkPHP/Library/Think/Model/ViewModel.class.php
./thinkphp2/ThinkPHP/Library/Think/Model.class.php
./thinkphp2/ThinkPHP/Library/Think/Page.class.php
./thinkphp2/ThinkPHP/Library/Think/Route.class.php
./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Db.class.php
./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Memcache.class.php
./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Mysqli.class.php
./thinkphp2/ThinkPHP/Library/Think/Storage/Driver/File.class.php
./thinkphp2/ThinkPHP/Library/Think/Storage/Driver/Sae.class.php
./thinkphp2/ThinkPHP/Library/Think/Storage.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Ease.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Lite.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Mobile.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Smart.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Smarty.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/TagLib/Html.class.php
./thinkphp2/ThinkPHP/Library/Think/Template/TagLib.class.php
./thinkphp2/ThinkPHP/Library/Think/Template.class.php
./thinkphp2/ThinkPHP/Library/Think/Think.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/bcs.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/mimetypes.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/requestcore.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Ftp.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Local.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Qiniu/QiniuStorage.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Qiniu.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Sae.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Upyun.class.php
./thinkphp2/ThinkPHP/Library/Think/Upload.class.php
./thinkphp2/ThinkPHP/Library/Think/Verify.class.php
./thinkphp2/ThinkPHP/Library/Think/View.class.php
./thinkphp2/ThinkPHP/Library/Vendor/Smarty/Smarty.class.php
./thinkphp2/ThinkPHP/Library/Vendor/Smarty/SmartyBC.class.php
./thinkphp2/ThinkPHP/Mode/Api/App.class.php
./thinkphp2/ThinkPHP/Mode/Api/Controller.class.php
./thinkphp2/ThinkPHP/Mode/Api/Dispatcher.class.php
./thinkphp2/ThinkPHP/Mode/Lite/App.class.php
./thinkphp2/ThinkPHP/Mode/Lite/Controller.class.php
./thinkphp2/ThinkPHP/Mode/Lite/Dispatcher.class.php
./thinkphp2/ThinkPHP/Mode/Lite/Model.class.php
./thinkphp2/ThinkPHP/Mode/Lite/View.class.php

接下来我们对文件内容进行拆分,然后组合成正确的文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
baoguoxiao@192:~/windows$ find ./thinkphp2 -name *.class.php | awk -F ".class" '{print "mv " $0 " " $1$2}'
mv ./thinkphp2/ThinkPHP/Library/Behavior/AgentCheckBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/AgentCheckBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/BorisBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/BorisBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/BrowserCheckBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/BrowserCheckBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/BuildLiteBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/CheckActionRouteBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/CheckActionRouteBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/CheckLangBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/CheckLangBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/ChromeShowPageTraceBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/ChromeShowPageTraceBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/ContentReplaceBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/CronRunBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/CronRunBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/FireShowPageTraceBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/FireShowPageTraceBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/ParseTemplateBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/RobotCheckBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/RobotCheckBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/ShowRuntimeBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/ShowRuntimeBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/TokenBuildBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/TokenBuildBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/UpgradeNoticeBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/UpgradeNoticeBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ./thinkphp2/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.php
mv ./thinkphp2/ThinkPHP/Library/Org/Net/Http.class.php ./thinkphp2/ThinkPHP/Library/Org/Net/Http.php
mv ./thinkphp2/ThinkPHP/Library/Org/Net/IpLocation.class.php ./thinkphp2/ThinkPHP/Library/Org/Net/IpLocation.php
mv ./thinkphp2/ThinkPHP/Library/Org/Util/ArrayList.class.php ./thinkphp2/ThinkPHP/Library/Org/Util/ArrayList.php
mv ./thinkphp2/ThinkPHP/Library/Org/Util/CodeSwitch.class.php ./thinkphp2/ThinkPHP/Library/Org/Util/CodeSwitch.php
mv ./thinkphp2/ThinkPHP/Library/Org/Util/Date.class.php ./thinkphp2/ThinkPHP/Library/Org/Util/Date.php
mv ./thinkphp2/ThinkPHP/Library/Org/Util/Rbac.class.php ./thinkphp2/ThinkPHP/Library/Org/Util/Rbac.php
mv ./thinkphp2/ThinkPHP/Library/Org/Util/Stack.class.php ./thinkphp2/ThinkPHP/Library/Org/Util/Stack.php
mv ./thinkphp2/ThinkPHP/Library/Org/Util/String.class.php ./thinkphp2/ThinkPHP/Library/Org/Util/String.php
mv ./thinkphp2/ThinkPHP/Library/Think/App.class.php ./thinkphp2/ThinkPHP/Library/Think/App.php
mv ./thinkphp2/ThinkPHP/Library/Think/Auth.class.php ./thinkphp2/ThinkPHP/Library/Think/Auth.php
mv ./thinkphp2/ThinkPHP/Library/Think/Behavior.class.php ./thinkphp2/ThinkPHP/Library/Think/Behavior.php
mv ./thinkphp2/ThinkPHP/Library/Think/Build.class.php ./thinkphp2/ThinkPHP/Library/Think/Build.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Apachenote.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Apachenote.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Apc.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Apc.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Db.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Db.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Eaccelerator.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Eaccelerator.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/File.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/File.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcache.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcache.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcached.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcached.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcachesae.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Memcachesae.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Redis.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Redis.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Shmop.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Shmop.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Sqlite.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Sqlite.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Wincache.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Wincache.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Xcache.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache/Driver/Xcache.php
mv ./thinkphp2/ThinkPHP/Library/Think/Cache.class.php ./thinkphp2/ThinkPHP/Library/Think/Cache.php
mv ./thinkphp2/ThinkPHP/Library/Think/Controller/HproseController.class.php ./thinkphp2/ThinkPHP/Library/Think/Controller/HproseController.php
mv ./thinkphp2/ThinkPHP/Library/Think/Controller/JsonRpcController.class.php ./thinkphp2/ThinkPHP/Library/Think/Controller/JsonRpcController.php
mv ./thinkphp2/ThinkPHP/Library/Think/Controller/RestController.class.php ./thinkphp2/ThinkPHP/Library/Think/Controller/RestController.php
mv ./thinkphp2/ThinkPHP/Library/Think/Controller/RpcController.class.php ./thinkphp2/ThinkPHP/Library/Think/Controller/RpcController.php
mv ./thinkphp2/ThinkPHP/Library/Think/Controller/YarController.class.php ./thinkphp2/ThinkPHP/Library/Think/Controller/YarController.php
mv ./thinkphp2/ThinkPHP/Library/Think/Controller.class.php ./thinkphp2/ThinkPHP/Library/Think/Controller.php
mv ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Base64.class.php ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Base64.php
mv ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Crypt.class.php ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Crypt.php
mv ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Des.class.php ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Des.php
mv ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Think.class.php ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Think.php
mv ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Xxtea.class.php ./thinkphp2/ThinkPHP/Library/Think/Crypt/Driver/Xxtea.php
mv ./thinkphp2/ThinkPHP/Library/Think/Crypt.class.php ./thinkphp2/ThinkPHP/Library/Think/Crypt.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Firebird.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Firebird.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Mongo.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Mysql.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Mysql.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Oracle.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Oracle.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Pgsql.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Pgsql.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Sqlite.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Sqlite.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Sqlsrv.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver/Sqlsrv.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Driver.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Driver.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db/Lite.class.php ./thinkphp2/ThinkPHP/Library/Think/Db/Lite.php
mv ./thinkphp2/ThinkPHP/Library/Think/Db.class.php ./thinkphp2/ThinkPHP/Library/Think/Db.php
mv ./thinkphp2/ThinkPHP/Library/Think/Dispatcher.class.php ./thinkphp2/ThinkPHP/Library/Think/Dispatcher.php
mv ./thinkphp2/ThinkPHP/Library/Think/Exception.class.php ./thinkphp2/ThinkPHP/Library/Think/Exception.php
mv ./thinkphp2/ThinkPHP/Library/Think/Hook.class.php ./thinkphp2/ThinkPHP/Library/Think/Hook.php
mv ./thinkphp2/ThinkPHP/Library/Think/Image/Driver/Gd.class.php ./thinkphp2/ThinkPHP/Library/Think/Image/Driver/Gd.php
mv ./thinkphp2/ThinkPHP/Library/Think/Image/Driver/GIF.class.php ./thinkphp2/ThinkPHP/Library/Think/Image/Driver/GIF.php
mv ./thinkphp2/ThinkPHP/Library/Think/Image/Driver/Imagick.class.php ./thinkphp2/ThinkPHP/Library/Think/Image/Driver/Imagick.php
mv ./thinkphp2/ThinkPHP/Library/Think/Image.class.php ./thinkphp2/ThinkPHP/Library/Think/Image.php
mv ./thinkphp2/ThinkPHP/Library/Think/Log/Driver/File.class.php ./thinkphp2/ThinkPHP/Library/Think/Log/Driver/File.php
mv ./thinkphp2/ThinkPHP/Library/Think/Log/Driver/Sae.class.php ./thinkphp2/ThinkPHP/Library/Think/Log/Driver/Sae.php
mv ./thinkphp2/ThinkPHP/Library/Think/Log.class.php ./thinkphp2/ThinkPHP/Library/Think/Log.php
mv ./thinkphp2/ThinkPHP/Library/Think/Model/AdvModel.class.php ./thinkphp2/ThinkPHP/Library/Think/Model/AdvModel.php
mv ./thinkphp2/ThinkPHP/Library/Think/Model/MergeModel.class.php ./thinkphp2/ThinkPHP/Library/Think/Model/MergeModel.php
mv ./thinkphp2/ThinkPHP/Library/Think/Model/MongoModel.class.php ./thinkphp2/ThinkPHP/Library/Think/Model/MongoModel.php
mv ./thinkphp2/ThinkPHP/Library/Think/Model/RelationModel.class.php ./thinkphp2/ThinkPHP/Library/Think/Model/RelationModel.php
mv ./thinkphp2/ThinkPHP/Library/Think/Model/ViewModel.class.php ./thinkphp2/ThinkPHP/Library/Think/Model/ViewModel.php
mv ./thinkphp2/ThinkPHP/Library/Think/Model.class.php ./thinkphp2/ThinkPHP/Library/Think/Model.php
mv ./thinkphp2/ThinkPHP/Library/Think/Page.class.php ./thinkphp2/ThinkPHP/Library/Think/Page.php
mv ./thinkphp2/ThinkPHP/Library/Think/Route.class.php ./thinkphp2/ThinkPHP/Library/Think/Route.php
mv ./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Db.class.php ./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Db.php
mv ./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Memcache.class.php ./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Memcache.php
mv ./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Mysqli.class.php ./thinkphp2/ThinkPHP/Library/Think/Session/Driver/Mysqli.php
mv ./thinkphp2/ThinkPHP/Library/Think/Storage/Driver/File.class.php ./thinkphp2/ThinkPHP/Library/Think/Storage/Driver/File.php
mv ./thinkphp2/ThinkPHP/Library/Think/Storage/Driver/Sae.class.php ./thinkphp2/ThinkPHP/Library/Think/Storage/Driver/Sae.php
mv ./thinkphp2/ThinkPHP/Library/Think/Storage.class.php ./thinkphp2/ThinkPHP/Library/Think/Storage.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Ease.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Ease.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Lite.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Lite.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Mobile.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Mobile.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Smart.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Smart.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Smarty.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/Driver/Smarty.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/TagLib/Cx.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/TagLib/Html.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/TagLib/Html.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template/TagLib.class.php ./thinkphp2/ThinkPHP/Library/Think/Template/TagLib.php
mv ./thinkphp2/ThinkPHP/Library/Think/Template.class.php ./thinkphp2/ThinkPHP/Library/Think/Template.php
mv ./thinkphp2/ThinkPHP/Library/Think/Think.class.php ./thinkphp2/ThinkPHP/Library/Think/Think.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/bcs.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/bcs.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/mimetypes.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/mimetypes.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/requestcore.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs/requestcore.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Bcs.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Ftp.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Ftp.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Local.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Local.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Qiniu/QiniuStorage.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Qiniu/QiniuStorage.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Qiniu.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Qiniu.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Sae.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Sae.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Upyun.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload/Driver/Upyun.php
mv ./thinkphp2/ThinkPHP/Library/Think/Upload.class.php ./thinkphp2/ThinkPHP/Library/Think/Upload.php
mv ./thinkphp2/ThinkPHP/Library/Think/Verify.class.php ./thinkphp2/ThinkPHP/Library/Think/Verify.php
mv ./thinkphp2/ThinkPHP/Library/Think/View.class.php ./thinkphp2/ThinkPHP/Library/Think/View.php
mv ./thinkphp2/ThinkPHP/Library/Vendor/Smarty/Smarty.class.php ./thinkphp2/ThinkPHP/Library/Vendor/Smarty/Smarty.php
mv ./thinkphp2/ThinkPHP/Library/Vendor/Smarty/SmartyBC.class.php ./thinkphp2/ThinkPHP/Library/Vendor/Smarty/SmartyBC.php
mv ./thinkphp2/ThinkPHP/Mode/Api/App.class.php ./thinkphp2/ThinkPHP/Mode/Api/App.php
mv ./thinkphp2/ThinkPHP/Mode/Api/Controller.class.php ./thinkphp2/ThinkPHP/Mode/Api/Controller.php
mv ./thinkphp2/ThinkPHP/Mode/Api/Dispatcher.class.php ./thinkphp2/ThinkPHP/Mode/Api/Dispatcher.php
mv ./thinkphp2/ThinkPHP/Mode/Lite/App.class.php ./thinkphp2/ThinkPHP/Mode/Lite/App.php
mv ./thinkphp2/ThinkPHP/Mode/Lite/Controller.class.php ./thinkphp2/ThinkPHP/Mode/Lite/Controller.php
mv ./thinkphp2/ThinkPHP/Mode/Lite/Dispatcher.class.php ./thinkphp2/ThinkPHP/Mode/Lite/Dispatcher.php
mv ./thinkphp2/ThinkPHP/Mode/Lite/Model.class.php ./thinkphp2/ThinkPHP/Mode/Lite/Model.php
mv ./thinkphp2/ThinkPHP/Mode/Lite/View.class.php ./thinkphp2/ThinkPHP/Mode/Lite/View.php

awk中**-F**代表是分割符。这一段命令是我们使用分隔符将文件名拆开,然后打印是修改文件名的命令。

最后一步,我们就是将输出的内容导入到sh执行即可。

1
find ./thinkphp3 -name *.class.php | awk -F "class." '{print "mv "$0 " " $1$2}' | sh

参考资料:

现在我们有一个C语言文件(hello.c):

1
2
3
4
5
6
#include <stdio.h>

int main() {
printf("hello, world!\n");
return 0;
}

现在我们将执行以下命令将源文件(hello.c)转化为可执行目标文件(hello):

1
gcc -o hello hello.c

我们使用的是gcc编译器驱动程序完成的。这个转化的过程可分为四个阶段完成。执行这四个阶段的程序(预处理器、编译器、汇编器和链接器)一起构成了编译系统。

1
hello.c(源程序[文本])->预处理器(cpp)->hello.i(修改了的源程序[文本])->编译器(ccl)->hello.s(汇编程序[文本])->汇编器(as)->hello.o(可重定位目标程序[二进制])->链接器(ld)->hello(可执行目标程序[二进制])
  • 预处理阶段。预处理器(cpp)根据字符#开头的命令,修改原始的C程序。结果就得到了另外一个C程序,通常是以**.i**作为文件扩展名。
  • 编译阶段。编译器(ccl)将文本文件hello.i翻译成文本文件hello.s,它包含一个汇编语言程序。汇编程序是非常有用的,它位不同高级语言的不同编译器提供了通用的输出语言。
  • 汇编阶段。汇编器(as)将hello.s翻译成机器语言指令,将这些指令打包成一种叫做可重定位目标程序的格式,并将格式保存在目标文件hello.o中。如果我们使用文本编辑器打开hello.o文件,将会看到一堆乱码。
  • 链接阶段。hello程序调用了printf函数,它是每个C编译器都会提供的标准C库中的一个函数。printf函数存在于一个名为printf.o的单独的预编译好了的目标文件中,而这个文件必须以某种方式合并到我们的hello.o程序中,链接器(ld)就是处理这种合并,结果就得到了hello文件。它是一个可执行文件,可以被加载到内存中,由系统执行。

来源于:《深入理解计算机系统》 · 第三版

次方

设定a为某数,n为负整数。那么a的n次方则就表示为 1除以 a的n次方。

$$
a ^ n = 1 / a ^ n
$$

举例:10的-2次方就等于1除以10的平方,那么值就等于1/100,结果为0.01。

进制转换

整数部分十进制转二进制(除以2逆向取余法):

$$
108_{10} = 1 * 2^6 + 1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 0 * 2^0
$$

计算方法:

108 / 2 = 54 … 0
54 / 2 = 27 … 0
27 / 2 = 13 … 1
13 / 2 = 6 … 1
6 / 2 = 3 … 0
3 / 2 = 1 … 0
1 / 2 = 0 … 1

然后我们将上面的余数按照从下往上进行取数。即可得出 $$ 108_{10} = 1001100_2 $$ 。

小数部分十进制转二进制(乘以2顺向取整法):

0.875 * 2 = 1.75
0.75 * 2 = 1.5
0.5 * 2 = 1

我们可以查看以上的计算公式,对初始小数乘以2,当数值超过1时,继续将小数部分乘以2,一直到数值正好为1。

我们通过观察以上公式即可得出 $$ 0.875_{10} = 0.111_2 $$

原码,反码,补码

原码是一种计算机对二进制数字的表示方式。其中最高位为符号位。符号位0表示正数,符号位1表示负数。

反码是对原码进行取反。如果机器数是正数,那么反码与原码相同,如果是负数则对除符号位的其他位进行取反。

补码则是如果机器数整数则补码和原码相同,如果是负数,那么补码就是对除符号位的其他位进行取反,并且在末位+1。

其实非常简单。

首先我们编辑**~/.vimrc**文件。新增以下内容:

1
2
set ts=4
set expandtab

然后保存。

重启之后使用tab就是四个空格了。

公司的电脑是windows的。因为平常要用到linux的独有特性。比如swoole等等。所以,我在虚拟机中安装ubuntu。

好了,废话不多说,我是使用的自动安装,在安装之前就已经设定了登录的账号密码。安装完成是没有安装ssh服务的。

开启ssh服务

首先执行以下命令安装openssh

1
sudo apt install openssh-server -y

启动服务并且设置开机启动

1
2
sudo systemctl start ssh
sudo systemctl enable ssh

查看IP,并使用xshell连接

查看ip

1
ip addr

查看你的网卡信息,发现我的IP是192.168.110.128.

因为直接使用虚拟机内部的终端并不好用,不能够进行复制粘贴啥的,所以我使用外部的终端程序。我在这里使用的是xshell。

新建会话属性,输入主机ip(就是刚才我们获得的),在用户身份验证输入用户名和密码。

然后点击最下面的连接。会出现一个弹出框,选择“接口并保存”。

修改源

进入后第一件事就是修改apt源。

在修改源之前,我们先安装VIM编辑器:

1
sudo apt install vim -y

安装完成之后,我们要编辑apt源文件去除cd源。

1
sudo vi /etc/apt/sources.list

将第5行的配置进行注释:

1
# deb cdrom:[Ubuntu-Server 17.10 _Artful Aardvark_ - Release amd64 (20171017.1)]/ artful main restricted

修改源为阿里源

这是我从阿里云的帮助文件中复制到的源的内容。

1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-backports main restricted universe multiverse

首先我们需要对该内容进行修改。

我们查看版本信息:

1
2
3
4
5
6
baoguoxiao@ubuntu:~$ sudo lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.10
Release: 17.10
Codename: artful

可以看到codename的值是artful

我们可以把上面quantal全部替换为artful,替换完成如下:

1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ artful main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ artful-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ artful-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ artful-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ artful-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ artful main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ artful-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ artful-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ artful-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ artful-backports main restricted universe multiverse

然后将替换好的内容添加到**/etc/apt/sources.list**开头。

这样就可以**:x**进行保存了。

更新包

其实很简单。首先更新源:

1
sudo apt update 

在输出的消息的最后一行会输出以下内容:

1
46 packages can be upgraded. Run 'apt list --upgradable' to see them.

这就表示我有46个包需要更新。那么就执行运行以下命令进行更新吧:

1
sudo apt upgrade -y

好了,最开始的安装环境就已经搭建完成了。