how to pass bash script comand results to a bash array?

solohayuno
solohayuno used Ask the Experts™
on
Need to pass two values MAC and IPv4 to an array, look code below.
#/bin/bash
# needs:
# $1 file_path (cisco dhcp static client file, see attachments:CiscoRouter.dhcp)
# $2 network address (according to file 10.0.0.)
 
X=$(grep "$2" $1 | wc -l| tr -d ' ' )
for ((i=1; i <= $X; ++i  ));
do
 N="$i"p
 echo $(grep "$2" $1 | sed -n "$N" | awk {'print $4"\t"$1'})
#the above line print mac and ip,net to separe on the array mac and IP
# X = (`echo $(grep "$2" $1 | sed -n "$N" | awk {'print $4"\t"$1'})`)
# expected result when i=1
# echo ${X[0]:0} = 001b.b46b.41ae
# echo ${X[1]:0} = 10.0.0.1
done

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
X = ($(grep "$2" $1 | sed -n "$N" | awk {'print $4"\t"$1'}))

Author

Commented:
that code return:
syntax error near unexpected token `('
X = ($(grep "$2" $1 | sed -n "$N" | awk {'print $4"\t"$1'}))
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
X=($(grep "$2" $1 | sed -n "$N" | awk {'print $4"\t"$1'}))
your code has a lot of overheads, it can be cut down to improve efficiency. where is your attachment of the input file? and show the final output that you want.....
Here is the file.
And here is the closest code to convert mac address from cisco hex format 0123.4567.89AB to linux dchp3 format 01:23:45:67:89:AB
#/bin/bash
# needs:
# $1 file_path (cisco dhcp static client file, see attachments:CiscoRouter.dhcp)
# $2 network address (according to file 10.0.0.)
 
X=$(grep "$2" $1 | wc -l| tr -d ' ' )
for ((i=1; i <= $X; ++i  ));
do
 N="$i"p
VAR=$(echo $(grep "$2" $1 | sed -n "$N" | awk {'print $4"\t"$1'} | sed 's/.\([0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]\).\([0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]\)/\1\2/g'))
VAR=(${VAR// / })
echo "hardware ethernet ${VAR[0]:0:2}:${VAR[0]:2:2}:${VAR[0]:4:2}:${VAR[0]:6:2}:${VAR[0]:8:2}:${VAR[0]:10:2};"
done

Open in new window

cisco.txt

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial