How to increment a string variable?

Hi All,

I am new to this forum and a novice at shell script. I am trying to write a script to determine each of the NIC configured on a linux system and its speed and Duplex. I came up with the following piece of code:

echo `ifconfig -a | grep eth > /home/a/nic.txt`

i=`awk -F, '{print NR}' /home/a/nic.txt`

for j in $i

do

  nic"${j}"=`awk '{NR=$j} {print $1}' /home/a/nic.txt`

  echo $nic"${j}"

  if [ "$nic" == "" ];

  then

  exit 1

  else

   echo "SPEED & DUPLEX OF $nic$j:`ethtool $nic$j | grep -i duplex; ethtool $nic$j | grep -i speed`"


  fi

done

But I am coming up with command not found for line in red. Why can't I declare a variable and increment just its count?

Use eval built-in

Here is an example:

#!/bin/bash

for i in {1..5}
do
        eval "nic$i"=$i
        echo "nic$i=`eval echo $nic$i`"
done

But this is a bad programming practice. I recommend using arrays instead.

Simple array example:

#!/bin/bash
for j in 1 2 3; do
  nic[j]=a$j
done
$ echo "${nic[@]}"
a1 a2 a3
$ echo "${nic[2]}"
a2

How about:

for nic in $(ifconfig -s | awk 'NR>1&&$1!="lo"{print $1}')
do
    echo "SPEED & DUPLEX OF $nic: " $(ethtool $nic | grep -iE "duplex|speed")
done
SPEED & DUPLEX OF p2p1:  Speed: 100Mb/s Duplex: Full
SPEED & DUPLEX OF p2p2:  Speed: 1000Mb/s Duplex: Full

Thank you people....yes using arrays will be the best way of programming. But as I needed to get the system info, I ended up doing what Chubler_XL suggested and it works like a charm. Thank you Chubler_XL