W
W
wellspring2019-10-06 19:21:36
bash
wellspring, 2019-10-06 19:21:36

Why doesn't the bash wget command work if the variable is taken from a file?

Good afternoon.
Just discovering the world of scripts.
I want to automate getting information from a large number of pages.
I am using the wget utility.
Everything works if I create a user variable:
for ip in 10.0.0.1 10.0.0.2 etc.
The task is to take the IP addresses (written in a column) from the hosts.txt file through the command

wget -qO- http://admin:[email protected]$ip/System |grep model.

If as a variable I set the output of ip addresses from a file in which they are written one by one in a column, then the command stops working.
Working option:
#!/bin/bash
file="hosts.txt"
for ip in 10.0.0.1 10.0.0.2
do
echo "Device on ip address $ip"
wget -qO- http://admin:[email protected]$ip/System |grep model
done

When I specify that the variable should be taken from the hosts.txt file, the script sends it to work.
#!/bin/bash
file="hosts.txt"
for ip in $(cat $file)
do
echo "Model for ip address $ip"
wget -qO- http://admin:[email protected]$ip/System |grep model
done

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AUser0, 2019-10-06
@wellspring

In echo, change $var to $ip, otherwise it's somehow illogical.
Before grep, put a pipe, that is, a vertical dash: |
And the rest - the code is quite working.

A
Andrey Barbolin, 2019-10-06
@dronmaxman

for ip in `cat hosts.txt`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question