最近花了些时间学习Shell,视频学习过程中做了笔记,留给大家参考。
第一部分 Shell基础编程
第一部分 Shell基础编程——第一章 Shell简介
http://blog.csdn.net/wentasy/article/details/8702846
第一部分 Shell基础编程——第二章变量和运算符
http://blog.csdn.net/wentasy/article/details/8710837
第一部分 Shell基础编程——第三章 Shell输入和输出
http://blog.csdn.net/wentasy/article/details/8740922
第一部分 Shell基础编程——第四章控制流结构
http://blog.csdn.net/wentasy/article/details/8789222
第一部分 Shell基础编程——第五章文本过滤
http://blog.csdn.net/wentasy/article/details/8810755
第一部分 Shell基础编程——第六章 Shell函数
http://blog.csdn.net/wentasy/article/details/8810779
第一部分 Shell基础编程——第七章脚本参数传递
http://blog.csdn.net/wentasy/article/details/8816835
第二部分 Linux Shell高级编程技巧
第二部分 Linux Shell高级编程技巧——第一章深入讨论
http://blog.csdn.net/wentasy/article/details/8825727
第二部分 Linux Shell高级编程技巧——第二章 Shell工具
http://blog.csdn.net/wentasy/article/details/8825778
第二部分 Linux Shell高级编程技巧——第三章运行级别脚本介绍
http://blog.csdn.net/wentasy/article/details/8831883
第二部分 Linux Shell高级编程技巧——第四章几个脚本例子——终结篇
http://blog.csdn.net/wentasy/article/details/8851658
资源下载
Shell 编程笔记,点此下载。
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]
有时候我们需要将一个文件拷贝好几分。批量添加用户也是同样的原理的。
使用到的知识:
test 或 [ :可用来表示比较的
获取运行时传递的参数
while语句的格式
下面直接看代码吧。(由于使用了算数计算。我使用的是bash shell。在ubunut12.04中运行成功。)
#/bin/bash
argc=$#
if [ $argc -lt 2 ]
then
echo "请输入两个参数,依次为要拷贝的文件和次数"
exit 0
fi
file=$1
count=$2
left=${file%.*} #取出文件的名字。如a.tar.gz 的结果为a.tar
right=${file##*.} #得到文件的后缀。如a.tar.gz 的结果为gz
i=0
if [ -f "$file" ] #判断文件是否存在。
then
while [ $i -le $count ]
do
cp $file "$left$i.$right" #"$left$i.$right"组合出新的文件名字
let "i=$i + 1" #对i的值加一
done
else
echo "$file is not exist\n"
fiblog:http://blog.csdn.net/rentiansheng/article/details/8851684
#几个脚本例子
#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博客]