您的位置:首页 > 运维架构 > Shell

shell编程进阶

2015-07-29 21:16 609 查看
Shell编程进阶

Shell结构以及执行
[root@wangchao ~]# mkdir shell
[root@wangchao ~]# cd shell/
[root@wangchao shell]# vim first.sh
#!/bin/bash
##The first test shell script
##written by wangchao
ls /tmp/
echo "This is the first script."

[root@wangchao shell]# bash first.sh //执行脚本

[root@wangchao shell]# chmod a+x first.sh
[root@wangchao shell]# ./first.sh //执行脚本(相对路劲)
[root@wangchao shell]# /root/shell/first.sh //执行脚本(相对路径)

[root@wangchao shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 877480 Oct 16 2014 /bin/bash
[root@wangchao shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Jun 8 19:15 /bin/sh -> bash
//两者为同一命令

[root@wangchao shell]# sh first.sh //执行文件
[root@wangchao shell]# sh -x first.sh //查看脚本执行过程

Date命令
[root@wangchao shell]# date //显示日期,时间
Tue Jul 28 20:59:41 CST 2015
[root@wangchao 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

[root@wangchao shell]# date -s"2015-7-28 19:26:35" //修改日期、时间
Tue Jul 28 19:26:35 CST 2015
[root@wangchao shell]# yum install -y ntp
[root@wangchao shell]# ntpdatetime.windows.com //与该时间服务器同步时间
28 Jul 19:33:03 ntpdate[6388]: step timeserver 23.99.222.162 offset -5.054457 sec

[root@wangchao shell]# date +%F //打印日期
2015-07-28
[root@wangchao shell]# date +%T //打印时间
19:35:05
[root@wangchao shell]# date +%Y //打印年
2015
[root@wangchao shell]# date +%y //打印年的后两位
15
[root@wangchao shell]# date +%m //打印月
07
[root@wangchao shell]# date +%d //日期
28
[root@wangchao shell]# date +%H //时
19
[root@wangchao shell]# date +%M //分
37
[root@wangchao shell]# date +%S //秒
56
[root@wangchao shell]# date +%s
1438083494
//时间簇(距1970年1月1日0点0分0秒走了多少秒了)
[root@wangchao shell]# date +"%Y-%m-%d%H:%M:%S" //显示日期,时间
2015-07-28 19:40:58

[root@wangchao shell]# date -d "-2day" +%F //调整时间2天前
2015-07-26
[root@wangchao shell]# date -d "-2month" +%F //调整时间两个月前
2015-05-28
[root@wangchao shell]# date -d "-2hour" +%T //2小时前
17:44:21
[root@wangchao shell]# date -d "-2min" +%T //2分钟前
19:42:28
[root@wangchao shell]# date -d "-2sec" +%T //2秒前
19:44:33
[root@wangchao shell]# date +%w //显示今天周几
2
[root@wangchao shell]# date +%W //今年第几周
30
Shell自定义变量
[root@wangchao 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
[root@wangchao shell]# echo $HOME
/root
[root@wangchao shell]# echo $PWD
/root/shell
[root@wangchao shell]# a=1
[root@wangchao shell]# echo $a
1
[root@wangchao shell]# vim 2.sh
#!/bin/bash
##
read -p "please input a number:" number
echo $number
[root@wangchao shell]# sh 2.sh
please input a number:4
4

[root@wangchao shell]# vim 2.sh
#!/bin/bash
##
read -t 3 -p "please input a number:" number //加-t 3参数3秒内无输出,退出
echo $number

[root@wangchao shell]# vim 3.sh
#!/bin/bash
##
echo $1 $2 $0 $3
[root@wangchao shell]# sh 3.sh //只输出3.sh
3.sh

[root@wangchao shell]# vim 3.sh
#!/bin/bash
##
echo "\$1=$1"
echo "\$2=$2"
echo "\$0=$0"
echo "\$3=$3"
[root@wangchao shell]# sh 3.sh
$1=
$2=
$0=3.sh
$3=
[root@wangchao shell]# sh 3.sh aa 11 dd
$1=aa
$2=11
$0=3.sh
$3=dd

[root@wangchao shell]# a=1;b=2
[root@wangchao shell]# c=$a+$b
[root@wangchao shell]# echo $c
1+2
[root@wangchao shell]# c=$[$a+$b]
[root@wangchao shell]# echo $c
3

If逻辑判断
[root@wangchao shell]# vim if.sh
#!/bin/bash
##
a=5
if [ $a -gt 3 ]
then
echo "a>3"
fi

[root@wangchao shell]# sh if.sh
[root@wangchao shell]# sh if.sh
a>3
[root@wangchao shell]# sh -x if.sh //查看执行过程
+ a=5
+ '[' 5 -gt 3 ']'
+ echo 'a>3'
a>3

[root@wangchao shell]# vim if.sh
#!/bin/bash
##
a=5
if [ $a -gt 10]
then
echo "a>10"
else
echo "a<=10"
fi

[root@wangchao shell]# sh -x if.sh
+ a=5
+ '[' 5 -gt '10]'
if.sh: line 4: [: missing `]'
+ echo 'a<=10'
a<=10

[root@wangchao 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

[root@wangchao 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判断的几种用法

[root@wangchao shell]# if [ -f 1.txt ];thenecho ok;fi //如果存在1.txt,则显示OK
[root@wangchao shell]# touch 1.txt
[root@wangchao shell]# if [ -f 1.txt ];thenecho ok;fi
ok

[root@wangchao shell]# if [ -d 1.txt ];thenecho ok;fi //判断目录是否存在
[root@wangchao shell]# if [ -r 1.txt ];thenecho ok;fi //判断1.txt是否为可读文件
ok
[root@wangchao shell]# if [ -w 1.txt ];thenecho ok;fi //是否可写
ok
[root@wangchao shell]# if [ -x 1.txt ];thenecho ok;fi //是否可执行

[root@wangchao 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
[root@wangchao shell]# sh if2.sh
please input a number:r
The character you input is not anumber,please retry.

//-n判断变量是否不为空,-z可判断是否为空

[root@wangchao 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.
[root@wangchao shell]# grep '^tom:'/etc/passwd //找出tom的行
tom:x:500:500::/home/tom:/bin/bash
[root@wangchao shell]# if grep -q '^tom:'/etc/passwd;then echo "tom exist";fi
tom exist
//-q只做判断,不输出grep匹配的结果
[root@wangchao shell]# if [ -d /tmp/]&&[ -f 1.txt ];then echo ok;fi
ok
// &&且,两者都存在
[root@wangchao shell]# if [ -d /tmp/ ]||[-f 1.txt ];then echo ok;fi
Ok
// ||或
[root@wangchao shell]# if [ -d /tmp/ -o -f1.txt ];then echo ok;fi
ok
// -oor 或者的意思
[root@wangchao shell]# if [ -d /tmp/ -a -f1.txt ];then echo ok;fi
ok
//a all并且的意思

Case选择
[root@wangchao shell]# cat /etc/init.d/atd //查看linux的一个启动脚本,看case使用例子
[root@wangchao 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

[root@wangchao shell]# sh case.sh
please input a number:10
The number is oushu.
[root@wangchao shell]# sh case.sh
please input a number:1
The number is jishu.
For循环

[root@wangchao shell]# seq 1 10 //产生数字1-10
[root@wangchao shell]# seq 1 2 10 //步长为2生成1-10
[root@wangchao shell]# seq 10 -2 1 //倒叙生成10-1,步长为-2
[root@wangchao shell]# seq 10 -1 1

[root@wangchao shell]# seq -w 01 10 //产生的数 01、02、03方式
01
02
03
04
05
06
07
08
09
10
[root@wangchao shell]# seq -w 01 100 //参数的数001、002、003方式 -w参数

[root@wangchao shell]# vim for.sh //参数数字1-5
#!/bin/bash
for i in `seq 1 5`;
do
echo $i
done

[root@wangchao shell]# sh for.sh
1
2
3
4
5

[root@wangchao shell]# vim for.sh //求1-10和
#!/bin/bash
sum=0
for i in {1..10}
do
sum=$[ $sum+$i ]
done
echo $sum
[root@wangchao shell]# sh for.sh
55

[root@wangchao shell]# vim for.sh
#!/bin/bash
for l in `cat 1.txt`
do
echo $l
done
[root@wangchao shell]# sh for.sh
aa
[root@wangchao shell]# cat 1.txt
aa

[root@wangchao shell]# vim 2.txt //显示每一行(其中空格认为分割符)
1 2 3
lssd
aaa bbb
[root@wangchao shell]# for l in `cat2.txt`;do echo $l;done
1
2
3
lssd
aaa
bbb

[root@wangchao shell]# vim 3.txt
1.1.1.1
2.2.2.2
3.3.3.3

[root@wangchao shell]# for file in `ls` ;do echo $file;done //循环打印文件名
[root@wangchao shell]# for file in `ls` ;do echo $file;du -sh $file;done
//循环打印文件名,并查看文件大小

While循环
[root@wangchao ~]# vim while.sh
#!/bin/bash
while :
do
date +%T
sleep 3
done
//每隔三秒输出时间

[root@wangchao ~]# sh while.sh
21:24:34
21:24:37
21:24:40

[root@wangchao ~]# vim while.sh //生成数字1-10
#!/bin/bash
n=0
while [ $n -le 10 ]
do
echo $n
n=$[n+1]
done
[root@wangchao ~]# sh while.sh
0
1
2
3
4
5
6
7
8
9
10

[root@wangchao ~]# 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

[root@wangchao ~]# 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
[root@wangchao ~]#

//输入数字,则显示退出,非数字,显示并继续输入

Shell中断继续退出
break 、 continue 、exit
[root@wangchao ~]# vim for2.sh
#!/bin/bash
##
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
break //退出本次循环
fi
echo $i
done
[root@wangchao ~]# sh for2.sh
1
1
2
2
3
3
4
[root@wangchao ~]# sh -x for2.sh

[root@wangchao ~]# 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"

[root@wangchao ~]# sh for2.sh
1
1
2
2
3
3
4
for done

[root@wangchao ~]# 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"
[root@wangchao ~]# sh for2.sh
1
1
2
2
3
3
4
5
5
6
6
7
7
8
8
9
9
10
10
for done
[root@wangchao ~]# 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"
[root@wangchao ~]# sh for2.sh
1
1
2
2
3
3
4

Break 结束整个循环体 continue结束本次循环 exit结束shell

Shell函数组
[root@wangchao ~]# a=1
[root@wangchao ~]# echo $a
1
[root@wangchao ~]# a=(1 2 3 4)
[root@wangchao ~]# echo $a
1
[root@wangchao ~]# echo ${a[@]} //显示数组
1 2 3 4
[root@wangchao ~]# echo ${a[*]}
1 2 3 4
[root@wangchao ~]# echo ${a[0]}
1
[root@wangchao ~]# echo ${a[1]}
2
[root@wangchao ~]# echo ${a[2]}
3
[root@wangchao ~]# echo ${a[3]}
4
[root@wangchao ~]# a[4]=9
[root@wangchao ~]# echo ${a[*]}
1 2 3 4 9
[root@wangchao ~]# a[2]=7
[root@wangchao ~]# echo ${a[*]}
1 2 7 4 9
[root@wangchao ~]# echo ${#a[@]} //显示数组个数
5
[root@wangchao ~]# for i in `seq 0 9`;doa[$i]=$RANDOM;done;echo ${a[@]}
30430 29357 27930 22531 11764 1293 1918427274 4774 21057
//产生10个随机数
[root@wangchao ~]# 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个随机数,一行一行显示出

[root@wangchao ~]# 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个随机数,一行一行显示出,并按大小排

[root@wangchao ~]# unset a
[root@wangchao ~]# echo ${a[*]}

[root@wangchao ~]# for i in `seq 0 9`;doa[$i]=$RANDOM;done;echo ${a[@]}
25440 14378 23079 23134 3514 18340 155612812 13446 22478
[root@wangchao ~]# unset a[4] //删除元素a[4]
[root@wangchao ~]# echo ${a[@]}
25440 14378 23079 23134 18340 1556 1281213446 22478
[root@wangchao ~]# echo ${a[@]:0:3} //从0开始取3个数
25440 14378 23079
[root@wangchao ~]# echo ${a[@]:4:4} //显示从第四个开始的后4个数
18340 1556 12812 13446
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell编程进阶