shell编程
示例1
ex1 文件内容如下:
#!/bin/sh
#This is to show what a example looks like.
echo "My First Shell!"
echo "This is current directory."
/bin/pwd
echo
echo "This is files."
/bin/ls
使用sh example 运行当前shell 文件 (example:文件名)
注意:
Shell 脚本以.sh 结尾
运行后结果:

ex1 文件内容详解:
#!/bin/sh shell 脚本文件开头内容固定格式
#This is to show what a example looks like. #是注释类似于Java中的// 或 /**/
echo "My First Shell!" echo 是输出类似于Java中的System.out.println("My First Shell!");
shell 最主要的作用是执行一些命令
/bin/pwd shell中要是用绝对路径
echo 输出空行相当于Java中空的System.out.println
echo "This is files." 输出语句
/bin/ls 列出当前文件所在文件目录的文件列表
shell应用实例
如果我们想使用Shell脚钵,指定个计划任务,比如每周的周一到周五给管理员发一个信息(比如当前主机的信息,如内存使用情况,在线人数,磁盘空间等)
ex2文件内容:
#!/bin/sh
/bin/date +%F >> /test/shelldir/ex2.info
echo "disk info:" >> /test/shelldir/ex2.info
/bin/df -h >> /test/shelldir/ex2.info
echo >> /test/shelldir/ex2.info
echo "online users:" >> /test/shelldir/ex2.info
/usr/bin/who | /bin/grep -v root >> /test/shelldir/ex2.info
echo "memory info:" >> /test/shelldir/ex2.info
/usr/bin/free -m >> /test/shelldir/ex2.info
echo >> /test/shelldir/ex2.info
#write root
/usr/bin/write root < /test/shelldir/ex2.info && /bin/rm /test/shelldir/ex2.info
#crontab -e
#0 9 * * 1-5 /bin /sh /test/ex2.sh
ex2文件内容详解:
#!/bin/sh 固定格式
/bin/date +%F >> /test/shelldir/ex2.info bin文件加下的data命令转换日期格式输入到
/test/shelldir/ex2.info 文件中,如果没有这个文件就会创建一个新的文件
+%F 转换日期格式

>> 追加输出(俩大于号追加输出 )
> 覆盖掉原有信息
echo "disk info:" >> /test/shelldir/ex2.info 追加disk info:(输出磁盘信息)到/test/shelldir/ex2.info文件中
/bin/df -h >> /test/shelldir/ex2.info 把磁盘信息追加到 /test/shelldir/ex2.info文件中
输入/bin/df -h 显示:
系统文件、大小、已用、剩余、使用百分比、目录 等信息

echo >> /test/shelldir/ex2.info 把一个空行追加到 /test/shelldir/ex2.info中
echo "online users:" >> /test/shelldir/ex2.info 把online users:追加输入到/test/shelldir/ex2.info中
/usr/bin/who | /bin/grep -v root >> /test/shelldir/ex2.info
把当前登录人的信息输出作为/bin/grep的输入,排除root用户追加输入到/test/shelldir/ex2.info中
/usr/bin/who 当前登录人
-v:排除
使用的命令一般在/bin 下 和 /usr/bin 下
/usr/bin 一般使用root登录时使用


echo "memory info:" >> /test/shelldir/ex2.info 把memory info:追加输入到 /test/shelldir/ex2.info中
/usr/bin/free -m >> /test/shelldir/ex2.info 把内存信息追加输入到 /test/shelldir/ex2.info中
/usr/bin/free -m 显示内存信息
echo >> /test/shelldir/ex2.info 追加输入到/test/shelldir/ex2.info中
#write root
/usr/bin/write root < /test/shelldir/ex2.info && /bin/rm /test/shelldir/ex2.info
把/test/shelldir/ex2.info文件中的信息写给管理员root用户 并且写成功之后 删除 /test/shelldir/ex2.info文件
< 写给
&& 如果成功的话 && 的前边后边都执行如果失败则不执行
rm 删除命令
#crontab -e 定时执行命令
#0 9 * * 1-5 /bin /sh /test/shelldir/ex2.sh 每周一到周五执行 ex2.sh 脚本
0 9 * * 1-5 格式:分时天月星期
如果注释掉 && /bin/rm /test/shelldir/ex2.info后执行 sh ex2.sh结果如下

将返回信息写给了当前本机root用户
如果将&& /bin/rm /test/shelldir/ex2.info 注释放开执行之后,就将 /test/shelldir/ex2.info文件删除了

