Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into multiple variables in Bash [duplicate]

Tags:

bash

I have a string that gets generated below:

192.168.1.1,UPDOWN,Line protocol on Interface GigabitEthernet1/0/13, changed state to up 

How can I take that string and make 2 variables out of it (using bash)?

For example I want

$ip=192.168.1.1  $int=GigabitEthernet1/0/13 
like image 723
l0sts0ck Avatar asked May 14 '14 19:05

l0sts0ck


1 Answers

Try this:

mystring="192.168.1.1,UPDOWN,Line protocol on Interface GigabitEthernet1/0/13, changed state to up"  IFS=',' read -a myarray <<< "$mystring"  echo "IP: ${myarray[0]}" echo "STATUS: ${myarray[3]}" 

In this script ${myarray[0]} refers to the first field in the comma-separated string, ${myarray[1]} refers to the second field in the comma-separated string, etc.

like image 166
Helio Avatar answered Sep 20 '22 15:09

Helio