How to do I manipulate a variable in a do loop?

This is my code

cat /path/file |
while read host 
do
echo "\nPINGING $host"
ip=`eval "$host | cut -c14-28"`
echo $ip
ping $ip
sleep 2
done

the file contains a list of hostnames and IP addresses in format

HOSTNAMEXXXX,168.192.100.150

This should work with a slight modification:

cat /path/file |
while read host 
do
   echo "\nPINGING $host"
   ip=`echo "$host" | cut -c14-28`
   echo $ip
   ping $ip
   sleep 2
done

That is replacing that eval. Or did I understand something wrong and you want to achieve something else?

Try:

while IFS=, read host ip
do
  echo "PINGING $host"
  echo "$ip"
  ping "$ip"
  sleep 2
done < /path/file
1 Like

There's quite some methods to manipulate variables, be it within a loop or outside of it, but what you are doing above is not feasible. You are trying to execute the variable's contents like a command, but that doesn't exist: HOSTNAMEXXXX,168.192.100.150: command not found . You should have gotten this error msg as well. On top, counting chars is error prone - a host name's length changing will spoil your command. The eval (to be used judiciously) doesn't help here at all.
The straightforward way is to read the file according to its structure:

while IFS=, read host ip; do echo $ip $host; done < file
168.192.100.150 HOSTNAMEXXXX

ANother way would be to read one line into a variable and then use the shell's parameter expansion: Remove matching prefix pattern. (see e.g. man bash ):

ip=${line#*,}
echo $ip
168.192.100.150

There's still other (powerful) options that can be applied to more complex problems.

1 Like

The file that is read is outputted from another script in the format . . .

hostname,IP

e.g.

HOSTNAMEXXX1,168.192.100.150
HOSTNAMEXXX2,168.192.100.151
HOSTNAMEXXX3,168.192.100.152

The hostname and IP is on the same line separated by a comma. The hostname is 12 characters long and then a comma and the IP thus the cut -c14-28 which gives only the IP. The list of hostnames and IP combination can be any length. I then want to cut the IP alone and ping each one.

I'd strongly encourage reading previous posts/replies on any thread BEFORE making another post.
You've been given options to consider!

1 Like

This doesn't work. I get the following error.

 PINGING HOSTNAMEXXX1,192.168.100.150
HOSTNAMEXXX1,192.168.100.150 | cut -c14-28
ping: bad timeout: |
  
 PINGING HOSTNAMEXXX2,192.168.100.151
HOSTNAMEXXX1,192.168.100.151 | cut -c14-28
ping: bad timeout: |
 

Have you read any of the posts in this thread. Is there anything you do not understand about these suggestions?

Thanks RudiC, the first example works great.

General definition of insanity:
Repeating the very same thing over and over and expecting diffrent results.

Increased chances if:
One is ignoring everything one is beeing told.
And simply keeping repeating the very same question over and over.
(saying: rephrasing your repeated question, maybe that will help)

Rather than stay focused on removing single characters, try the new approach - removing strings.

Have a nice and sunny friday :slight_smile:

Thanks Scrutinizer, this works great.