0

I am quite new to bash scripting but I managed to write a script that works almost perfectly and I am really proud of myself. However, I have this problem which I dont get why it would not work. Part of my current code is this:

for begin in 1 51
do 
    echo -e "$begin"
    url="http://something&start-index=$begin&something"

    wget -O $HOME/Music/YoutubePlaylist/output$begin.txt $url 2>&1

    cat output1.txt > output.txt

    if ! [[ "$begin" = 1 ]]
        then
        cat output$begin.txt >> output.txt
    fi

done

It works perfectly.

However, if I add 101 to the first line, it wont work. Any idea why? What it actually does when I look at my temp files, is only write what is in output1.txt in output.txt and nothing else.

PS: output1.txt and output51.txt are not empty whereas output101.txt is empty. Maybe thats an hint....

2
  • 1
    please add "set -x" in the shell script before the for statement. This is the way to debug Shell Script Commented Feb 19, 2013 at 1:13
  • Did you try wgetting the URL manually for your "101" case? Commented Feb 19, 2013 at 5:28

2 Answers 2

1

cat output1.txt > output.txt is overwriting output.txt for every lap.

Do this instead (only correcting the issue with output.txt):

rm -f output.txt # if it should start with an empty file

for begin in 1 51
do 
  echo -e "$begin"
  url="http://something&start-index=$begin&something"

  wget -O $HOME/Music/YoutubePlaylist/output$begin.txt $url 2>&1

  cat output$begin.txt >> output.txt
done
Sign up to request clarification or add additional context in comments.

Comments

0

Such option should work all right:

for begin in 1 51 101
do
 echo -e "$begin"
 ...
done

Have you checked if wget -O $HOME/Music/YoutubePlaylist/output$begin.txt $url 2>&1 exists? Could you execute that from command line and check the result? e.g.

$wget -O $HOME/Music/YoutubePlaylist/output101.txt $url 2>&1

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.