实验1:自动备份源代码
把指定目录下所有C文件拷贝到/home/c_src目录,并把这些文件打包为src_日期.tgz,把这个源码打包文件copy到共享目录。
(MYD=`date +%Y_%m_%d`)
#!/bin/sh DIR=c_src DATE=`date +%Y_%d_%m` FILE=src_${DATE}.tar.gz if [ -z $1 ] then echo "argv1 error" exit 0 fi if [ -d $DIR ] then rm -rf $DIR
fi mkdir $DIR cp $1/*.c $DIR tar -czvf $FILE $DIR cp $FILE /root
实验2:自动解包分类统计文件
编写一个shell脚本,解压test.tar.gz目录.创建c_dir与h_dir目录,把解压好的test目录中所有的.c文件copy到c_dir/目录下,.h文件copy到h_dir/目录下,并且生成一个dirinfo.txt文件,其中包括全部.c和.h文件名,统计每个目录中文件总数.
#!/bin/sh clear echo "=================================" echo " my test shell programe " echo "=================================" testfile="test.tar.gz" cdir="c_files" hdir="h_files" csum=0 hsum=0
fileinfo() { echo " " echo "============file count=========" > fileinfo.txt date >> fileinfo.txt echo "====c file info====" >> fileinfo.txt ls $cdir/* >> fileinfo.txt echo "----c files count:${csum}" >> fileinfo.txt echo "====h file info====" >> fileinfo.txt ls $hdir/* >> fileinfo.txt echo "----h files count:${hsum}" >> fileinfo.txt cat fileinfo.txt echo " " } if [ -e "$testfile" ] then if [ -e test ] then echo "rm test dir" rm -rf test fi tar -xzf $testfile
if [ -e "$cdir" ] then echo "rm $cdir dir" rm -rf "$cdir" fi mkdir "$cdir" if [ -e "$hdir" ] then echo "rm $hdir dir" rm -rf "$hdir" fi mkdir "$hdir" if [ -e test ] then for xfile in test/* do case "${xfile##*.}" in c) cp $xfile $cdir csum=$(($csum+1)) ;; h) cp $xfile $hdir hsum=$(($hsum+1))
;; *);; esac done fileinfo else echo "test dir error" fi else echo "$testfile no found" fi echo "=================================" echo " test finsh "
echo "================================="
时间: 2024-10-16 00:39:17