I'm having issues with a shell script I'm working on that for whatever reason is not working as intended. The bulk of the script, and where things are going wrong, is below:
for username in $list
do
sleep $sleep
VAL=`curl -H 'Client-ID: $clientid' -s $host$username | jq -r .message | grep -E "unavailable|not found"`
if [[ $VAL == *"not found"* ]]; then
echo "$username is available"
echo "$username" >> available.names
else
echo -e "$username is reserved"
fi
done
I realize there are some variables such as sleep, host, clientid, and username but to give you an idea of what I'm working with, the result of the curl command ran at the VAL line (minus the grep) would be something like this:
User "username" was not found
or
User "username" is unavailable
So including the pipe to grep -E, the result of VAL would simply be something like:
not found
Now according to this StackOverflow answer, I should be able to wildcards so that, when using my script, if the VAL contains "not found" then it should echo that the username is available. As I've done here:
[[ $VAL == *"not found"* ]]
However what it's doing is the else statement for all usernames that are being checked. So even ones where the VAL contains "not found", it's still echoing that the username is taken.
Does anyone see anything wrong with my script that would cause this to happen? I've looked it over about 100 times and I'm getting no errors to help troubleshoot, only that it's not working as intended. Any help would be appreciated.
-Hstring, which prevents variables from being expanded. Use double quotes.for username in $listis a code smell -- it indicates that you're trying to store a list in a string rather than using a native array type. A string is the wrong tool for the job -- not only does it restrict what values you can store, but it also means that if you have a value of*it'll be replaced with a list of files in the current directory. Use an array instead:list=( allison bob charlie ); for username in "${list[@]}"; do ...bash -x yourscriptto log every command run -- that way if (for example) an expansion isn't taking place somewhere you expect it to, you can see it in the log and ask a question about that one specific line/operation.