LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-28-2011, 01:01 PM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,444

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829

How about using the trap mechanism:
Code:
#!/bin/bash

FILE="$1"

onexit()
{
    grep -vFf downloaded-links.txt "$FILE" > remaining-links.txt \
      && [ $? -le 1 ] && mv remaining-links.txt "$FILE"
    exit
}

trap onexit EXIT

while read -r LINE
do
    if axel --alternate --num-connections=6 "$LINE"; then
	echo
	echo "Succeeded downloading following link $LINE"
	echo "$LINE" >> downloaded-links.txt
	echo
    else
	echo
	echo "Failed downloading following link $LINE"
	echo "$LINE" >> failed-links.txt
	echo
    fi
done < "$FILE"
 
Old 04-28-2011, 10:11 PM   #17
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
@ntubski - So I understand how you are waiting till exit to make the changes, but I believe the bigger issue is that the FILE you are reading
from will be actively changed, according to OP, whilst your loop is running. So in turn your script will not be aware of changes until the script is started again.
 
Old 04-29-2011, 12:44 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,444

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
Oops, I was confused, second try:
Code:
#!/bin/bash

FILE="$1"

cp "$FILE" remaining-links.txt
touch downloaded-links.txt

while [ -s remaining-links.txt ]
do
    while read -r LINE
    do
        if axel --alternate --num-connections=6 "$LINE"; then
	    echo
	    echo "Succeeded downloading following link $LINE"
	    echo "$LINE" >> downloaded-links.txt
	    echo
        else
	    echo
	    echo "Failed downloading following link $LINE"
	    echo "$LINE" >> failed-links.txt
	    echo
        fi

        if ! diff -q remaining-links.txt "$FILE" ; then
            break
        fi
    done < remaining-links.txt

    grep -vFf downloaded-links.txt "$FILE" > remaining-links.txt

done
 
Old 04-29-2011, 11:52 PM   #19
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
Yes that looks like a better alternative This appears to address my concerns at least ... let us see what the OP thinks?
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
BASH script to move files/folders to either the .Trash file or to another direcotry bettsy583 Linux - Newbie 13 12-03-2010 03:41 AM
Bash script only appears to process one line of file Nylex Programming 5 08-19-2007 10:35 PM
bash script for displaying a file one line at a time. panchosansa Programming 6 10-12-2006 10:54 AM


All times are GMT -5. The time now is 12:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration