3

I'm using ubuntu 11.04 and im trying to write a bash script that opens urls from a file each in a new tab with a sleep of 15 or so seconds, the file is 198 lines long and there is one url on each line. i have two potential methods of doing this

method one

while read line; do 
    firefox -new-tab "$line"
    sleep 15
done < file

method two

for line in $(cat file); do
    firefox -new-tab "$line"
    sleep 15
done

now both methods seem to give the same result, it opens firefox with the url of the first line and doesn't open any of the other urls until i close the browser, in which case it then opens up firefox with the url on the second line etc...

i have had some success with method one when i remove the sleep command it will try to open all of the urls at once in different tabs

i got the code for method one from: https://unix.stackexchange.com/questions/17659/opening-multiple-urls-from-a-text-file-as-different-tabs-in-firefox-chrome

and i got the code for method two from: http://whileonefork.blogspot.com/2011/02/bash-for-each-line-1-liners.html

2 Answers 2

6

Just try the following code:

while read line; do 
    firefox -new-tab "$line" & 2>/dev/null
    sleep 15
done < file
7
  • it works perfectly thanks man, but i dont understand the change you made, if its not to much to ask can you explain it Nov 8, 2011 at 5:06
  • 1
    The & operator puts command in background and free up your terminal.
    – chemila
    Nov 8, 2011 at 5:17
  • and the 2>/dev/null just puts all the output to somewhere it wont clog up? Nov 8, 2011 at 5:54
  • 2
    @hamsolo474 2>/dev/null redirects all error messages from stderr to /dev/null (actually hides them) Nov 8, 2011 at 13:35
  • 1
    2> redirects stderr, 1> (or just >) redirects stdout (any non-errors). & redirects nothing, it forces background execution of the previous command. Nov 9, 2011 at 0:04
0

For me both methods work if I just open browser window before starting the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.