首页 > 代码库 > 以练代学之shell入门(一)

以练代学之shell入门(一)

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 hosts service.
#Date:2011 04 21
#Version:1.0.0.0
#1.print the programs 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入门(一)