Shell编程进阶
Shell结构以及执行
[[email protected] ~]# mkdir shell
[[email protected] ~]# cd shell/
[[email protected] shell]# vim first.sh
#!/bin/bash
##The first test shell script
##written by wangchao
ls /tmp/
echo "This is the first script."
[[email protected] shell]# bash first.sh //执行脚本
[[email protected] shell]# chmod a+x first.sh
[[email protected] shell]# ./first.sh //执行脚本(相对路劲)
[[email protected] shell]# /root/shell/first.sh //执行脚本(相对路径)
[[email protected] shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 877480 Oct 16 2014 /bin/bash
[[email protected] shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Jun 8 19:15 /bin/sh -> bash
//两者为同一命令
[[email protected] shell]# sh first.sh //执行文件
[[email protected] shell]# sh -x first.sh //查看脚本执行过程
Date命令
[[email protected] shell]# date //显示日期,时间
Tue Jul 28 20:59:41 CST 2015
[[email protected] shell]# cal //显示日历
July 2015
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 1011
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
[[email protected] shell]# date -s"2015-7-28 19:26:35" //修改日期、时间
Tue Jul 28 19:26:35 CST 2015
[[email protected] shell]# yum install -y ntp
[[email protected] shell]# ntpdatetime.windows.com //与该时间服务器同步时间
28 Jul 19:33:03 ntpdate[6388]: step timeserver 23.99.222.162 offset -5.054457 sec
[[email protected] shell]# date +%F //打印日期
2015-07-28
[[email protected] shell]# date +%T //打印时间
19:35:05
[[email protected] shell]# date +%Y //打印年
2015
[[email protected] shell]# date +%y //打印年的后两位
15
[[email protected] shell]# date +%m //打印月
07
[[email protected] shell]# date +%d //日期
28
[[email protected] shell]# date +%H //时
19
[[email protected] shell]# date +%M //分
37
[[email protected] shell]# date +%S //秒
56
[[email protected] shell]# date +%s
1438083494
//时间簇(距1970年1月1日0点0分0秒走了多少秒了)
[[email protected] shell]# date +"%Y-%m-%d%H:%M:%S" //显示日期,时间
2015-07-28 19:40:58
[[email protected] shell]# date -d "-2day" +%F //调整时间2天前
2015-07-26
[[email protected] shell]# date -d "-2month" +%F //调整时间两个月前
2015-05-28
[[email protected] shell]# date -d "-2hour" +%T //2小时前
17:44:21
[[email protected] shell]# date -d "-2min" +%T //2分钟前
19:42:28
[[email protected] shell]# date -d "-2sec" +%T //2秒前
19:44:33
[[email protected] shell]# date +%w //显示今天周几
2
[[email protected] shell]# date +%W //今年第几周
30
Shell自定义变量
[[email protected] shell]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/apache2/bin:/root/bin
[[email protected] shell]# echo $HOME
/root
[[email protected] shell]# echo $PWD
/root/shell
[[email protected] shell]# a=1
[[email protected] shell]# echo $a
1
[[email protected] shell]# vim 2.sh
#!/bin/bash
##
read -p "please input a number:" number
echo $number
[[email protected] shell]# sh 2.sh
please input a number:4
4
[[email protected] shell]# vim 2.sh
#!/bin/bash
##
read -t 3 -p "please input a number:" number //加-t 3参数3秒内无输出,退出
echo $number
[[email protected] shell]# vim 3.sh
#!/bin/bash
##
echo $1 $2 $0 $3
[[email protected] shell]# sh 3.sh //只输出3.sh
3.sh
[[email protected] shell]# vim 3.sh
#!/bin/bash
##
echo "\$1=$1"
echo "\$2=$2"
echo "\$0=$0"
echo "\$3=$3"
[[email protected] shell]# sh 3.sh
$1=
$2=
$0=3.sh
$3=
[[email protected] shell]# sh 3.sh aa 11 dd
$1=aa
$2=11
$0=3.sh
$3=dd
[[email protected] shell]# a=1;b=2
[[email protected] shell]# c=$a+$b
[[email protected] shell]# echo $c
1+2
[[email protected] shell]# c=$[$a+$b]
[[email protected] shell]# echo $c
3
If逻辑判断
[[email protected] shell]# vim if.sh
#!/bin/bash
##
a=5
if [ $a -gt 3 ]
then
echo "a>3"
fi
[[email protected] shell]# sh if.sh
[[email protected] shell]# sh if.sh
a>3
[[email protected] shell]# sh -x if.sh //查看执行过程
+ a=5
+ ‘[‘ 5 -gt 3 ‘]‘
+ echo ‘a>3‘
a>3
[[email protected] shell]# vim if.sh
#!/bin/bash
##
a=5
if [ $a -gt 10]
then
echo "a>10"
else
echo "a<=10"
fi
[[email protected] shell]# sh -x if.sh
+ a=5
+ ‘[‘ 5 -gt ‘10]‘
if.sh: line 4: [: missing `]‘
+ echo ‘a<=10‘
a<=10
[[email protected] shell]# vim if.sh
#!/bin/bash
##
a=5
if [ $a -gt 10]
then
echo "a>10"
elif [$a -lt 4]
then
echo "a<4"
else
echo "4<a<10"
fi
[[email protected] shell]# sh -x if.sh
+ a=5
+ ‘[‘ 5 -gt ‘10]‘
if.sh: line 4: [: missing `]‘
+ ‘[5‘ -lt ‘4]‘
if.sh: line 7: [5: command not found
+ echo ‘4<a<10‘
4<a<10
-gt : > 、 -lt:< 、 -eq :== 、 -ne:!= 、 -ge :>= 、-le:<=
If判断的几种用法
[[email protected] shell]# if [ -f 1.txt ];thenecho ok;fi //如果存在1.txt,则显示OK
[[email protected] shell]# touch 1.txt
[[email protected] shell]# if [ -f 1.txt ];thenecho ok;fi
ok
[[email protected] shell]# if [ -d 1.txt ];thenecho ok;fi //判断目录是否存在
[[email protected] shell]# if [ -r 1.txt ];thenecho ok;fi //判断1.txt是否为可读文件
ok
[[email protected] shell]# if [ -w 1.txt ];thenecho ok;fi //是否可写
ok
[[email protected] shell]# if [ -x 1.txt ];thenecho ok;fi //是否可执行
[[email protected] shell]# vim if2.sh
#!/bin/bash
read -p "please input a number:"n
m=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$m" ]
then
echo "The character you input is not a number,please retry."
else
echo $n
fi
please input a number:3
3
[[email protected] shell]# sh if2.sh
please input a number:r
The character you input is not anumber,please retry.
//-n判断变量是否不为空,-z可判断是否为空
[[email protected] shell]# sh -x if2.sh //查看执行过程
+ read -p ‘please input a number:‘ n
please input a number:w
++ sed ‘s/[0-9]//g‘
++ echo w
+ m=w
+ ‘[‘ -n w ‘]‘
+ echo ‘The character you input is not anumber,please retry.‘
The character you input is not anumber,please retry.
[[email protected] shell]# grep ‘^tom:‘/etc/passwd //找出tom的行
tom:x:500:500::/home/tom:/bin/bash
[[email protected] shell]# if grep -q ‘^tom:‘/etc/passwd;then echo "tom exist";fi
tom exist
//-q只做判断,不输出grep匹配的结果
[[email protected] shell]# if [ -d /tmp/]&&[ -f 1.txt ];then echo ok;fi
ok
// &&且,两者都存在
[[email protected] shell]# if [ -d /tmp/ ]||[-f 1.txt ];then echo ok;fi
Ok
// ||或
[[email protected] shell]# if [ -d /tmp/ -o -f1.txt ];then echo ok;fi
ok
// -oor 或者的意思
[[email protected] shell]# if [ -d /tmp/ -a -f1.txt ];then echo ok;fi
ok
//a all并且的意思
Case选择
[[email protected] shell]# cat /etc/init.d/atd //查看linux的一个启动脚本,看case使用例子
[[email protected] shell]# vim case.sh
#!/bin/bash
read -p "please input a number:"n
m=$[ $n%2 ]
case $m in
1)
echo "The number is jishu."
;;
0)
echo "The number is oushu."
;;
*)
echo "Tt is not jishu or oushu."
;;
esac
[[email protected] shell]# sh case.sh
please input a number:10
The number is oushu.
[[email protected] shell]# sh case.sh
please input a number:1
The number is jishu.
For循环
[[email protected] shell]# seq 1 10 //产生数字1-10
[[email protected] shell]# seq 1 2 10 //步长为2生成1-10
[[email protected] shell]# seq 10 -2 1 //倒叙生成10-1,步长为-2
[[email protected] shell]# seq 10 -1 1
[[email protected] shell]# seq -w 01 10 //产生的数 01、02、03方式
01
02
03
04
05
06
07
08
09
10
[[email protected] shell]# seq -w 01 100 //参数的数001、002、003方式 -w参数
[[email protected] shell]# vim for.sh //参数数字1-5
#!/bin/bash
for i in `seq 1 5`;
do
echo $i
done
[[email protected] shell]# sh for.sh
1
2
3
4
5
[[email protected] shell]# vim for.sh //求1-10和
#!/bin/bash
sum=0
for i in {1..10}
do
sum=$[ $sum+$i ]
done
echo $sum
[[email protected] shell]# sh for.sh
55
[[email protected] shell]# vim for.sh
#!/bin/bash
for l in `cat 1.txt`
do
echo $l
done
[[email protected] shell]# sh for.sh
aa
[[email protected] shell]# cat 1.txt
aa
[[email protected] shell]# vim 2.txt //显示每一行(其中空格认为分割符)
1 2 3
lssd
aaa bbb
[[email protected] shell]# for l in `cat2.txt`;do echo $l;done
1
2
3
lssd
aaa
bbb
[[email protected] shell]# vim 3.txt
1.1.1.1
2.2.2.2
3.3.3.3
[[email protected] shell]# for file in `ls` ;do echo $file;done //循环打印文件名
[[email protected] shell]# for file in `ls` ;do echo $file;du -sh $file;done
//循环打印文件名,并查看文件大小
While循环
[[email protected] ~]# vim while.sh
#!/bin/bash
while :
do
date +%T
sleep 3
done
//每隔三秒输出时间
[[email protected] ~]# sh while.sh
21:24:34
21:24:37
21:24:40
[[email protected] ~]# vim while.sh //生成数字1-10
#!/bin/bash
n=0
while [ $n -le 10 ]
do
echo $n
n=$[n+1]
done
[[email protected] ~]# sh while.sh
0
1
2
3
4
5
6
7
8
9
10
[[email protected] ~]# vim while.sh
#!/bin/bash
n=1
while [ ! -z "$n" ]
do
read -p "please input a number:" m
n= `echo $m|sed ‘s/[0-9]//g‘`
echo $m
done
[[email protected] ~]# sh while.sh
please input a number:q
while.sh: line 6: q: command not found
q
please input a number:d
while.sh: line 6: d: command not found
d
please input a number:c
while.sh: line 6: c: command not found
c
please input a number:3
3
[[email protected] ~]#
//输入数字,则显示退出,非数字,显示并继续输入
Shell中断继续退出
break 、 continue 、exit
[[email protected] ~]# vim for2.sh
#!/bin/bash
##
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
break //退出本次循环
fi
echo $i
done
[[email protected] ~]# sh for2.sh
1
1
2
2
3
3
4
[[email protected] ~]# sh -x for2.sh
[[email protected] ~]# vim for2.sh
#!/bin/bash
##
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
break
fi
echo $i
done
echo "for done"
[[email protected] ~]# sh for2.sh
1
1
2
2
3
3
4
for done
[[email protected] ~]# vim for2.sh
#!/bin/bash
##
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
continue //退出本次循环
fi
echo $i
done
echo "for done"
[[email protected] ~]# sh for2.sh
1
1
2
2
3
3
4
5
5
6
6
7
7
8
8
9
9
10
10
for done
[[email protected] ~]# vim for2.sh
#!/bin/bash
##
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
exit //直接退出,退出整个shell
fi
echo $i
done
echo "for done"
[[email protected] ~]# sh for2.sh
1
1
2
2
3
3
4
Break 结束整个循环体 continue结束本次循环 exit结束shell
Shell函数组
[[email protected] ~]# a=1
[[email protected] ~]# echo $a
1
[[email protected] ~]# a=(1 2 3 4)
[[email protected] ~]# echo $a
1
[[email protected] ~]# echo ${a[@]} //显示数组
1 2 3 4
[[email protected] ~]# echo ${a[*]}
1 2 3 4
[[email protected] ~]# echo ${a[0]}
1
[[email protected] ~]# echo ${a[1]}
2
[[email protected] ~]# echo ${a[2]}
3
[[email protected] ~]# echo ${a[3]}
4
[[email protected] ~]# a[4]=9
[[email protected] ~]# echo ${a[*]}
1 2 3 4 9
[[email protected] ~]# a[2]=7
[[email protected] ~]# echo ${a[*]}
1 2 7 4 9
[[email protected] ~]# echo ${#a[@]} //显示数组个数
5
[[email protected] ~]# for i in `seq 0 9`;doa[$i]=$RANDOM;done;echo ${a[@]}
30430 29357 27930 22531 11764 1293 1918427274 4774 21057
//产生10个随机数
[[email protected] ~]# for i in `seq 0 9`;doa[$i]=$RANDOM;done;echo ${a[@]}|sed ‘s/ /\n/g‘
13292
28753
27735
741
2638
16644
29293
3927
6481
16613
//产生10个随机数,一行一行显示出
[[email protected] ~]# for i in `seq 0 9`;doa[$i]=$RANDOM;done;echo ${a[@]}|sed ‘s/ /\n/g‘|sort -n
5375
5876
7387
16610
16981
21613
28498
30253
30815
32143
//产生10个随机数,一行一行显示出,并按大小排
[[email protected] ~]# unset a
[[email protected] ~]# echo ${a[*]}
[[email protected] ~]# for i in `seq 0 9`;doa[$i]=$RANDOM;done;echo ${a[@]}
25440 14378 23079 23134 3514 18340 155612812 13446 22478
[[email protected] ~]# unset a[4] //删除元素a[4]
[[email protected] ~]# echo ${a[@]}
25440 14378 23079 23134 18340 1556 1281213446 22478
[[email protected] ~]# echo ${a[@]:0:3} //从0开始取3个数
25440 14378 23079
[[email protected] ~]# echo ${a[@]:4:4} //显示从第四个开始的后4个数
18340 1556 12812 13446