shell编程进阶

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

时间: 2024-12-25 03:17:22

shell编程进阶的相关文章

Shell编程进阶篇(完结)

1.1 for循环语句 在计算机科学中,for循环(英语:for loop)是一种编程语言的迭代陈述,能够让程式码反复的执行. 它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或是循环变数.这使得for循环能够知道在迭代过程中的执行顺序. 1.1.1 shell中的for循环 shell中的for 循环与在c中不同,它包含三种形式:第一种结构是列表for 循环;第二种结构就是不带列表的for循环:第三种就类似于C语言. ①   列表for循环(常用) #!/bin/bash

linux shell编程进阶学习

第一节:基础 ls -lh  --可以用户友好的方式看到文件大小 file 文件名 --查看文件类型 stat 文件名 --查看文件当前状态 man 命令/函数名 --查看详细的帮助文档 man中看某一命令选项的定位技巧  -- 输入/ -n -n前面要有一定的空格 #!/bin/bash echo "Hello World!" chmod 777 hello.sh或chmod a+x hello.sh等 ./hello.sh执行 echo -e选项用来增强,支持转义字符. var=w

Shell编程进阶 2.0 shell中断继续退出

break    continue   exit break 结束本次for循环 写个for循环脚本 vim for2.sh #!/bin/bash ## for i in `seq 1 5` do echo $i if [ $i -eq 4 ] then break fi echo $i done echo "for done" sh for2.sh 1122334for done continue 结束本次循环 #!/bin/bash ## for i in `seq 1 5` d

Shell编程进阶 1.2 shell结构及执行

创建一个shell脚本 mkdir shell vim first.sh #!/bin/bash ##The first test shell script. ##Written by wangshaojun. ls /tmp/ echo "my home is $HOME" echo "This is first script" 执行shell脚本,两种方法 bash first.sh mysql.sock www_slow.log2015-12-31.wangs

Shell 编程进阶

Shell 介绍 date 命令 自定义变量 if 逻辑判断 case 选择 for 循环 while 循环 Shell 中断继续退出 Shell 函数 Shell 数组

shell编程进阶之数组

数组ARRAY 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合,元素编号支持稀疏格式,即索引编号不连续. 高级变量用法 - 有类型变量 Shell变量一般是无类型的,但是bash提供了declare和typeset两个命令用于指定变量的类型,两个命令是等价的 declare [OPTION] 变量名 -r  声明或显示只读变量 -i  声明或显示整数型变量 -a  声明或显示索引数组 -A  声明或显示关联数组 -f  显示系统的所有函数 -F  仅显示所

SHELL编程-Shell编程进阶

1.1 shell脚本介绍 shell结构以及执行

shell编程进阶篇

上一篇文章介绍的是shell的基础知识:https://blog.51cto.com/14048416/2355550??有人会说,shell简单啊,就是一些命令的堆砌啊,是的一些简单的操作仅仅执行几个命令就行了,但是相对一些复杂的业务和要求下,如果只能做到命令的堆砌,那也太有损shell的名声了.??小编通过一个例子给大家介绍,如果没有逻辑和条件,只是命令的堆砌,那么对维护Linux,是多么大的灾难. #需求:清除/var/log 下message 日志文件命令堆砌 #!/bin/bash #

Shell编程进阶 1.3data命令

date命令是显示日期时间的命令 date 2016年 01月 01日 星期五 15:05:01 CST 修改时间的选项是 -s date -s "2016-01-01 12:56:10" 查看日历 cal 全年日历 cal -y 同步服务器的时间 yum install ntp -y ntpdate ntp.fudan.edu.cn date的用途 date +%F 年月日T 时分秒Y 四位的年y 两位的年m 月份d 日W 今年的第几周w 周几H 时M 分S 秒s 距 1970-01