第二部分 Linux Shell高级编程技巧——第四章 几个脚本例子——终结篇
2013-04-25 20:48
751 查看
笔记
#几个脚本例子 #kill_process.sh #编辑 [root@localhost 0421]# vi kill_processes.sh #查看内容 [root@localhost 0421]# cat kill_processes.sh #!/bin/bash #kill_process.sh current_PID=$$ #获得特定进程的进程号并重定向到一个临时文件中 ps -aux|grep "/usr/sbin/httpd"|grep -v "grep"|awk '{print $2}'>/tmp/${current_PID}.txt #命令块开始 for pid in `cat /tmp/${current_PID}.txt` do { echo "Kill -9 $pid" kill -9 $pid } done #命令块结束 #删除临时文件 #echo "rm -f /tmp/${current_PID}.txt" rm -f /tmp/${current_PID}.txt #cpdir.sh [root@localhost 0421]# vi cpdir.sh [root@localhost shell]# cat cpdir.sh #!/bin/bash #cpdir.sh #此脚本用于将源目录下的子目录全部复制到目的目录中,不复制源目录中的文件。确保目的目录中的子目录是空目录 #脚本用法函数 usage() { echo "cpdir.sh 源目录 目的目录" } #判断是否为两个参数,否则提示脚本用法 if [ $# -ne 2 ] then { usage exit 0 } fi srcdir=$1 desdir=$2 #判断源目录${srcdir}是否为目录,否则提示错误信息和用法 if [ ! -d $srcdir ] then { usage echo "错误:源目录${srcdir}不是目录" exit } fi #判断目的目录${descdir}是否为目录,否则提示错误信息和用法 if [ ! -d $desdir ] then { usage echo "错误:目的目录${desdir}不是目录" } fi processid=$$; #查找源目录下所有的子目录,输出并保存到/tmp/srcdir_进程号.txt文件中 echo "源目录下${srcdir}所有的子目录" echo "-----------------------------" find $srcdir/* -type d|/usr/bin/tee /tmp/srcdir_tmp_${processid}.txt sed "s/^${srcdir}/${desdir}/g" /tmp/srcdir_tmp_${processid}.txt >/tmp/srcdir_${processid}.txt #在目的目录下建立空子目录 rm -rf ${desdir}/* for subdir in `cat /tmp/srcdir_${processid}.txt` do { mkdir ${subdir} } done echo "" echo "目标目录下${desdir}所有的子目录" echo "------------------------------" find $desdir/* -type d|/usr/bin/tee /tmp/desdir_${processid}.txt #比较在目的目录下建立空子目录后的差异 echo "" echo "比较目的目录和源目录的差异" echo "-------------------------" diff /tmp/desdir_${processid}.txt /tmp/srcdir_${processid}.txt rm -f /tmp/srcdir_${processid}.txt rm -f /tmp/desdir_${processid}.txt rm -f /tmp/srcdir_tmp_${processid}.txt [root@localhost shell]# ls -al test/ total 20 drwxr-xr-x 4 root root 4096 Apr 21 17:33 . drwxr-xr-x 17 root root 4096 Apr 21 17:33 .. drwxr-xr-x 2 root root 4096 Apr 21 17:33 aa drwxr-xr-x 2 root root 4096 Apr 21 17:33 cc -rwxr-xr-x 1 root root 391 Apr 21 17:32 test.sh [root@localhost shell]# ls -al test/aa/bb/ total 8 drwxr-xr-x 2 root root 4096 Apr 21 17:34 . drwxr-xr-x 3 root root 4096 Apr 21 17:34 .. [root@localhost shell]# find test/* -type d test/aa test/aa/bb test/cc test/cc/ss [root@localhost shell]# find test1/* -type d find: test1/*: No such file or directory [root@localhost shell]# ./cpdir.sh test test1 cpdir.sh 源目录 目的目录 错误:目的目录test1不是目录 源目录下test所有的子目录 ----------------------------- ./cpdir.sh: line 43: usr/bin/tee: No such file or directory ./cpdir.sh: line 44: tmp/srcdir_5075.txt: No such file or directory cat: /tmp/srcdir_5075.txt: No such file or directory 目标目录下test1所有的子目录 ------------------------------ find: test1/*: No such file or directory ./cpdir.sh: line 57: usr/bin/tee: No such file or directory 比较目的目录和源目录的差异 ------------------------- diff: /tmp/desdir_5075.txt: No such file or directory diff: /tmp/srcdir_.txt: No such file or directory [root@localhost shell]# ./cpdir.sh test test1 源目录下test所有的子目录 ----------------------------- test/aa test/aa/bb test/cc test/cc/ss 目标目录下test1所有的子目录 ------------------------------ test1/aa test1/aa/bb test1/cc test1/cc/ss 比较目的目录和源目录的差异 ------------------------- [root@localhost shell]# cd test test test1 [root@localhost shell]# cd test [root@localhost shell]# cd test/cc/ss/ [root@localhost test1]# cd .. [root@localhost shell]# cd test [root@localhost test]# ls aa cc test.sh [root@localhost test]# cd ../test1 [root@localhost test1]# ls aa cc [root@localhost test1]# cd cc [root@localhost cc]# ls ss [root@localhost cc]# ls -al total 12 drwxr-xr-x 3 root root 4096 Apr 21 17:48 . drwxr-xr-x 4 root root 4096 Apr 21 17:48 .. drwxr-xr-x 2 root root 4096 Apr 21 17:48 ss #menu.sh #创建文件夹 mkdir 0425 #进入 cd 0425 #拷贝 cp ../0421/cpdir.sh . #改名 mv cpdir.sh menu.sh #编辑 vi menu.sh #拷贝 cp awkif.sh ../0425 #改名 mv awkif.sh functions vi搜索内容:/要匹配的内容 #致歉,menu.sh内容过多,此处省略,若读者感兴趣可以到我的资源下载。
附图
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客] |
相关文章推荐
- 第二部分 Linux Shell高级编程技巧——第三章 运行级别脚本介绍
- 第二部分 Linux Shell高级编程技巧——第三章 运行级别脚本介绍
- 查看进程第二部分 Linux Shell高级编程技巧——第二章 Shell工具
- *Linux Shell 高级编程技巧4----几个常用的shell脚本例子
- 博客文件第二部分 Linux Shell高级编程技巧——第一章 深入讨论
- 第二部分 Linux Shell高级编程技巧——第二章 Shell工具
- 第二部分 Linux Shell高级编程技巧——第一章 深入讨论
- 第二部分 Linux Shell高级编程技巧——第一章 深入讨论
- 第二部分 Linux Shell高级编程技巧——第二章 Shell工具
- linux shell编程指南第二十七章------几个脚本例子
- 分享50个使你成为高级javascript开发者的jQuery的代码开发技巧 - 第二部分
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!
- Windows 游戏编程大师技巧第四章第9个例子
- Linux Shell高级编程技巧——第二章 Shell工具
- *Linux Shell 高级编程技巧1----深入讨论(awk、<<)
- 《Spring Security3》第四章第二部分翻译(JdbcDaoImpl的高级配置)
- Windows 游戏编程大师技巧第四章第一个例子
- 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法
- 第IV部分 高级编程技巧 第14章 使用VBA开发Excel实用程序