Stat命令与文件时间

stat命令

stat命令常用来获取文件三个时间access time,modify timechange time

$ stat access.log
  File: `access.log'
  Size: 1559877779	Blocks: 3049624    IO Block: 4096   regular file
Device: ca20h/51744d	Inode: 16269326    Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/    work)   Gid: (  500/    work)
Access: 2014-03-09 21:58:33.000000000 +0800
Modify: 2014-03-07 08:17:36.000000000 +0800
Change: 2014-03-07 08:17:36.000000000 +0800

通常可以使用-c参数直接获取三个时间

$ stat -c %x access.log   #获取access time
2014-03-09 21:58:33.000000000 +0800
$ stat -c %y access.log  #获取modify time
2014-03-07 08:17:36.000000000 +0800
$ stat -c %z access.log #获取change time
2014-03-07 08:17:36.000000000 +0800
Read on →

Shell数组

数组定义

定义数组需要用括号把元素包裹起来。

colors=(red green blue black white)
# 打印整个数组
echo ${colors[*]}  # red green blue black white
echo ${colors[@]}  # red green blue black white
Read on →

Compile Ruby From Source

下载需要的软件包

Read on →

Curl

基本用法

1
2
3
4
curl http://www.google.com
curl --proxy http://proxy.com:8888 http://ww.google.com #使用代理
curl http://user:password@example.org/  #http验证
curl -u user:password http://example.org/ #http验证

获取响应头

1
2
curl -i http://example.com  #在返回结果中包含响应头 
curl -IL http://example.com  #仅返回响应头
Read on →

列出目录结构

最简单美观的方法tree

tree命令是专门用来罗列目录结构的,输出树形结果,很漂亮。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ tree demo
demo
├── Gemfile
├── boot.rb
├── collectors
├── config
│   └── mail_config.rb
├── controllers
├── db
│   ├── connection.rb
│   ├── database.yml
│   └── migrate
├── helpers
├── models
├── rakefile
└── views

8 directories, 6 files
Read on →

Find命令

find的基本语法

1
find PATH OPTIONS [-exec COMMANDD {} \;]

find命令可以使用多个OPTION,不同OPTION之间默认是and关系,除了and关系还有notor关系,如:

1
2
3
find / -name 'n1' -type f  #查找/目录下名称为n1且为普通文件的文件
find / -name 'n1' -o -name 'n2' #查找名称为n1或n2的文件
find / ! -name 'n1'  # 查找名称不为n1的文件

当使用的OPTION很多时,可以将OPTIONS括起来增加可读性,注意括号需要用\来转义,同时\(\)两边都需要有空格:

1
find / \( -name 'n1' -o -name 'n2' \)
Read on →

使用expect实现自动登录

网上有很多类似的文章,但很多都是先写expect脚本再从bash里调用expect脚本, 我希望直接在bash脚本里使用expect命令来实现自动登录。

利用expect命令实现自动登录并执行命令

1
2
3
4
5
6
7
8
9
#!/bin/bash
expect -c '
spawn ssh USER@HOST "commands"
expect {
"*(yes/no)?" { send "yes\r";exp_continue }
"*assword:" { send "PASSWORD\r" }
}
expect eof
'

关于expect的命令在网上有很多资料,这里不在赘述。下面讲讲怎么在bash和expect传递变量。

Read on →

Linux日常维护

某些用户cron任务失败

有时某些普通用户的crontab任务会失败,这可能是由于crond执行普通用户的任务时,是以非登录shell的形式切换到普通用户来执行的,所以可能缺失了某些环境变量。

解决办法是在crontab任务前先执行source /home/username/.bash_profile,后面再接用户自己的任务命令即可。

ps./etc/profile,~/.bash_profile,~/.bashrc三个脚本的区别:

  • /etc/profile #系统级初始化脚本,会被登录shell执行
  • ~/.bash_profile #用户配置,会被登录shell执行,非登录shell不执行
  • ~/.bashrc #非登录shell执行,但通常~/.bash_profile都会在代码里调用~/.bashrc,所以登录shell也执行它
Read on →

Sendmail发邮件中文乱码

如果这是要使用sendmail命令发送的邮件file内容:

Subject:标题
TO:to@example.com
From:from@example
Content-Type:text/html
<html>
	<body>
		邮件内容
	</body>
</html>

解决内容乱码

内容乱码比较好解决,首先内容先使用utf-8编码,然后在修改邮件的Content-Type为:

Content-Type:text/html;charset=UTF-8

解决标题乱码

需要利用base64编码标题内容,例如,如果UTF-8编码的字符串标题进行base64编码后的内容为5qCH6aKY,则邮件标题为:

Subject:=?UTF-8?B?5qCH6aKY?=

即邮件标题Subject:后字符串格式为:”=?UTF-8?B?base64编码的utf-8字串?=

发送邮件

最后发送文件可以正确显示:

1
cat file | sendmail -t

Axlsx报表工具(四)——条件格式化

定义格式化操作

条件格式化风格定义也是使用格式化定义语句add_style,不同的是必须将type指定为:dxf。

1
2
3
# define the style for conditional formatting
profitable = book.styles.add_style( :fg_color => "FF428751", :type => :dxf )
unprofitable = book.styles.add_style( :fg_color => "FF0000", :type => :dxf )

条件格式化有四种类型cellIs,colorScale,dataBar,iconSet。

Read on →