zoukankan      html  css  js  c++  java
  • while read读取文本内容

    读取文件给 while 循环
    方式一:

    exec <FILE 
    while read line 
    do 
        cmd 
    done 

    方式二:

    cat FILE_PATH |while read line 
    do 
        cmd 
    done 

    方式三:

    while read line 
    do 
        cmd 
    done <FILE

    举例:

    ip.txt内容如下:

    10.1.1.11 root 123
    10.1.1.22 root 111
    10.1.1.33 root 123456
    10.1.1.44 root 54321

    写法1:

    cat ip.txt | while read ip user pass
    do
        echo "$ip--$user--$pass"
    done

    写法2:

    while read ip user pass
    do
        echo "$ip--$user--$pass"
    done < ip.txt

    使用IFS作为分隔符读文件

    说明:默认情况下IFS是空格,如果需要使用其它的需要重新赋值

    IFS=:

    例如:

    # cat test
    chen:222:gogo
    jie:333:hehe
    # cat test.sh
    #!/bin/bash
    IFS=:
    cat test | while read a1 a2 a3
    do
        echo "$a1--$a2--$a3"
    done
    本文作者:fcing
    本文出处:https://www.cnblogs.com/fcing
    版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明出处!
  • 相关阅读:
    Postgresql常用命令&&函数
    gcc g++参数
    cython编译Python为c语言
    安装python3
    pip 国内源
    ceph 对象存储s3
    Rancher基础
    helm常用命令
    chartmuseum配置和使用
    python-etcd3
  • 原文地址:https://www.cnblogs.com/fcing/p/9375117.html
Copyright © 2011-2022 走看看