Shell脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell脚本相关的知识,希望对你有一定的参考价值。

#######################

强引用、弱引用

‘‘ ""

[[email protected] Desktop]# echo "I have a $fruit"

I have a 

[[email protected] Desktop]# echo ‘I have a $fruit‘

I have a $fruit

[[email protected] Desktop]# echo Make \$\$ Fast\!

Make $$ Fast!

[[email protected] Desktop]# echo Make $$ Fast!

Make 4119 Fast!

[[email protected] Desktop]# echo #I have a banana#


[[email protected] Desktop]# echo \#I have a banana

#I have a banana

[[email protected] Desktop]# echo ‘#I have a banana‘

#I have a banana

[[email protected] Desktop]# echo `pwd`

/root/Desktop

[[email protected] Desktop]# echo ‘ "Goodafternoon" ‘

 "Goodafternoon" 

变量:

[[email protected] Desktop]# i=9

[[email protected] Desktop]# j=0

[[email protected] Desktop]# echo $i $j

9 0

父shell,子shell

[[email protected] Desktop]# bash

[[email protected] Desktop]# bash

[[email protected] Desktop]# bash

[[email protected] Desktop]# bash

[[email protected] Desktop]# exit

exit

[[email protected] Desktop]# exit

exit

[[email protected] Desktop]# exit

exit

[[email protected] Desktop]# exit

exit

[[email protected] Desktop]# exit

exit

#######################

算术运算符

<value>++ 赋值后增1  <value>--  赋值后减1

 -     减法       +      加法

 **    幂运算     *      乘法

 /     除法      %       余数

 +=    加等     -=       减等

计算命令:$[],expr,let,(())

#######################

循环:

for do done

while do done

例子:1+2+...+100,数据库备份

vim 100.sh

  #!/bin/bash

  for i in {1..100}

  do

  ((j+=i));

  echo $j

  done

chmod +x 100.sh

./100.sh

显示结果:

1

3

6

10

...

5050

vim mysql.sh

 #!/bin/bash

 for name in ` mysql -uroot -predhat -e "show databases;" -NE | grep -  E "^\*|^u|schema$" -v `

 do

 mysqldump -uroot -predhat $name > /mnt/${name}.dump

 echo "$name is ok."

 done

#######################

test条件判断:

语法:test EXPRESSION 或[EXPRESSION]

[[email protected] Desktop]#[ -n westos ];echo $?

0

[[email protected] Desktop]#[ -z westos ];echo $?

1

字符串比较运算符:==、!=

[[email protected] Desktop]#[ abc=ABC ];echo $?

0

[[email protected] Desktop]#[ abc=ABC ];echo $?    

1

[[email protected] Desktop]#[ abc!=ABC ];echo $?

0

数字比较运算符:

-eq 判断是否相等,相等返回0;

-ne 判断是否不等,别等返回0;

-lt 判断是否小于,小于返回0 ;

-le 判断是否小于等于,若小于等于为0;

-gt 判断是否大于,大于返回0;

-ge 判断是否大于等于,若大于等于返回0。

[[email protected] Desktop]#[ 1 -eq 1 ];echo $?

0

[[email protected] Desktop]#[ 1 -ne 1 ];echo $?

1

[[email protected] Desktop]#[ 1 -gt 2 ];echo $?

1

文件状态运算符:

语法: test -{-b|-c|-e|-f|-d|-L} filename|direcrtory

-b:判断是否为块设备

-c:判断是否为字符文件

-e:判断文件是否存在

-f:判断是否为普通文件

-d:判断是否是目录

-L:判断是否为链接文件(软连接)


二进制文件运算符:-ef -nf -ot

逻辑运算符:-o -a ! && ||

{ if;then fi }

{if ;then else fi}

{if ;then else if; then else if; then ... else fi}

case "$1" in

 );;

 );;

 );;

esac

简易一天倒计时:

#!/bin/bash

for ((hour=23;hour>=0;hour--))

do

for ((min=59;min>0;min--))

do

for (( sec=59;sec>=0;sec--))

do

echo -ne "After ${hour}:${min}:${sec} is end."

echo -ne "\r\r"

sleep 1

done

done

done

#!/bin/bash

for num in {1..30}

do

ping -c1 -w1 192.168.122.$num &>/dev/null

while [ "$?" -eq "0" ]

do

echo 192.168.122.$num is up

break 

done

while [ "$?" -ne "0" ]

do

echo 192.168.122.$num is down

done

done

#######################

expect

#######################

env

#######################

别名alias

#######################


本文出自 “12098166” 博客,请务必保留此出处http://12108166.blog.51cto.com/12098166/1911282

以上是关于Shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

Shell脚本--变量(后附简单shell脚本案例)!

shell脚本翻译 急求

shell脚本书写方法

如何在shell脚本里调用另一个shell脚本

shell脚本获取参数&在线执行shell脚本

shell脚本中#是啥意思