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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to do I manipulate a variable in a do loop?
# 1  
Old 02-19-2015
How to do I manipulate a variable in a do loop?

This is my code

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
Code:
HOSTNAMEXXXX,168.192.100.150


Last edited by bartus11; 02-19-2015 at 04:56 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 02-19-2015
This should work with a slight modification:

Code:
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?
# 3  
Old 02-19-2015
Try:
Code:
while IFS=, read host ip
do
  echo "PINGING $host"
  echo "$ip"
  ping "$ip"
  sleep 2
done < /path/file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-19-2015
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:
Code:
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):
Code:
ip=${line#*,}
echo $ip
168.192.100.150

There's still other (powerful) options that can be applied to more complex problems.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-19-2015
The file that is read is outputted from another script in the format . . .

hostname,IP

e.g.
Code:
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.

Last edited by vgersh99; 02-19-2015 at 07:35 PM.. Reason: code tags, please!
# 6  
Old 02-19-2015
Quote:
Originally Posted by ditnl555v
The file that is read is outputted from another script in the format . . .

hostname,IP

e.g.
Code:
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!
This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 02-20-2015
This doesn't work. I get the following error.

Code:
 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: |

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

While loop variable

while read -r tabname rem do select count(*) from $tabname" > /dev/null RC=$? if ] then echo "error on $tabname" >> error.out fi done well in above code i need the echo "error on $tabname" to come in separate o/p file which show the table name whc... (3 Replies)
Discussion started by: netdbaind
3 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. UNIX for Dummies Questions & Answers

Writing a for loop to manipulate multiple files

Hi, I have 1000 text files in a folder that are labeled data1.txt all the way to data1000.txt. I want to write a small script that manipulates the text files in this way: (1) cut the 2nd and 9th columns of the text files (2) sort by the numerical value in the 9th column (3) then save the rows... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

7. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

8. UNIX for Dummies Questions & Answers

Writing a loop to manipulate a script and store it in multiple output files

I have a script where the the 9th line looks like this: $filename=sprintf("250.1chr%d.ped", $N); I want to modify this script 1000 times, changing 250.1chr%d.ped to 250.2chr%d.ped, 250.3chr%.ped.......and so on all the way to 250.1000chr%d.ped and store each output in files called ... (4 Replies)
Discussion started by: evelibertine
4 Replies

9. Shell Programming and Scripting

Variable changes after a loop

Hi, I am a beginner with shell programming: the code is following: #!/usr/bin/ksh v_res="start" echo "v_res_begin="$v_res grep -E "#FlatFiles" "a.log" | while IFS= read vLine do set -- $vLine v_res="error" # there is some code ... if error then break echo... (4 Replies)
Discussion started by: llelkes
4 Replies

10. Shell Programming and Scripting

variable in a for loop

Hi... i am trying to ping my servers .The hostnames are present in a file .They are separated by spaces in the file . i am doing the following : a=1 for name in $(cat host2 |cut -d" " -f$a) do echo Pinging server $name ping -c5 $name a=$a+1 done It is... (3 Replies)
Discussion started by: sars
3 Replies
Login or Register to Ask a Question