Linux - pits: IFS

1 Overview

  1. When reading the contents of the file for loop, the output is broken shivers

2. Scene

  1. demand
    1. Read the file
    2. Progressive
  2. Source File

    Continuous Delivery with Docker and Jenkins
    Jenkins 2 Up and Running
    Jenkins 2.x Continuous Integration Cookbook(3rd)
    Jenkins Fundamentals  
  3. script

    #!/bin/bash
    
    for line in `cat ${1}` 
    do 
        echo ${line}
    done 
    
  4. problem
    1. The result output, this is

      Continuous
      Delivery
      with
      Docker
      and
      Jenkins
      Jenkins
      2
      Up
      and
      Running
      Jenkins
      2.x
      Continuous
      Integration
      Cookbook(3rd)
      Jenkins
      Fundamentals

3. Solution

  1. the reason
    1. Before output, the need to develop ifs
  2. ifs
    1. linux environment variables
    2. slightly
  3. The final script
    `` `
    #! / Bin / bash

    OLD_IFS=${IFS}
    IFS=""

    for line in cat ${1}
    do
    echo ${line}
    done

    IFS=${OLD_IFS}
    ```

4. Other

  1. ref
    1. SHELL read each line of the content and output files
      1. While output by, looks like you do not have this problem ...
    2. Shell usage in IFS
  2. doubt
    1. IFS What the hell is this thing in the end
    2. $ {1} is a bash script parameter, if that script with awk command, need to refer to $ 1, which prevail in the end it?

Guess you like

Origin www.cnblogs.com/xy14/p/11484769.html