LucidTruth
LucidTruth

Reputation: 13

New to bash scripting, writing wget script

This is the first time I have ever tried to code anything, and now I would like some help.

I get a syntax error, but don't know where it is. Can you please look at my code and tell me what I need to fix the syntax, and what I need to improve this script?

#!/bin/bash

echo -e "Please Input Website To Get URLS and IPs" 

while read line do wget $line -O $line.txt -o /dev/null ls -l $line.txt

grep "href=" $line.txt | cat -d"/" -f3 |grep $line |sort -u > $line-srv.txt

for hostname in $(cat $line-srv.txt);do host $hostname |grep "has adress"

done

Upvotes: 0

Views: 166

Answers (2)

konsolebox
konsolebox

Reputation: 75588

You might find this simpler:

#!/bin/bash
while read -p "Please Input Website To Get URLS and IP (CTRL-D to exit): " TARGET || { echo >&2; false; }; do
    wget -O - -o "/dev/null" "$TARGET" | grep -Po '(?<=://)[^/]+' | grep "$TARGET"
done | sort -u | xargs -r host | grep 'has address'

Try to run it and give a comment about how your expected function compares to it.

Or another form:

#!/bin/bash
while read -p "Please Input Website To Get URLS and IP (CTRL-D to exit): " TARGET || { echo >&2; false; }; do
    wget -O - -o "/dev/null" "$TARGET" | grep -Po '(?<=://)[^/]+' | grep "$TARGET" | sort -u | xargs -r host | grep 'has address'
done

Upvotes: 0

EJK
EJK

Reputation: 12534

You are missing a 2nd "done". You have only terminated one of your while loops.

Consistent indentation would catch this. That is, if you indent everything within your loop, then it would be much more obvious that something is missing. E.g.

echo -e "Please Input Website To Get URLS and IPs" 

while read line
do
     wget $line -O $line.txt -o /dev/null
     ls -l $line.txt

     grep "href=" $line.txt | cat -d"/" -f3 |grep $line |sort -u > $line-srv.txt

     for hostname in $(cat $line-srv.txt);do
        host $hostname |grep "has adress"
     done

Upvotes: 1

Related Questions