5年前的时候,开始接触linux操作系统,接触的第一步就是学习shell脚本。用小脚本以连代学入了门。

1) 9*9乘法输出

2) 检验主机的服务是否启动

3) 冒泡排序

4) 备份当时team服务器上的dokuwili、mantis等应用。

 

一、 9*9乘法输出

主要目的是练习变量、for循环语句、if条件语句。

功能是输出9*9乘法

稚嫩的手法:

#!/bin/bash
#Function:output 9*9 expression
#Date:2011 04 21
#Version:1.0.0.0
LANG=C
export LANG i=1
j=1
rst=0 for i in $(seq 1 9)
do
result=''
for j in $(seq 1 $i)
do
rst=$(($i*$j))
result="$result$i*$j=$rst "
j=$(($j+1))
done
echo $result
i=$(($i+1))
done exit 0

效果

[root@Grace ~]# ./99Plus.sh

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36

7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49

8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64

9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

二、 检验主机的服务是否启动

主要目的是if条件语句、``。

功能是检验主机服务是否启动,并输出对应的信息

稚嫩的手法:

#!/bin/bash
#Function:using "if then" to show host's service.
#Date:2011 04 21
#Version:1.0.0.0
#1.print the program's function
echo "Now,the service of your linux system will be detected!"
echo "the www,ftp,ssh and send mail+pop3 will be detected!"
echo " "
#2.www
www=`netstat -an|grep LISTEN|grep :80`
if [ "$www" != "" ]; then
echo "WWW is running"
else
echo "WWW is NOT running"
fi #3.ftp
ftp=`netstat -an|grep LISTEN|grep :21`
if [ "$ftp" != "" ]; then
echo "FTP is running"
else
echo "FTP is NOT running"
fi #4.ssh
ssh=`netstat -an|grep LISTEN|grep :22`
if [ "$ssh" != "" ]; then
echo "SSH is running"
else
echo "SSH is NOT running"
fi
#5.smtp+pop3
smtp=`netstat -an|grep LISTEN|grep :25`
pop3=`netstat -an|grep LISTEN|grep :110`
if [ "$smtp" != "" ] && [ "$pop3" != "" ]; then
echo "sendmail is OK!"
elif [ "$smtp" != "" ] && [ "$pop3" == "" ]; then
echo "sendmail have some proplems of your pop3!"
elif [ "$smtp" == ""] && [ "$pop3" != "" ]; then
echo "sendmail have some proplems of your SMTP!"
else
echo "sendmail is not running!"
fi

效果

[root@Grace ~]# ./port-test.sh

Now,the service of your linux system will be detected!

the www,ftp,ssh and send mail+pop3 will be detected!

 
WWW is NOT running

FTP is NOT running

SSH is running

sendmail have some proplems of your pop3!

三、 冒泡排序

练习参数的使用、eval、if条件语句、比较符号、for循环语句。

功能是一次性输入n个数字,请按降序输出

拙劣的手法:

#Function:asc sort
#Date:2011 04 19
#Version:1.0.0.0
LANG=C
export LANG #1.read numbers
i=$#
var[1]=0
for (( a=1; a<=$i; a=a+1 ))
do
eval var[$a]=\$$a
done #2.sort
num=""
for (( a=1; a<=$i-1; a=a+1 ))
do
for (( j=1; j<=$i-$a; j=j+1 ))
do
if [ "${var[$j]}" -gt "${var[$j+1]}" ]; then
num="${var[$j]}"
var[$j]="${var[$j+1]}"
var[$j+1]="$num"
fi
done
done #3.output
result=""
for (( a=1;a<=$i-1; a=a+1 ))
do
result="$result${var[$a]},"
done
echo "$result${var[$i]}" exit 0

效果

[root@Grace ~]# ./asort.sh 8 100 9 2 0 119 19

0,2,8,9,19,100,119

[root@Grace ~]# ./asort.sh 3220 456 1000 9 0 2 10 20 98 166

0,2,9,10,20,98,456,1000,3220,32200

显而易见的缺点是:

没有对输入进行校验。

此外,获取值也可以使用read读取

四、 备份服务器的应用组件

主要目的是练习case语句、同时真的需要备份数据。

功能是

1) 备份目录 'data' 到 'qvbackp'

2) 加入简单的输入校验

3) 输出备份信息

4) 备份成功的话,同时把恢复脚本写好

拙劣的手法:

#!/bin/bash
#Function:backup directory 'data' to dest 'qvbackp'
#Date:2011 04 25
#Version:1.0.0.0 declare -i count=$#
# step1:backup and move backuped files to another directory
case $count in
"1")
if [ "$1" == "--help" ] ; then
echo "Usage: qvbackup.sh SOURCE-DIRECTORY DEST-DIRECTORY"
echo "Backup files of SOURCE-DIRECTORY to DEST-DIRECTORY."
exit
else
echo "Invalid parameters, make sure all parameters are correct. "
exit
fi
;;
"2")
if [ ! -d $1 ] ; then
echo "No such file or directory"
exit
elif [ ! -d $2 ] ; then
mkdir -p $2
fi srcdir=$(dirname $1)
srcname=$(basename $1)
destdir=$2
destname=$(basename $2)
destdir="${destdir%/}/"
# setp1.1:when today is the first day of the month,
# the programme will move the all the backuped file
# to another directory.
if [ $(date +%d) == "01" ] ; then
if [ $(whereis -b "$destname$(date +%d)") ] ; then
pushd $(dirname $destdir)
rm -rf "$destname$(date +%d)" 1>/dev/null 2>>"${destdir}qvbackup.log"
popd
fi
pushd $(dirname $destdir)
mv "$destname" "$destname$(date +%d)"
mkdir -p $2
popd
fi
# step1.2:backup files of SOURCE-DIRECTORY to
# DEST-DIRECTORY
backupfile="$(date +%Y-%m-%d-%H-%M-%S)_$srcname.tgz"
destfile="$destdir$backupfile"
pushd $srcdir
tar -g "${destdir}snapshot" -zcf $destfile $srcname 1>/dev/null 2>>"${destdir}qvbackup.log"
retCode=$?
popd ;;
"0")
echo "No parameters, please make sure."
exit
;;
*)
echo "Too more parameters, please make sure."
exit
;;
esac # setp2:output backup log ,backup information and recover scripts
rstMsg=""
pushd $destdir
if [ "$retCode" == "0" ] ; then
rstMsg="$(date +%Y-%m-%d-%H-%M-%S) backup OK"
echo "tar zxf $destfile">>qvrecover.sh
else
rstMsg="$(date +%Y-%m-%d-%H-%M-%S) backup ERROR"
fi
echo $rstMsg>>qvbackup.log
popd
echo $rstMsg
exit 0

问题是

1.一直备份,但磁盘占满了怎么办,需要自动化监测磁盘使用率。

2.因为组件比较小,使用的是完全备份,是不是可以保持最近一周的不删掉或者最近的五个、十个?

以练代学之shell入门(一)的更多相关文章

  1. 好记性不如烂笔头--linux学习笔记9练手写个shell脚本

    #!/bin/bash #auto make install httpd #by authors baker95935 #httpd define path variable H_FILES=http ...

  2. Linux shell入门基础(六)

    六.Shell脚本编程详解 将上述五部分的内容,串联起来,增加对Shell的了解 01.shell脚本 shell: # #perl #python #php #jsp 不同的脚本执行不同的文本,执行 ...

  3. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  4. Shell - 简明Shell入门

    本文以示例和注释的方式,对Shell编程的基本知识点进行了总结和回顾,所有脚本均已做了基本的调试和验证. Shell - 简明Shell入门 01 - 第一个脚本 脚本的定义.执行方法以及echo命令 ...

  5. linux shell 入门

    本文是本人学习linux shell入门收集整理,不完全原创. 参考博文: http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html ...

  6. 小代学Spring Boot之数据源

    想要获取更多文章可以访问我的博客 - 代码无止境. 经过一天对Spring Boot的研究,小代同学已经对Spring Boot框架有了一个大概的认识.并且还创建了一个简单的Spring Boot的W ...

  7. 小代学Spring Boot之集成MyBatis

    想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...

  8. 小代学Spring Boot之自定义Starter

    想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...

  9. shell 入门学习

    目录 shell 入门学习 注释 执行与启动 变量 语法 调试 title: shell 入门学习 date: 2019/7/16 15:47:49 toc: true --- shell 入门学习 ...

随机推荐

  1. 自动化运维:网站svn代码上线更新(flask+saltstack)

    阶段性总结:      跌跌撞撞的用了一周左右的时间做完了网站自动升级功能,中间遇到了很多的问题,也学到了很多,在此做一个总结.   1.整体架构: 后台:nginx+uwsgi  #nginx提供w ...

  2. [PHP] - Laravel - Route路由

    前言 这里使用的是Laravel 5 PHP Laravel的路由比较强悍,但也正因如此,不统一而容易凌乱.比如在路由中可以直接写方法操作(破坏封装啊) 以下是个人学习的例子,不供参考 路由中的直接方 ...

  3. sap 取货币之间汇率

    FORM FRM_GET_KURSK USING PV_KURST "M PV_FCURR PV_TCURR PV_PRSDT CHANGING PV_KURSK. DATA: LV_RAT ...

  4. MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存

    二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...

  5. HTML5精美网站模板分享

    1. http://newweb.top/ 2. http://newweb.top/Templates/agency-gh-pages/index.html

  6. 使用7-zip制作自解压安装包

    7-zip制作自解压包很方便,只要在压缩时选择”创建自释放程序”选项. 而自解压安装包有点麻烦,不如WinRAR方便. 准备工具:下载 LZMA SDK 这里面有 7zSD.sfx (16.04版7z ...

  7. Visual Studio 14 初试,vNext

    下了几天的VS 2014 .终于安装上了,花了好几天时间, VS 2014  下载地址, http://www.visualstudio.com/en-us/downloads/visual-stud ...

  8. loadrunner中web_reg_find使用使用方法

    Java语法:int object.reg_find( String text, String[] argumentList ); (例子:略) C语法:int web_reg_find( const ...

  9. H5的一些小问题

    [每日壹闻]常用HTML代码解释-------六.歌曲代码(1):在这组代码中,不必管它是mms.http.rtsp,只要看尾缀是asf.wma.wmv.wmv.rm都可适用下面的代码:1. 手动播放 ...

  10. iOS开发 - OC - 实现本地数据存储的几种方式二(直接使用sqlite)

    连接上一篇文章http://www.cnblogs.com/FBiOSBlog/p/5819418.html. 上一篇文章介绍了OC内部一些方法进行数据的本地存储,其中包括 NSUser类.Plist ...