好久不写脚本了,最近写awk比较多,今天发现好多基本的忘记了,就写个测试的(今天用了while),调试老报错,弄迷糊了。。。因为awk里面的语法跟c差不多,而shell里面的则是乱七八糟的,好多也记不住了,丢人。。。
----------------------------------------------------------------------------------------------
#!/bin/bash
i=1
#while ($i < "10" )//awk里面是这样,shell不行,**了。。。
#while [ $i < "10" ]//这样写不对,无法识别“10”。
while [ $i -le "10" ]
do
#i=`${i}+1` //这样写不对,要么用expr。
i=$((${i}+1))
#i+=$((${i}+1))//这样写也可以
done
echo $i
exit
----------------------------------------------------------------------------------------------
字符串相等比较 一般用 =
字符串大于比较 > ( [ ]结构中要转义成\>)
字符串小于比较 < ( [ ]结构中要转义成\<)
模式匹配 ==
正则表达式匹配 =~
整数相等比较 -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;i
do
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
----------------------------------------------------------------------------------------------
阅读(13825) | 评论(0) | 转发(1) |