linux for循环 数组,shell 数组(for循环、while)

字符串小于比较 < ( [ ]结构中要转义成\

模式匹配 ==

正则表达式匹配 =~

整数相等比较 -eq

整数大于 -gt

整数小于 -le

[ -d /opt/db_back ]等等,test表达式。等我找到全的test,贴出来给大家看。

-----------------------------------------------------------------------------------------------------------------------------------

[root@CMS1 log]# cd /cmspic/uploadfiles/xml/2012/1/

1/  10/ 11/ 12/ 2/  3/  4/  5/  6/  7/  8/  9/

[root@CMS1 log]# cd /cmspic/uploadfiles/xml/2012/1/12/

[root@CMS1 12]# ls

abook_0010b435-bd3f-4b9f-bd3b-9d109db3d07e.xml  abook_5dffce1e-2fcd-4c70-b660-0c9d0e1e3e08.xml  audiobook  mobile

abook_0e68e1d2-1c9c-4f5b-991b-dade277db357.xml  abook_9ab7553e-ca47-40a7-85d3-5897d658305b.xml  ebook      series

[root@CMS1 12]# grep "伸冤记" *

[root@CMS1 12]# find . -type f | xargs grep -l "伸冤记"

./mobile/MoviesXml_6109c33b-4aaf-4a2f-8378-aee617256e77.xml

[root@CMS1 12]#

----------------------------------------------------------------------------------------------

-bash-3.2$ cat fffor.sh

#!/bin/bash

xml=$(ls /home/kaige/audiobook)

for var1 in ${xml}

do

echo ${var1}

mv ${var1} ${var1}yk

done

----------------------------------------------------------------------------------------------

-bash-3.2$ cat fffor.sh | grep "(*)"

xml=$(ls /home/kaige/audiobook)

-bash-3.2$

--------------------------------------------

下面的是以前写在别的地方的博客,弄回来了。

1.

#!/bin/bash

#ip=(ip1 ip2 ip3)

for i in ip1 ip2 ip3

do

echo ssh root@$i

#/opt/apache2/conf/apachectl restart &

echo "已重启$i apache 服务"

done

exit

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2.

[root@testcms tbird]# cat ceshi412

#!/bin/bash

ip=(ip1 ip2 ip3)  //数组

for i in ${ip[@]}  //数组

do

echo ssh root@ $i

#/opt/apache2/conf/apachctl restart &

done

echo "已重启${ip[@]} apache 服务"

exit

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.

#!/bin/bash

ip=(ip1 ip2 ip3)

/opt/apache2/conf/apachectl restart &

duankou=`netstat -tnl | grep 80`

if ["$duankou" -eq 1 ];then

sleep 5

echo "已重启本地apache!"

elif

echo "本地apache重启失败!"

fi

for i in ${ip[@]}

do

ssh root@$i

/opt/apache2/conf/apachectl restart &

done

echo "已重启$ip apache 服务!"

exit

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4.

#!/bin/bash

ip=(ip1 ip2 ip3)

#for i in ${ip[@]}

j=${#ip[@]}

for((i=0;ido

echo ssh root@${ip[i]}

#/opt/apache2/conf/apachelt restart &

echo "已重启$((i+1)) apache 服务"

done

exit

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

array

Bash中还可以使用数组变量,其赋值有两种:

(1) name = (value1 ... valuen) 此时下标从0开始

(2) name[index] = value

数组下标的范围没有任何限制,同时也不必使用连续的分量.

--------------------------------------------------------------------------------

$ A=(a b c def)

----------------------------------------------------------------------------------------------$ echo ${A[@]} //取全部元素

a b c def

--------------------------------------------------------------------------------------------- $ echo ${A[0]} //取第一个元素

a

----------------------------------------------------------------------------------------------//取得数组元素的个数

$ echo ${#A[@]}

4

$ echo ${#A}

4

$ echo ${#A[3]} //取得元素3的长度

$

----------------------------------------------------------------------------------------------$ A[3]=yaoshuyin //将第三个元素重新赋值

$ echo ${A[@]}

a b c yaoshuyin

----------------------------------------------------------------------------------------------//清除变量

$ unset A

$ echo ${A[@]}

$

----------------------------------------------------------------------------------------------//清空变量,即将值变为空

$ A=

$ echo ${A[@]}

$

----------------------------------------------------------------------------------------------A=B

B=C

unset $A 事实上所取消的变量是 B 而不是 A

-------------------------------

while ---------------------------------------------------

#建立数组

arrSource=("arrJobs.php" "arrSubHangye.php" "arrFirst.php" )

arrDest=("buildhr" \

"buildtrain/htdocs" \

"bankhr" \

"healthr" \

"elehr" \

)

#取数组无元素个数

lenArrSource=${#arrSource

}

lenArrDest=${#arrDest

}

#循环列出数组元素

i=0

while [ $i -lt $lenArrSource ]

do

echo ${arrSource[$i]}

let i++

done

i=0

while [ $i -lt $lenArrDest ]

do

echo ${arrDest[$i]}

let i++

done

-----------------------------------for---------------------------------------------------

#源文件

arrSource=("/home/800hr/htdocs/login_jump.php")

#目标网站

arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)

for outer in ${arrSource}

#${arrSource

} 是数组中的所有元素

do

for inner in ${arrDest

}

do

echo "ln -s $outer /home/${inner}/campus/"

done

done

----------------------------------------------------------------------------------------------

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值