shell scripts的简单使用一

概念

  scripts 最基础的功能就是汇整一些在 command line 下达的连续指令,将他写入 scripts 当中,而由直接执行 scripts 来启动一连串的 command line 指令输出/输入
基本上,一个 script 被执行的时候, bash 会据以判断执行的步骤为:
1、如果读取到一个 Enter 符号( CR ),就尝试开始执行该行命令;
2、如同前面 bash command 提到的,指令间的多个空白会被忽略掉;
3、而空白行也将被忽略掉!,并且 tab 也是不会被理会的!
4、至于如果一行的内容太多,则可以使用 \ 来延伸至下一行;
5、使用最多的 # 可做为批注!任何加在 # 后面的字,将全部被视为批注文字而被忽略!


  在撰写一个 scripts 的时候,最好养成良好的习惯:
1、先宣告使用的 shell 为何?(特别留意这一点,在某些状况中,例如 /etc/crontab 情况下,如果没有宣告使用的 shell ,常常会出现错误讯息而导致 scripts 无法被执行)
2、注明该 script 的内容功能、版本信息、作者、建文件日期等等
3、每一个大步骤的主要功能(也顺便提供自己未来修改之用!)

script的两种执行的方法 
1、一个是将该档案改成可以执行的属性,如chmod 755 scripts.file ,然后执行该档案;
2、另一种则是直接以 sh 这个执行档来执行 script 的内容,如 sh scripts.file

例1、输出Hello Word!的script
第一步建立hello.sh文件
vi hello.sh
内容如下

#!/bin/bash
#该脚本用于输出Hello Word!
#建立日期2017-02-10
#创建者:csk
hello=Hello\ Word\ !
echo $hello

第二步执行hello.sh
sh hello.sh
第三步显示结果
Hello Word !

declare 指令
declare用于定义变量,宣告变量内容,否则将默认做字符串形态
declare [-afirx]
参数说明:
-a  :定义为数组 array
-f  :定义为函数 function  
-i  :定义为整数 integer
-r  :定义为『只读』
-x  :定义为透过环境输出变量

例2、输出四则运算结果 2*3+4/2-1

第一步建立declare.sh文件
vi declare.sh
内容如下

#!/bin/bash
#该脚本用于输出四则运算结果
#建立日期2017-02-10
#创建者:csk
num1=2*3+4/2-1
declare -i num2=2*3+4/2-1
echo "num1 result is ==> $num1"
echo "num2 result is ==> $num2"

第二步执行declare.sh
sh declare.sh
第三步结果输出
num1 result is ==> 2*3+4/2-1
num2 result is ==> 7

条件式判断:if...then...fi, case.....esac
if...then...fi条件判断的语法为:
 

if [ 条件判断一 ] && (||) [ 条件判断二 ]; then       <== if 是起始的意思,后面可以接若干个判断式,使用 && 或 ||
    执行内容程序
elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then     <==第二段的判断,如果第一段没有符合就来此搜寻条件
    执行第二段内容程序
else                                                 <==当前两段都不符合时,就以这段内容来执行!
    执行第三段内容程序
fi                                                   <==结束 if then 的条件判断!

注*
1、在 [ ] 当中,只能有一个判别式;
2、在 [ ] 与 [ ] 当中,可以使用 && 或 || 来组织判别式;
3、每一个独立的组件之间『都需要有空格键来隔开』

例3 条件判断的运用
第一步建立declare.sh文件
vi declare.sh
内容如下

#!/bin/bash
#该脚本用于条件判断
#建立日期2017-02-10
#创建者:csk
echo "Press y to continue"
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ];then
    echo "script is runing...."
else
    echo "STOP!"
fi

例4、侦测主机上面的 port 是否有开启

#!/bin/bash
#该脚本用于侦测主机上面的 port 是否有开启
#建立日期2017-02-10
#创建者:csk
# content: I will using this program to show your services
# 1. print the program's work in your screen
  echo "Now, the services of your Linux system will be detect!"
  echo "The www, ftp, ssh, and sendmail + pop3 will be detect!"
  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. sendmail + 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 problem of your pop3"
  elif [ "$smtp"  = "" ] && [ "$pop3" != "" ]; then
      echo "sendmail have some problem of your smtp"
  else
      echo "sendmail is NOT running"
  fi

case.....esac 条件判断的语法为:

case 种类方式(string) in          <==开始阶段,那个种类方式可分成两种类型,通常使用 $1 这一种直接下达类型!
    种类方式一)
       程序执行段
       ;;                     <==种类方式一的结束符号!
    种类方式二)
       程序执行段
       ;;
    *)
       echo "Usage: {种类方式一|种类方式二}"     <==列出可以利用的参数值!
       exit 1
esac                         <==这个 case 的设定结束处!

例5、

#!/bin/bash
#该脚本用于条件判断
#建立日期2017-02-10
#创建者:csk
echo "Press you select one,two,three"
read number
case $number in
    one)
        echo "your choice is one"
        ;;
    two)
        echo "your choice is two"
        ;;
    three)
        echo "your choice is three"
        ;;
    *)
        echo "Usage {one|two|three}"
        exit 1
esac

 

posted @ 2017-02-10 17:57  沉舟侧畔  阅读(1360)  评论(0编辑  收藏  举报