Redirect output to a bash array

前端 未结 5 1237
感动是毒
感动是毒 2020-12-14 02:39

I have a file containing the string

ipAddress=10.78.90.137;10.78.90.149

I\'d like to place these two IP addresses in a bash array. To achie

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 03:29

    You can do this by using IFS in bash.

    • First read the first line from file.
    • Seoncd convert that to an array with = as delimeter.
    • Third convert the value to an array with ; as delimeter.

    Thats it !!!

    #!/bin/bash
    IFS='\n' read -r lstr < "a.txt"
    IFS='=' read -r -a lstr_arr <<< $lstr
    IFS=';' read -r -a ip_arr <<< ${lstr_arr[1]}
    echo ${ip_arr[0]}
    echo ${ip_arr[1]}
    

提交回复
热议问题