for 循环及指定范围语法
1 | 0...5 { print (i) } for i in |
游乐场
对于REPL,测试简单的代码是绰绰有余的,但是对于比较复杂一点的就需要使用Xcode的新特性游乐场了playground。
1 | 93> let color = ["red","orange","yellow","green"] |
1 | 126> var dict = ["name":"hello", "age":"10", "sex":"Female"] |
1 | var array [String : [String]] |
1 | 142> var empty_array : Array<Int> = [] |
1 | 151> var empty_dictionary = Dictionary<String,Double>() |
执行下面的命令可以进入Swift的交互终端。
1 | $ xcrun swift |
然后可以输入:help
打印帮助信息,:quit
来退出交互环境。
1 | let x = 12 // 常量 |
类型 | 特征 |
---|---|
Bool | true或者false |
Int/Int32/Int64 | 32/64位整数 |
Int8/Int16 | 8/16位整数 |
UInt/UInt32/UInt64 | 32/64位正整数 |
UInt8/UInt16 | 8/16位正整数 |
Float/Double | 可正可负的浮点数 |
Character | 用双引号括起的单个字符、数字或者其他符号 |
String | 用双括号括起的一系列字符 |
1 | 15> Int8.max |
1 | 22> var number : Double = 3 |
1 | 48> let b = 0b110 // 二进制 |
1 | 53> let big = 1_000_000_000 |
1 | 59> let city = "shanghai" |
1 | 72> typealias EightBits = UInt8 |
1 | 79> let car = (2014,"Mercedes-Benz","S-Class") |
在swift显示的类型说明中,有一个问号,可以表明变量的类型是可选类型。
1 | 83> var s="123" |
因为Hexo是基于Node.js环境的,所以我们需要安装Node.js.
1 | brew install npm |
安装Hexo
1 | npm install -g hexo-cli |
这里采用npm方式来部署hexo静态博客。
这里我们可以先建立一个文件夹,用来安装hexo
1 | mkdir hexo |
初始化Hexo
1 | hexo init |
生成静态页面
1 | hexo generate |
本地预览
1 | hexo server |
此时就可以打开浏览器输入http://localhost:4000
来预览了。
Linux ip
命令与 ifconfig
命令类似,但比 ifconfig
命令更加强大,主要用于显示或设置网络设备。
已经在Linux 2.2 加入到了内核。所以ip
是加强版的网络配置工具,用来替代ifconfig
并强化其他功能。
官方定义为:
ip - show / manipulate routing, devices, policy routing and tunnels
对于这个命令,命令集是相当的多。先说一些基础的,其他就要自己摸索了。
使用方法为:
1 | $ ip [ OPTIONS ] OBJECT { COMMAND | help } |
COMMAND的值主要取决于OBJECT,可能有所不同,一般可以使用add
,delete
和show
(或者list
),均可以输入help
来进行查询。
OBJECT中常用的为:
link
网络设备address
设备上的协议地址-s, -stats, -statistics
统计化输出1 | # 显示网络设备 |
命令中的show为默认,也可以直接使用ip link
或者ip address
,结果一致。
可以通过ip addr add/del xxx.xxx.xxx.xxx dev interface
来设置或者删除IP地址。
如下设置or删除eth0的IP地址。
1 | # 设置IP地址 |
与ifconfig类似,也使用up与down来进行启动和关闭,具体如下:
1 | # 开启网卡 |
选项-s可以统计一些信息方便我们阅读,如下看看网络的情况:
1 | $ ip -s link |
可以看到对输出进行了一些格式化,看起来更直观。
Linux ip
命令与 ifconfig
命令类似,但比 ifconfig
命令更加强大,主要用于显示或设置网络设备。
已经在Linux 2.2 加入到了内核。所以ip
是加强版的网络配置工具,用来替代ifconfig
并强化其他功能。
官方定义为:
ip - show / manipulate routing, devices, policy routing and tunnels
对于这个命令,命令集是相当的多。先说一些基础的,其他就要自己摸索了。
使用方法为:
1 | $ ip [ OPTIONS ] OBJECT { COMMAND | help } |
COMMAND的值主要取决于OBJECT,可能有所不同,一般可以使用add
,delete
和show
(或者list
),均可以输入help
来进行查询。
OBJECT中常用的为:
link
网络设备address
设备上的协议地址-s, -stats, -statistics
统计化输出1 | # 显示网络设备 |
命令中的show为默认,也可以直接使用ip link
或者ip address
,结果一致。
可以通过ip addr add/del xxx.xxx.xxx.xxx dev interface
来设置或者删除IP地址。
如下设置or删除eth0的IP地址。
1 | # 设置IP地址 |
与ifconfig类似,也使用up与down来进行启动和关闭,具体如下:
1 | # 开启网卡 |
选项-s可以统计一些信息方便我们阅读,如下看看网络的情况:
1 | $ ip -s link |
可以看到对输出进行了一些格式化,看起来更直观。
#TODO
OBJECT 取值含义如下:
OPTIONS 为常用选项,值可以是以下几种:
1 | ip link set eth0 promisc on # 开启网卡的混合模式 |
用 ip 命令显示网络设备的运行状态:
1 | [root@localhost ~]# ip link list |
显示核心路由表:
1 | [root@localhost ~]# ip route list |
显示邻居表:
1 | [root@localhost ~]# ip neigh list |
获取主机所有网络接口炫技:
1 | ip link | grep -E '^[0-9]' | awk -F: '{print $2}' |
-h, -human, -human-readable
output statistics with human readable values followed by suffix.
-b, -batch <FILENAME>
Read commands from provided file or standard input and invoke them. First failure will cause termination of ip.
-force Don't terminate ip on errors in batch mode. If there were any errors during execution of the commands, the application return code will be non zero.
-d, -details
Output more detailed information.
-l, -loops <COUNT>
Specify maximum number of loops the 'ip address flush' logic will attempt before giving up. The default is 10. Zero (0) means loop until all addresses are
removed.
-f, -family <FAMILY>
Specifies the protocol family to use. The protocol family identifier can be one of inet, inet6, bridge, ipx, dnet, mpls or link. If this option is not present,
the protocol family is guessed from other arguments. If the rest of the command line does not give enough information to guess the family, ip falls back to the
default one, usually inet or any. link is a special family identifier meaning that no networking protocol is involved.
-o, -oneline
output each record on a single line, replacing line feeds with the ‘' character. This is convenient when you want to count records with wc(1) or to grep(1) the
output.
-r, -resolve use the system’s name resolver to print DNS names instead of host addresses.
-n, -netns
switches ip to the specified network namespace NETNS. Actually it just simplifies executing of:
ip netns exec NETNS ip [ OPTIONS ] OBJECT { COMMAND | help }
to
ip -n[etns] NETNS [ OPTIONS ] OBJECT { COMMAND | help }
-a, -all
executes specified command over all objects, it depends if command supports this option.
-c, -color
Use color output.
-t, -timestamp
display current time when using monitor option.
-ts, -tshort
Like -timestamp, but use shorter format.
-rc, -rcvbuf<SIZE>
Set the netlink socket receive buffer size, defaults to 1MB.
-iec print human readable rates in IEC units (e.g. 1Ki = 1024).
IP - COMMAND SYNTAX
OBJECT
addrlabel
- label configuration for protocol address selection.
l2tp - tunnel ethernet over IP (L2TPv3).
maddress
- multicast address.
monitor
- watch for netlink messages.
mroute - multicast routing cache entry.
mrule - rule in multicast routing policy database.
neighbour
- manage ARP or NDISC cache entries.
netns - manage network namespaces.
ntable - manage the neighbor cache's operation.
route - routing table entry.
rule - rule in routing policy database.
tcp_metrics/tcpmetrics
- manage TCP Metrics
token - manage tokenized interface identifiers.
tunnel - tunnel over IP.
tuntap - manage TUN/TAP devices.
xfrm - manage IPSec policies.
The names of all objects may be written in full or abbreviated form, for example address can be abbreviated as addr or just a.
1 | sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ./` |
将当前目录下的所有.c、.h文件中的str1字符串替换为str2字符串。
参数解释:
sed:
grep
注:如果不需要查找子目录,仅需要在当前目录替换,可直接用sed命令:
1 | sed -i s/"str1"/"str2"/g ./*.[ch] |
sed -i '1d' filename.txt
sed -i '1,100d' filename.txt
sed -i '$d' filename.txt
sed ‘/hello/d’ filename.txt
在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:
1 | typedef unsigned short sa_family_t; |
在linux环境下,结构体struct sockaddr_in在/usr/include/netinet/in.h中定义,具体如下:
1 | /* Structure describing an Internet socket address. */ |
struct sockaddr
是通用的套接字地址,而struct sockaddr_in
则是internet环境下套接字的地址形式,二者长度一样,都是16个字节。二者是并列结构,指向sockaddr_in结构的指针也可以指向sockaddr。
一般情况下,需要把sockaddr_in结构强制转换成sockaddr结构再传入系统调用函数中。
下面是struct sockaddr_in中用到两个数据类型,具体定义如下:
1 | /* Type to represent a port. */ |
BSD网络软件中包含了两个函数,用来在二进制地址格式和点分十进制字符串格式之间相互转换,但是这两个函数仅仅支持IPv4。
1 | in_addr_t inet_addr(const char *cp); |
功能相似的两个函数同时支持IPv4和IPv6
1 | const char *inet_ntop(int domain, const void *addr, char *str, socklen_t size); |
通常的用法是:
1 | int sockfd; |
MacOSX上好用的工具不少,相信大家对 spotlight 和 alfred 印象很深刻。
今天推荐一款在Windows上使用的软件 Everything,用过MacOSX的童鞋应该都知道Spotlight的绚丽功能,至少这个功能我是一直在使用的,快速的搜索,方便(捉急)的快捷键,把输入法切换给占用了。
这个也是为数不多经常给大家推荐提高工作效率的软件。
Everything应该是为唯一一个民用的Haskell编写的软件,其他都是军用的^_^。
Everything体积小巧,界面简洁易用,快速建立索引,快速搜索,同时占用极低的系统资源,实时跟踪文件变化,并且还可以通过http或ftp形式分享搜索。
在搜索框输入文字,它就会只显示过滤后的文件和目录。Everything搜索只基于文件和文件夹的名称,所以它创建数据库很快。
快到啥程度呢,官方的说明是一个刚安装完的Windows XP SP2系统(约20,000份文件),需要一秒钟。索引一百万份文件则需要一分钟。
我积攒到现在接近600多GB的数据,400多万个文件,第一次索引只用了2分钟,确实快如闪电。
最关键的是,这个软件只有几百KB,只有几百KB,最新版本不倒500KB,你晓得什么概念吧。。。。你估计不晓得什么概念,21世纪初的软盘可以放几份拷贝在上面。
一句话, Everything是速度最快的文件搜索软件,和windows自带搜索功能相比,简直是一个天上一个地下了。当然也可以使用 Total Commander 或其他 ,不过我还是会推荐这款体积小巧、免安装、免费、速度极快(比Locate32更快)的文件搜索工具Everything.
常用的普通搜索就可以了,进阶版的可以使用正则表达式。
可以通过正则表达式更加高效地使用Everything,比如下面的一些技巧。
比如我希望找到一个2016年访问过的,文件超过1GB大小的视频,该视频包含了family字样,就可以用下面的搜索技巧搜索:
1 | video: size:>1GB dateaccessed:2016 family |
其他一些比较常用的为:
hello world
: 将搜索包含hello 和 world的文件或文件夹hello | world
: 将搜索包含hello 或 world的文件或文件夹hello !world
: 将搜索包含hello 不包含world 的文件或文件夹20??01
:因为?表示一个字符,所以这个表示搜索包含20??01的文件或文件夹,其中20与01之间包含2个字符20*01
:因为*表示匹配0或多个字符,所以搜索的范围比较广有一些有用的宏,比较方便使用,如下:
audio:
搜索音频文件zip:
搜索压缩文件.doc:
搜索文档文件.exe:
搜索可执行文件.pic:
搜索图片文件.video:
搜索视频文件.case:
区分大小写.file:
仅匹配文件.folder:
仅匹配文件夹.nocase:
不区分大小写.path:
匹配路径和文件名.regex:
启用正则表达式.wholefilename:
匹配完整文件名.
dateaccessed:<date>
搜索指定访问时间的文件和文件夹.datecreated:<date>
搜索指定创建日期的文件和文件夹.datemodified:<date>
搜索指定修改日期的文件和文件夹.size:<size>
搜索指定大小的文件 (以字节为单位),可以指定为kb,mb和gb接下来的就是开始下载使用吧。