zoukankan      html  css  js  c++  java
  • shell script

    一)、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环境的支持,达到预期的效果,再考虑速度方面的问题。
     
     
     
     
     
  • 相关阅读:
    2019重新起航:搭建个人博客
    Alibaba Nacos:搭建Nacos平台
    ELK:使用docker搭建elk平台
    SpringBoot:关于默认连接池Hikari的源码剖析
    git常用命名:自用,持续更新
    linux常用命名汇总:自用,持续更新
    升级项目版本:SpringBoot1.5.x到SpringBoot2.0.x
    Python学习第十五课——类的基本思想(实例化对象,类对象)
    Python学习第十三课——re(正则表达式)模块
    Python学习第十四课——面向对象基本思想part1
  • 原文地址:https://www.cnblogs.com/feika/p/4036203.html
Copyright © 2011-2022 走看看