![]() |
Reading and writing a file with bash
Hi all,
this is my first post so I would first like to say hi to everyone before asking my question... I am in the beginning of learning bash scripting and haven't really gotten the hang of reading and writing files. What I am trying to create is a bash script that by crontab is run every hour, it then checks a text file for links, wget's them all and then marks them as done. Let's say I have this textfile /home/user/urls : http://www.example.com/file_1.zip #http://www.example.com/file_2.zip http://www.example.com/file_3.zip #http://www.example.com/file_4.zip http://www.example.com/file_5.zip Then my bash script is in /home/user/bash/wget.sh : exec < /home/user/urls while read line do echo $line done That is all I have done so it just prints the line, and I could of course just make it wget $line but I thought I want some more working before I do that. Now what I was thinking is that it opens the file, loops through every line and on the lines not starting with # it does wget (the line). After it has downloaded the url it adds a # before that line and continues the loop. This would make it so if I wanted a file downloaded I would just put the url in this file and the script would within an hour (or any other crontab time) download the file and add a # first on the line so I know it's done. Any help would be great :) Thanks... |
Something along this perhaps:
Code:
#!/bin/sh |
Quote:
It works just like I wanted it to and I can now add it to my crontab. |
Next on your list should be to understand this bit of shell script completely :-)
Eric |
|
Got another problem with this, more related to crontab though.
If I add this to crontab like this : */30 * * * * /home/user/bash/wget.sh Then it starts the bash script every 30 minutes but the conflict comes if the first download isn't done by the second start. I recently had three /home/user/bash/wget.sh and three wget of the same file running at the same time. I guess this could create a few errors in the file or just a bunch of wgets running on the same file all the time. Anyone have any idea on how to solve this? |
Quote:
Code:
#!/bin/sh |
AFAIK you don't even need a lock, there's enough envvars to go on I think: cronjobs run at a certain $SHLVL, share the $PPID ($PID of cron daemon) and if you 'pgrep -f 'cron.*/path/tojobname'' the amount of $PIDs should be exactly one plus the $PID should be equal to $$, plus fuser should show only cronjob wget process $PID accessing the wgetfile.
|
| All times are GMT -5. The time now is 03:45 AM. |