Linux替换命令:
%s/old/new/g
如果替换的内容中有 / 需要 %s+/test/shelldir/ex2.info+/test/shelldir/ex2.info/g 用+表示分隔
shell 编程的基础知识(变量、命令、运算)
shell 的变量
变量是shell传递数据的一种方法,用来代表每个取值的符号名
Shell有两类变量:临时变量和永久变量。
临时变量是shell程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。包括。用户自定义变量、位置变量。
永久变量是环境交量,其值不随shell脚本的执行结束而消失.
关于临时变量和永久变量的理解:
临时变量:相当于在shell脚本中写的变量,这个变量只在脚本内部用
永久变量: 在整个linux系统中都可以用相当于全局变量,类似于Java环境变量中的path
永久变量:

解析:
echo $PATH 输出$PATH 输出当前计算机Linux的环境变量目录
echo $LANG 输出当前计算机Linux的字符集
变量的自定义:
用户定义的变量由字母或下划线开头,由字母、数字或下划线序列组成,并且大小写字母意义不同。变量名长度没有限制。在使用变量值时,要在变量名前加上前缀“$”。一般变量使用大写字母表示(小写也可以),并且是英文字母开头,赋值号“=”两边应没有空格,如NUM=5、STR=""A String"
可以将一个命令的执行结果赋值给变量:但是需要使用命令替换符号。
注意单引号和双引号的区别,“”号是会把里面的变量值进行输出,‘’是会把内容原封不动输出,不会识别里面的变量。
使用set命令查看所有的变量(当前系统中所有的变量)。
使用unset命令删除指定的变量。
命令替换符号:如图所示键盘按钮在英文状态下的``
自定义变量:

使用set命令:获取当前系统下所有变量

使用unset命令:
![]()

P1 = 10 设置自定义变量P1等于10
echo $P1 输出变量P1
P1=500 修改变量值 P1为500
cho $P1 输出变量P1
P1=”A string" 修改变量值 P1为A string
set l grep P1 取出全部变量并过滤P1
P1=`date` 把data的执行结果原封不用的赋值给P1
echo $P1 输出P1变量
P2=$P1 把P1的值赋值给P2
echo $P2 输出P2
P2=”time is $P1” 把P2的值设置成time is $P1
echo $P2 输出P2
在Shell里面还有俩种特殊的变量,一种是位置变量, 还有种是特殊的变量,在我们编
写Shell的时候十分的常用,一定要熟悉他们。
位置变量: Is -l file1 file2 fle3...(n范围=149) 在代码里使用$0-9进行替代
Is -l 打印当前目录的所有信息
file1 file2 file3... 都是占位的参数,最多只能到file9
es3文件内容::
#!/bin/sh
DATE=`/bin/date +%Y%m%d`
echo "TODAY IS $DATE"
/bin/ls -l $1
/bin/ls -l $2
/bin/ls -l $3
es3文件详解
#!/bin/sh
DATE=`/bin/date +%Y%m%d` 声明变量DATE `` 是命令替换符 把命令/bin/date +%Y%m%d的执行结果赋值给DATA
echo "TODAY IS $DATE" 输出 TODAY IS $DATE $DATE 是变量
/bin/ls -l $1
/bin/ls -l $2
/bin/ls -l $3
$1、 $2、 $3:占位符在执行脚本的时候需要将占位符的位置进行补全
注意:$0是shell执行文件本身,$1到$9是可以传递的参数最多9个
/bin/ls -l 绝对路径下的文件列表

ls -l 相对路径下的文件列表

查看test目录下的文件信息

es3文件执行结果:

特殊变量:
$*这个程序的所有参数(当前执行的.sh文件所有参数)
$#这个程序的参数个数(当前执行的.sh文件所有参数个数)
$$这个程序的PID
$!执行上一个后台命令的PID
$?执行上一个命令的返回值
$(0-9)显示位置变量
es4文件内容:
#!/bin/sh
DATE=`/bin/date +%F`
echo "today is $DATE"
echo '$# :' $#
echo '$* :' $*
echo '$? :' $?
echo '$$ :' $$
echo '$0 :' $0
单引号包裹的内容原样输出
es4文件执行结果:

键盘录入:
命令: read从键盘读入数据,赋给变量。(类似Java中的scanner )
es5文件内容
#!/bin/sh
read f s t
echo "the first is $f"
echo "the second is $s"
echo "the third is $t"
es5内容详解
#!/bin/sh
read f s t f s t 三个变量 外部脚本指定的时候要传递这三个参数然后输出打印
echo "the first is $f"
echo "the second is $s"
echo "the third is $t"
在输入的时候
sh ex5.sh
10 20 30
执行结果:

在执行的时候可以加-x选项表示跟踪,用于脚本调试
在输入的时候
sh -x ex5.sh
之后出现
+ read f s t ( + 表示当前脚本执行到哪了,然后输入 30 50 80 )
30 50 80
执行结果

Shell运算
expr命令,对整数进行运算(只能计算整数)。
注意点:
1 expr的运算必须用空格间隔开。
2*表示转义字符。
3保持先算乘除后算加减,如果需要优先运算则需要加命令替换符。

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

