码迷,mamicode.com
首页 > 系统相关 > 详细

shell script

时间:2014-10-20 00:40:57      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   for   

一)、bash shell中expr命令下几种的使用

expr在linux中是一个功能非常强大的命令。通过学习做一个小小的总结。
1、计算字符串的长度。我们可以用awk中的length(s)进行计算。我们也可以用echo中的echo ${#string}进行计算,当然也可以expr中的expr length $string 求出字符串的长度。

举例

  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"  
  2. [root@localhost shell]# echo ${#string}  
  3. 34  
  4. [root@localhost shell]# expr length "$string"  
  5. 34  
2、expr中的expr index $string substring索引命令功能在字符串$string上找出substring中字符第一次出现的位置,若找不到则expr index返回0或1。
举例
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"   
  2. [root@localhost shell]# expr index "$string" my   
  3. 11  
  4. [root@localhost shell]# expr index "$string" nihao   
  5. 1  
3、expr中的expr match $string substring命令在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0。
举例
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"   
  2. [root@localhost shell]# expr match "$string" my   
  3. 0  
  4. [root@localhost shell]# expr match "$string" hell.*   
  5. 34  
  6. [root@localhost shell]# expr match "$string" hell   
  7. 4  
  8. [root@localhost shell]# expr match "$string" small   
  9. 0  
4、在 shell中可以用{string:position}和{string:position:length}进行对string字符串中字符的抽取。第一 种是从position位置开始抽取直到字符串结束,第二种是从position位置开始抽取长度为length的子串。而用expr中的expr substr $string $position $length同样能实现上述功能。
举例
  1. root@localhost shell]# string="hello,everyone my name is xiaoming"   
  2. [root@localhost shell]# echo ${string:10}   
  3. yone my name is xiaoming  
  4. [root@localhost shell]# echo ${string:10:5}   
  5. yone  
  6. [root@localhost shell]# echo ${string:10:10}   
  7. yone my na  
  8. [root@localhost shell]# expr substr "$string" 10 5   
  9. ryone  

注意:echo ${string:10:5}和 expr substr "$string" 10 5的区别在于${string:10:5}以0开始标号而expr substr "$string" 10 5以1开始标号。

5、删除字符串和抽取字符串相似${string#substring}为删除string开头处与substring匹配的最短字符子串,而${string##substring}为删除string开头处与substring匹配的最长字符子串。
举例
  1. [root@localhost shell]# string="20091111 readnow please"   
  2. [root@localhost shell]# echo ${string#2*1}   
  3. 111 readnow please  
  4. [root@localhost shell]# string="20091111 readnow please"   
  5. [root@localhost shell]# echo ${string##2*1}   
  6. readnow please  
解析:第一个为删除2和1之间最短匹配,第二个为删除2和1之间的最长匹配。
6、替换子串${string/substring/replacement}表示仅替换一次substring相配字符,而${string//substring//replacement}表示为替换所有的substring相配的子串。
举例
  1. [root@localhost shell]# string="you and you with me"   
  2. [root@localhost shell]# echo ${string/you/me}   
  3. me and you with me  
  4. [root@localhost shell]# string="you and you with me"   
  5. [root@localhost shell]# echo ${string//you/me}   
  6. me and me with me 
二)、关于shell中的for循环用法

1、 for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
2、在shell中常用的是 for i in $(seq 10)
3、for i in `ls`

4、for i in ${arr[@]}
5、for i in $* ; do
6、for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
7、for i in f1 f2 f3 ;do
8、for i in *.txt
9、for i in $(ls *.txt)
for in语句与` `和$( )合用,利用` `或$( )的将多行合为一行的缺陷,实际是合为一个字符串数组

============ -_- ==============for num in $(seq 1 100)
10、LIST="rootfs usr data data2"
for d in $LIST; do
用for in语句自动对字符串按空格遍历的特性,对多个目录遍历
11、for i in {1..10}
12、for i in stringchar {1..10}
13、awk ‘BEGIN{for(i=1; i<=10; i++) print i}‘

注意:AWK中的for循环写法和C语言一样的





===============================================================





01.#/bin/bash

02.# author: 周海汉

03.# date :2010.3.25

04.# blog.csdn.net/ablo_zhou

05.arr=("a" "b" "c")
06.echo "arr is (${arr[@]})"
07.echo "item in array:"
08.for i in ${arr[@]}
09.do
10. echo "$i"
11.done
12.echo "参数,\$*表示脚本输入的所有参数:"
13.for i in $* ; do
14.echo $i
15.done
16.echo
17.echo ‘处理文件 /proc/sys/net/ipv4/conf/*/accept_redirects:‘
18.for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
19.echo $File
20.done
21.echo "直接指定循环内容"
22.for i in f1 f2 f3 ;do
23.echo $i
24.done
25.echo
26.echo "C 语法for 循环:"
27.for (( i=0; i<10; i++)); do
28.echo $i
29.done


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

shell中for循环用法
shell语法好麻烦的,一个循环都弄了一会 ,找了几个不同的方法来实现输出1-100间可以被3整除的数
1.用(())
#!/bin/bash
clear

for((i=1;i<100;i++))
for
do
if((i%3==0))
then
echo $i
continue
fi
done
2.使用`seq 100`
#!/bin/bash
clear

for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
3.使用while
#!/bin/bash
clear

i=1
while(($i<100))
do
if(($i%3==0))
then
echo $i
fi
i=$(($i+1))
done



--------------------------------------------------------------------------------------------------------
在shell用for循环做数字递增的时候发现问题,特列出shell下for循环的几种方法:

1.

for i in `seq 1 1000000`;do

echo $i

done

用seq 1 10000000做递增,之前用这种方法的时候没遇到问题,因为之前的i根本就没用到百万(1000000),因为项目需要我这个数字远大于百万,发现用 seq 数值到 1000000时转换为1e+06,根本无法作为数字进行其他运算,或者将$i有效、正确的取用,遂求其他方法解决,如下

2.

for((i=1;i<10000000;i++));do

echo $i

done

3.

i=1

while(($i<10000000));do

echo $i

i=`expr $i + 1`

done

因为本方法调用expr故运行速度会比第1,第2种慢不少不过可稍作改进,将i=`expr $i + 1`改为i=$(($i+1))即可稍作速度的提升,不过具体得看相应shell环境是否支持

4.

for i in {1..10000000;do

echo $i

done

其实选用哪种方法具体还是得由相应的shell环境的支持,达到预期的效果,再考虑速度方面的问题。
 
 
 
 
 

shell script

标签:style   blog   http   color   io   os   ar   使用   for   

原文地址:http://www.cnblogs.com/feika/p/4036203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!