I am doing an introduction course on UNIX - part of it is bash scripting. I seem to have understood the concepts, but in this particular problem I can't wrap my head around the issue.
I have a txt file that consists of 1 column with random usernames. That txt file is then used as a parameter for my bash script, that ideally uses the username to fetch a page and count the character count on that page. If the page gets fetched successfully, the character count is then saved along with a username in a different txt file.
Here is a code:
#!/bin/bash
filename=$1
while read username; do
curl -fs "http://example.website.domain/$username/index.html"
if [ $? -eq 0 ]
then
x=$(wc -m)
echo "$username $x" > output.txt
else
echo "The page doesn't exist"
fi
done < $filename
Now the problem I have here is that after one successful fetch, it counts the characters, outputs them to the file and just finishes the loop and exits the program. If I remove specifically "wc -m" bit, the code runs perfectly fine.
Q: Is that supposed to happen, how should I go around that to achieve my goal? Or have I made a mistake somewhere else?