[root@bhz004 test] # NUM=30 //声明一个变量等于30
[root@bhz004 test]# echc `expr $NUM + 8 ` 输出这个变量+8
expr 不能计算小数

使用test命令可以对文件、字符串等进行测试,一般配合控制语句使用,不应该单独使用。如下:
字符串测试:
test str1=str2测试字符串是否相等
test str1!=str2测试字符串是否不相等
test str1测试字符串是否不为空
test -n str1测试字符串是否不为空
test -z str1测试字符串是否为空
int测试:
test int1-eq int2测试整数是否相等
test int1 -ne int2测试整数是否不相等
test int1-ge int2测试int1是否>=int2
test int1-gt int2 测试int1是否>int2
test int1 -le int2 测试int1是否< =int2
test int1 -lt int2测试int1是否<int2
文件测试:
test -d file指定文件是否目录
test f file 指定文件是否常规文件
test -x file指定文件是否可执行
test -r file指定文件是否可读
test -w file指定文件是否可写
test -a file指定文件是否存在
test s file文件的大小是否非0
test 命令都是在shell脚本里面配合if else if 执行的
If语法
语法格式: if test -d $1 then .…. else ... fi
变量测试语句可用[]进行简化,如test -d $1等价于[-d $1 ]
注意:["空格"-d $1"空格"]
es6 内容:
#!/bin/sh
# if test $1 then ... else ... fi
if [ -d $1 ]
then
echo "this is a directory!"
else
echo "this is not a directory!"
fi
es6 内容详解:
#!/bin/sh
# if test $1 then ... else ... fi
if [ -d $1 ] //等价于 if test -d $1 判断这个参数是否是一个文件夹
then
echo "this is a directory!" // 如果是文件夹输出
else
echo "this is not a directory!" //如果不是文件夹输出
fi
注意结尾处 if 的写法 极度恶心 fi
输出结果:
![]()
![]()
语法有错误会提示提示示例如下:

以下示例也是错的

这里执行之后会报错

注意空格,注意编码格式!!!!!!!!
es7内容:
#!/bin/sh
# if test then ... elif test then ... else ... fi
if [ -d $1 ]
then
echo "is a directory!"
elif [ -f $1 ]
then
echo "is a file!"
else
echo "error!"
fi
es7内容详解:
#!/bin/sh
# if test then ... elif test then ... else ... fi
if [ -d $1 ]
then
echo "is a directory!"
elif [ -f $1 ]
then
echo "is a file!"
else
echo "error!"
fi
如果是文件夹输出is a directory!如果是文件输出is a file!否则就是输出error
执行结果:

逻辑与逻辑或
es8内容:
#!/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
es8内容详解
#!/bin/sh
# -a -o
if [ $1 -eq $2 -a $1 = 1 ] 如果第一个参数和第二个参数相等并且第一个参数等于1
then
echo "param1 == param2 and param1 = 1" 输出param1 == param2 and param1 = 1
elif [ $1 -ne $2 -o $1 = 2 ] 如果第一个参数不等于第二个参数或者第一个参数等于2
then
echo "param1 != param2 or param1 = 2" 输出param1 != param2 or param1 = 2
else
echo "others" 否则输出others
fi
执行结果:

for循环:

es9内容
#!/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
es9内容详解:
#!/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循环:

es10 内容:
#!/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"
es10内容执行结果:

case语句

es11内容:
#!/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
es11内容详解:
#!/bin/sh
read op 从外部读取变量op
case $op in
a)
echo "you selected a";; 如果输入a输出you selected a
b)
echo "you selected b";;
c)
echo "you selected c";;
*)
echo "error"
esac 注意结尾
执行结果:

While语法

es12内容:
#!/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
es12内容详解:
#!/bin/sh
#while test do ... done
num=1
sum=0 声明两个变量num 和sun
while [ $num -le 100 ] 如果num 小于等于100
do
sum=`expr $sum + $num` 把 $sum + $num的运算结果赋值给sun
num=`expr $num + 1` $num + 1 的运算结果赋值给num
done
#sleep 5 等5s后输出
echo $sum 输出5050
执行结果:
![]()
continue 和 break 语法
es13 内容:
#!/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
es13 内容详解:
#!/bin/sh
i=0 声明白能量i=0
while [ $i -le 100 ] i小于等于100 执行循环体
do
i=`expr $i + 1` $i + 1 的值赋值给i
if [ $i -eq 5 -o $i -eq 10 ] 如果i 等于5 或 i 等于10
then continue; 跳过当前循环
else
echo "this number is $i" 否则输出当前i的值
fi
if [ $i -eq 15 ] 如果i等于15 退出while循环
then break;
fi
done
执行结果:

当前结果没有等于10 的页没有等于5的,等于15退出循环










