0
#! /bin/bash
#read line for line 
read -p "Enter path to file" file
url=""
while read -r line; do
# reading each line
echo "$line"
url="$url $line"
done < "$file" 
#-------------------------------------------------------------------
n=0
m=0

while [ $n -lt 5 ]
do

    
    n=$(( n+1))
    m=$(( m+1))
#ping url to usable ip for further use 
    pingurl=$(echo "${url[@]}"| awk '{print $1+$m}') 
    pingip=$(ping -c 1 $pingurl | grep 'bytes of data' | awk  '{print $3}' | awk -F "("  '{print $2}'| awk -F ")"  '{print $1}')
echo "$n"; echo "$pingip"
`

done


this is my bash script to put in a file path (with url). then read it line by line and store it in varible $url after that it defines the var. $pingurl by piping the var.$url with awk to print only one url to process via ping . I would like to add for each loop the awk value by 1 . As you see i aproached this by trying to add the value for each loop with the var. $m but it does not work . i am breaking my mind about it maybe some wise guys are able to help me with this issue .

i tried already to display the url with ${url[(1+$m)]}

nima
  • 11
  • Better add sample input/expected output – Gilles Quénot Apr 04 '23 at 16:26
  • I don't see you using the _awk_ pipe operator at all here; this is just shell pipes combining separate awk calls. In general, _don't do that_; it's much better to run fewer copies of awk and have them do more work instead of a lot of separate copies that each do very little. awk is a full-fledged programming language; it's good for much more than little one-line print commands. – Charles Duffy Apr 04 '23 at 16:30
  • Is the backtick before `done` a copying error? – Barmar Apr 04 '23 at 16:31
  • Anyhow, can you describe what you're trying to extract? We don't know what output your copy of `ping` has (each OS has its own), but there's probably a much better way to get the value you're looking for. (Similarly, if we don't know what the data you're reading from your file is, we can't test how your code is _actually_ interpreting that data or speculate about how you _intended_ it to interpret that data). – Charles Duffy Apr 04 '23 at 16:33
  • (If your "URLs" are actually IP addresses and you're trying to scan a network range, a tool built for that purpose will be much, _much_ faster; something like nmap can scan large parts of a subnet all at once, instead of looping around and trying to ping each address one at a time; also, `ping` is not so useful for reachability these days in the first place -- a lot of hosts intentionally ignore ICMP requests). – Charles Duffy Apr 04 '23 at 16:36
  • `$url` is not an array. Use `readarray` to create an array from a file, see the linked question. – Barmar Apr 04 '23 at 16:38
  • And the `awk` stuff is way more complicated than necessary. `ping -c 1 $pingurl | awk -F '[()]' '/bytes of data/ {print $2}'` – Barmar Apr 04 '23 at 16:39
  • @CharlesDuffy It doesn't look like they're trying to do `nmap`, they're doing `nslookup` using ping's output. – Barmar Apr 04 '23 at 16:40
  • 1
    And to loop over an array, do `for pingurl in "${url[@]}"` – Barmar Apr 04 '23 at 16:41
  • @Barmar yes just a mistake – nima Apr 04 '23 at 16:51
  • @CharlesDuffy i have a text file with urls and i wnt to automate the ping command to give me the ips for each url in the text file – nima Apr 04 '23 at 16:55
  • Enter path to file/home/nima/Desktop/test.txt google.com youtube.com facebook.com bs.to lvm.de amazon.com 1 172.217.16.46 2 172.217.16.46 3 172.217.16.46 4 172.217.16.46 5 172.217.16.46 that is the output from my terminal running the script – nima Apr 04 '23 at 17:10

0 Answers0