dong teacher wang
读取一个包含一些文件名(包含全路径)的文件 all_files.txt
- 对于在磁盘上面存在的文件,里面的 abc 替换成 def,替换结果保存在和文件相同路径下面,取名为 '文件名.当前日期.fix' (例如脚本执行的时候是 2014.2.14 那么文件名 file1 对应的文件修改后文件为 file1.2014_02_14.fix)。
- 对于在磁盘上面不存在的文件,把他的文件名(包括全路径)输出到当前目录下面的 files_not_found.txt 这个文件里面。
读取的文件的名称是 all_files.txt,内容类似下面的
$ cat all_files.txt
/tmp/abc.txt
/tmp/def.txt
/tmp/abc/abc.txt
/tmp/a/fff.txt
Shell ac
#!/bin/bash
nowDate=`date +'%Y%m%d'`;
fName=$1
if [ ! -n "$fName" ]; then
fName=./all_files.txt
fi
cat $fName | while read line;
do
if [ -f $line ];
then
fileName=${line##*/};
filePath=${line%$fileName};
sed -e 's/abc/def/g' $line > $filePath$fileName.$nowDate.fix
else
echo $line > files_not_found.txt;
fi
done
About GNU/Linux
Table of Contents
必备技能之前的部分大概 1/3 时间,之后大概 2/3 时间。
什么是 GNU/Linux
- Linux = kernel ( Linus Torvalds )
- Gnu = Gnu System 大量软件 ( Richard Stallman )
GNU/Linux 1
GNU/Linux 的发展 http://www.suse.url.tw/sles10/lesson1.htm
free = freedom (not price)
ref: Revolution OS
各种发行版
- 什么是发行版?
- 包管理和 release 方式
- 有哪些发行版
- Ubuntu, debian, Slackware, Gentoo, Suse, redhat, Fedora, Centos …
为什么要讲 Linux
- 配备需求低廉
- 稳定,不需要关机维护
- 免费或者少许费用
- 公司大都是基于 Linux
Linux 和 windows 的区别
出错信息
Figure 1: 出错了之 windows
Figure 2: 出错了之 windows
Figure 3: 出错了之 Linux
比尔盖茨以技术顾问回微软上班第一天
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011 debug1: Reading configuration data /Users/cc/.ssh/config debug1: /Users/cc/.ssh/config line 21: Applying options for * debug1: Reading configuration data /etc/ssh_config debug1: /etc/ssh_config line 20: Applying options for * debug1: auto-mux: Trying existing master debug2: ssh_connect: needpriv 0 debug1: Connecting to wdicc.com [117.79.146.98] port 22. debug1: Connection established. debug3: Incorrect RSA1 identifier debug3: Could not load "/Users/cc/.ssh/id_rsa" as a RSA1 public key debug1: identity file /Users/cc/.ssh/id_rsa type 1 debug1: identity file /Users/cc/.ssh/id_rsa-cert type -1 debug1: identity file /Users/cc/.ssh/id_dsa type -1 debug1: identity file /Users/cc/.ssh/id_dsa-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.2 debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1 debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 pat OpenSSH_5* debug2: fd 3 setting O_NONBLOCK ............... It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is 84:9e:c9:8e:7f:36:28:08:7e:13:bf:43:12:74:11:4e. Please contact your system administrator. Add correct host key in /Users/cc/.ssh/known_hosts to get rid of this message. Offending RSA key in /Users/cc/.ssh/known_hosts:155 RSA host key for wdicc.com has changed and you have requested strict checking. Host key verification failed.
Linux 的目录结构
目录
- /
- /home
- /home/q
- /etc
- /usr
- /usr/local
- /var
- /bin
- /sbin
- /boot
- /lib
- /dev
- /dev/null
- /dev/shm
- /proc
- /opt /mnt
启动过程
必备技能
翻墙
- vpn
- goagent
编辑器
- nano
- vim vimtutor
- emacs M-x help-with-tutorial or C-h t
脚本语言
- shell
if [ "$a" = "abc" ];then echo "$a is abc" else echo "$a is not abc" fi
- man bash 搜索 Compound Commands
- shell基础12篇 shell13问
- perl/python/ruby/lua
- 解释执行
- 编译执行 6
文本工具
- less, more, most 7
- less 可以回退
- most is more then less
- grep, sed, awk
- grep 'abc' file
- sed 's/abc/def/g' file
- awk '{print $1,$3}' file
- cat, head, tail, wc
统计工具
- sort
- uniq
特殊设备文件
- /dev/null cat abc.txt > /dev/null
- 0, 1, 2 cat abc.txt 2> /dev/null
管道
- |
- linux 脚本编程的精髓
学这些有什么用?
- 问题:把一个文件里面所有包含 abc 的行里面的 abc 替换成 def,然后输出第一列和第三列
cat abc.txt | grep abc | sed 's/abc/def/g' | awk '{print $1,$3}' awk '$0 ~ /abc/ {gsub("abc", "def", $0); print $1, $3}' abc.txt perl -ne 's/abc/def/g; my @a=split(" "); print $a[0] . " " . $a[2] . "\n"' abc.txt
- 统计一个日志文件里面访问量最大的 2 个 ip
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 2 awk '{ips[$1]++} END{for(ip in ips) { print ip, ips[ip]} }' access.log | sort -k2nr | head -n2
- 产生一个文件,其中包含最近 30 天的日期
for i in $(seq 1 30); do d=$(date -d "$i days ago" +%Y-%m-%d); echo $d; done
环境变量
- 什么是环境变量
- login shell
系统资源
- ps, top, vmstat, iostat, iftop, iotop, sar
遇到问题的排查手段
- debug, verbose, log
- strace, gdb, lsof 8
- Source Code
作业
写一个脚本,读取一个包含里面一些文件名(包含全路径)的文件
- 把其中在磁盘上面存在的文件里面的 abc 替换成 def,保存在和文件相同路径下面,取名为 文件名.当前日期.fix (例如脚本执行的时候是 2014.2.14 那么文件名为 file1.20140214.fix)
- 把其中在磁盘上面不存在的文件的名字,输出到当前目录下面的 filenotfound.txt 这个文件里面