'Whois scraping bash script—if clause errors

I wrote a fairly straightforward script to map out what one-word domains are still available. I'm getting an error in line 7, the if clause, but I can't see anything wrong with it.

#!/bin/bash

for i in $(cat /usr/share/dict/words);
do
        i="$i.com"
        echo $i processing `date`
        if [ $(whois $i) == "*No match for*" ]
        then
                echo $i AVAILABLE
#               echo "$i">>domains.available.`date "+%Y%m%d"`
        else
                echo $i unavailable
#               echo "$i">>domains.unavailable.`date "+%Y%m%d"`
        fi
done

I looked up similarly straightforward scripts online, and they seem syntactically identical.



Solution 1:[1]

Quote the argument:

if [ "$(whois $i)" == "*No match for*" ]

Otherwise all the words of the response will be split into separate arguments to the test command.

Or you could use the built-in conditional operator. It doesn't do word splitting of variables or command substitutions.

if [[ $(whois $i) == "*No match for*" ]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barmar