架构师养成记--37.简单shell编程

 

一、HelloWord.sh

echo 表示打印,可以在sh文件中写诸如pwd、ls 这样的命令,使用命令的时候尽量使用全路径。

#!/bin/sh
#this is my first sh
echo "this is my first sh"
/bin/pwd
echo
echo "this is files"
/bin/ls

 

 二、Shell应用实例

 如果我们想使用Shell脚步,指定个计划任务,比如像每周的周一到周五给管理员发一个消息(比如氮气主机的信息,如内存使用情况,在线人数,磁盘空间等)

#!/bin/sh
/bin/date +%F >> /test/ex2.info
echo "disk info:" >> /test/ex2.info
echo >> /test/ex2.info
/bin/df -h >> /test/ex2.info
echo >> /test/ex2.info
echo "online users:" >> /test/ex2.info
/usr/bin/who | /bin/grep -v root >> /test/ex2.info
echo "Memory info:" >> /test/ex2.info
/usr/bin/free -m >> /test/ex2.info
echo >> /test/ex2.info
#vim root
/usr/bin/write root < /test/ex2.info && /bin/rm /test/ex2.info
#crontab -e
#0 9 * * 1-5 /bin /sh /test/ex2.sh

 

三、变量

变量是shell传递数据的一种方法,用来代表每个取值的符号名。

shell有两类变量,临时变量和永久变量

临时变量是shell程序内部定义的,其使用范围仅限于定义它的程序,对其他程序不可见。包括:用户自定义变量、位置变量。永久变量是环境变量,其不随shell脚本的执行结果而消失。

永久变量

自定义变量

用户定义的变量由字母或者下划线开头,由字母、数字或者下划线序列组成,并且大小写字母意义不同。变量名长度没有限制。在使用变量值是,要在变量名前加上前缀“$” 。一般变量使用大写字母表示,并且全是英文字母开头,赋值符号“=”两边没有空格,如NUM=2,STRI="aaa"。可以将一个命令的执行结果赋值给变量,但是需要使用命令替换符号。注意单引号和双引号的区别,双引号是会把里面的变量值进行输出,单引号是会把内容原封不动输出,不会失败里面的变量。

使用set命令查看所有变量。

使用unset命令删除指定的变量。

 占位变量

在shell中还有两种特殊的变量,一种是位置变量,还有一种是特殊的变量,在编写shell脚本的时候常用,一定要熟悉。

位置变量:ls -l file1 file2 file3 ...(n范围=1~9)在代码中使用$0-9进行代替。如下建立 ex3.sh

#!/bin/sh
DATE=`/bin/date +%Y%m%d`
echo "TODAY IS $DATE"
/bin/ls -l $1
/bin/ls -l $2
/bin/ls -l $3

 

执行ex3.sh

 

特殊变量

$* 这个程序的所有参数

$# 这个程序的参数个数

$$ 这个程序的PID

$! 执行上一个后台命令的PID

$? 执行上一个命令的返回值

$(0-9) 显示位置变量

 

建立文件 ex4.sh

#!/bin/sh
DATE=`/bin/date +%F`
echo "today is $DATE"
echo '$# :' $#
echo '$* :' $*
echo '$? :' $?
echo '$$ :' $$
echo '$0 :' $0

 

 

四、read键盘录入

从键盘录入数据,赋值给变量

创建文件ex5.sh如下:

#!/bin/sh
read f s t  #读三个参数
echo "the first is $f" #打印第一个参数
echo "the second is $s"
echo "the third is $t"

 

执行文件ex5.sh

-x表示跟踪 可以看到每个执行结果前都会有 +执行语句,方便调试

没有-x的情况下执行如下

 

五、shell运算符

expr命令,对整数进行运算。

注意:1.expr的运算必须用空格隔开

   2. \*表示转义符

     3.先算乘除后算加减,如果要优先运算,则需要加命令替换符

     4.也可以对变量进行运算操作

     5.“ · ”不能相互嵌套

 

六、测试命令

使用test命令可以对文件、字符串等进行测试,一般配合控制语句使用,不应该单独使用。

字符串测试:

test str1=str2 测试字符串是否相等

test str1 测试字符串是否不为空

test -n str1 测试字符串是否不为空

test -z str1 测试字符串是否为空

int测试:

test int1 -eq int2 测试整数是否相等

test int1 -ne int2

test int1 -ge int2

test int1 -gt int2

test int2 -le int2

test int1 -lt int2

文件测试:

test -d file 知道文件是否是目录

test -f file 文件是否为常规文件

test -x file 文件是否是可执行文件

test -r file 文件是否可读

test -w file 文件是否可写

 

七、if语句

语法格式 if test -d $1 then ... elif then .... else ... fi

变量测试语句可以用简化格式:[],比如test -d $1 等价于 [ -d $1 ]  (注意空格)

#!/bin/sh
if [ -d $1 ]
then
    echo "a directory"
else
    echo "not a directory"
fi

else 使用 “ elif then ”

八、逻辑与和逻辑或

 

#!/bin/sh
# -a -o 
if [ $1 -eq $2 -a $1 = 1 ]
    then
    echo "param1 == param2 and param1 = 1"
elif [ $1 -ne $2 -o $1 = 2  ]
    then
    echo  "param1 != param2 or param1 = 2"
else
     echo "others"
fi

 

九、循环

for...done 语句格式

 

for 变量 in 名字表表

do

  命令列表

done

#!/bin/sh
# for var in [params] do ... done
for var in 1 2 3 4 5 6 7 8 9 10
do 
    echo "number is $var"
done

 

 

十.select

 格式:

    select 变量 in 列表

    do

      cmd...

    done

#!/bin/sh
# select var in [params] do ... done
select var in "java" "c++" "php" "linux" "python" "ruby" "c#" 
do 
    break
done
echo "you selected $var"

 

 

十一、case

#!/bin/sh
read op
case $op in
        a)
     echo "you selected a";;
        b)
    echo "you selected b";;
    c)
    echo "you selected c";;
    *)
    echo "error"
esac

 

 

十二、while循环

#!/bin/sh
#while test do ... done

num=1
sum=0
while [ $num -le 100 ]
do
    sum=`expr $sum + $num`
    num=`expr $num + 1`
done
#sleep 5
echo $sum

 

 

#!/bin/sh
i=0
while [ $i -le 100 ]
do
    i=`expr $i + 1`
    if [ $i -eq 5 -o $i -eq 10 ]
        then continue;
    else 
        echo "this number is $i"
    fi

    if [ $i -eq 15 ]
        then break;
        fi 
done

 

 

posted on 2017-03-16 09:42  司广孟  阅读(302)  评论(0编辑  收藏  举报

导航