Tuesday, September 26, 2023
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 6417  / 2 Years ago, fri, december 10, 2021, 9:08:11

I have a text file



Google.com
Youtube.com
Gmail.com
Yahoo.com


I am trying to open them with Lynx. Here is the thesis.



I will store all of those links at a txt file, say links.txt and then I need to open them with Lynx and then terminate Lynx by using kill.



Here the code I wrote but its not correct



for i in links.txt
do
lynx $i
sleep 10
pkill lynx
done


Whats wrong here?


More From » bash

 Answers
1

After several iterations...



for url in $(cat links.txt); do
timeout 10 lynx "$url"
done


Lynx is blocking (and has to be to work) so sleeping doesn't work properly and it also tries to nab stdin which makes piping things "interestingly" difficult. See here for iterating lines in a file.



Lynx can be a bit annoying with its prompts for allowing cookies. You can either change its settings if it's a problem or you can pass in the -accept_all_cookies flag, like so:



for url in $(cat links.txt); do
timeout 10 lynx -accept_all_cookies "$url"
done


Today I learned about the timeout command, so I'm happy.






To print a status at the end, the only way I can see how is to check the URL is okay separately, like so:



for url in $(cat links.txt); do
timeout 10 lynx -accept_all_cookies "$url"
if [[ $(curl -o /dev/null --silent --head --write-out '%{http_code}
' "$url") -eq "200" ]]; then
echo "Getting $url successful"
else
echo "Getting $url unsuccessful"
fi
done

[#29079] Sunday, December 12, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ormlai

Total Points: 292
Total Questions: 106
Total Answers: 115

Location: Cape Verde
Member since Fri, Sep 16, 2022
1 Year ago
ormlai questions
Wed, Sep 28, 22, 00:17, 1 Year ago
Sun, Aug 7, 22, 22:05, 1 Year ago
Wed, Jun 16, 21, 03:50, 2 Years ago
Sun, Feb 6, 22, 09:11, 2 Years ago
Mon, Jul 12, 21, 10:00, 2 Years ago
;