[newbie] newbie script help
Greetings and Happy NewYear, I would like to write a script to accomplish a task, and then run it at regular intervals (I'll start another thread for that). The thing is I know zero about programming and in process of learning. I have a stand-alone firewall (mdk9.1) with no X installed, and a wireless card and my access to the internet is from a access point else where. I would like to get the info from /proc/net/wireless (on the firewall) like below. ~$ date cat /proc/net/wireless So after a while I came up with something like this below #! /bin/bash #Take wireless link readings from /proc/net/wireless and output to #file wireless_stat { date; cat /proc/net/wireless; } wireless_stat -- That seems to give me what I want and I like to set it up to run every 5 minutes with cron.hourly (next thread). Now every 24 hours I would like to take the file wireless_stat and tar/gzip it up and start a new one like below. ~$ tar -c -z -f wireless_stat1.tar wireless_stat then run ~$ rm -f wireless_stat; touch wireless_stat; How would I write a script that it would increment the the archived files like logrotate does? For instance. ~/tmp$ ll -rw-rw-r--1 mike mike 3084 Jan 4 08:44 wireless_stat -rw-rw-r--1 mike mike 359 Jan 3 19:33 wireless_stat1.tar Then 24 hours later this. ~/tmp$ ll -rw-rw-r--1 mike mike 3084 Jan 4 08:44 wireless_stat -rw-rw-r--1 mike mike 359 Jan 3 19:33 wireless_stat1.tar -rw-rw-r--1 mike mike 359 Jan 3 19:33 wireless_stat2.tar This where I'm getting stuck would this be a loop? or a statement? Would it be a (if, while, for,)? I have a book to help, but I'm afraid my brain is stuck in a (loop) of not understanding and can not progress... :-) Any guidance would appreciated, Mike Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [newbie] newbie script help
Hi Mike, Your script to 'logrotate' the files could look like this: #!/bin/sh filetest() { if [ -f wireless_stat$1.tar ] ; then mv -f wireless_stat$1.tar wireless_stat$2.tar fi } cd /directory where your files are filetest 4 5 filetest 3 4 filetest 2 3 filetest 1 2 The filetest function takes 2 arguments, being the oldest and one newer file (you can expand this check to as many as you like, but if you want to keep things around forever, this would not be the best way to do it). To run this thing, put it somewhere and let root's cron take care of things. Paul On 01/04/2004 05:05 PM, mike wrote: Greetings and Happy NewYear, I would like to write a script to accomplish a task, and then run it at regular intervals (I'll start another thread for that). The thing is I know zero about programming and in process of learning. Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [newbie] newbie script help
Paul wrote: Hi Mike, Your script to 'logrotate' the files could look like this: #!/bin/sh filetest() { if [ -f wireless_stat$1.tar ] ; then mv -f wireless_stat$1.tar wireless_stat$2.tar fi } cd /directory where your files are filetest 4 5 filetest 3 4 filetest 2 3 filetest 1 2 The filetest function takes 2 arguments, being the oldest and one newer file (you can expand this check to as many as you like, but if you want to keep things around forever, this would not be the best way to do it). To run this thing, put it somewhere and let root's cron take care of things. Paul Thanks Paul! I think I understand the function is to take the oldest which is the first argument if it exists rename it to the second argument. So it cd's to my file directory and sees if wireless_stat3.tar it would filetest 3 4 (rename file3.tar to file4.tar) and 2 to 3, 1 to 2. Yes, I was wondering how to keep it from running forever. I see now by what you have in your script. Now to put it all together could I do something like this? - #! /bin/bash tar -c -z -f wireless_stat1.tar wireless_stat something() { if [ -f wireless_stat1.tar ] ; then rm -f wireless_stat; touch wireless_stat; fi } filetest() { if [ -f wireless_stat$1.tar ] ; then mv -f wireless_stat$1.tar wireless_stat$2.tar fi } cd /home/mike/tmp filetest 4 5 filetest 3 4 filetest 2 3 filetest 1 2 - Mike Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [newbie] newbie script help
On 01/04/2004 07:30 PM, mike wrote: I think I understand the function is to take the oldest which is the first argument if it exists rename it to the second argument. Yup. So it cd's to my file directory and sees if wireless_stat3.tar it would filetest 3 4 (rename file3.tar to file4.tar) and 2 to 3, 1 to 2. Yes, I was wondering how to keep it from running forever. I see now by what you have in your script. Good! :) Now to put it all together could I do something like this? I'd suggest this: #--start script #! /bin/bash filetest() { if [ -f wireless_stat$1.tar ] ; then mv -f wireless_stat$1.tar wireless_stat$2.tar fi } cd /home/mike/tmp filetest 4 5 filetest 3 4 filetest 2 3 filetest 1 2 tar -c -z -f wireless_stat1.tar wireless_stat if [ -f wireless_stat1.tar ] ; then rm -f wireless_stat; touch wireless_stat; fi #--end script First you define the function (it is only defined, not run), then you cd to $HOME/tmp. Then cycle the backup tar-files. That way you move the existing backups out of the way, preserving the old #1 as #2 and getting rid of old #5 in the process. And then you build the new stat1.tar after which you create a clean stat-file. Do you see the logic in this? I hope this is clear enough. Good luck, Paul Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [newbie] newbie script help
Paul wrote: On 01/04/2004 07:30 PM, mike wrote: I think I understand the function is to take the oldest which is the first argument if it exists rename it to the second argument. Yup. So it cd's to my file directory and sees if wireless_stat3.tar it would filetest 3 4 (rename file3.tar to file4.tar) and 2 to 3, 1 to 2. Yes, I was wondering how to keep it from running forever. I see now by what you have in your script. Good! :) Now to put it all together could I do something like this? I'd suggest this: #--start script #! /bin/bash filetest() { if [ -f wireless_stat$1.tar ] ; then mv -f wireless_stat$1.tar wireless_stat$2.tar fi } cd /home/mike/tmp filetest 4 5 filetest 3 4 filetest 2 3 filetest 1 2 tar -c -z -f wireless_stat1.tar wireless_stat if [ -f wireless_stat1.tar ] ; then rm -f wireless_stat; touch wireless_stat; fi #--end script First you define the function (it is only defined, not run), then you cd to $HOME/tmp. Then cycle the backup tar-files. That way you move the existing backups out of the way, preserving the old #1 as #2 and getting rid of old #5 in the process. And then you build the new stat1.tar after which you create a clean stat-file. Do you see the logic in this? I believe I do now, I had things reversed, I should of taking care of moving my backups to make room for the new ones before deleting the first file, else lose data! Also I noticed that I was trying to make a function out a if statement ( something() ) which was not necessary, probably not right either *grin* And the #--startscript, #--endsript to know when the script begins and ends. I should also add some comments about what its doing for my sake. I hope this is clear enough. Yes, again thanks Paul! You walked me right through it, easier than I thought it would be. I'll probably post back on the running the cron part but, I'll research it a bit more before I panic. :-) Mike Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Cron (was Re: [newbie] newbie script help)
On 01/04/2004 08:32 PM, mike wrote: I'll probably post back on the running the cron part but, I'll research it a bit more before I panic. :-) Your friends there will be: man crontab man 5 crontab and perhaps also man cron It is not difficult, just something you need to get used to. su to root, and type 'crontab -e' to edit the crontab file. But first read up on how to define things. And make sure you know how to handle vi... Paul Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: Cron (was Re: [newbie] newbie script help)
Paul wrote: On 01/04/2004 08:32 PM, mike wrote: I'll probably post back on the running the cron part but, I'll research it a bit more before I panic. :-) Your friends there will be: man crontab man 5 crontab and perhaps also man cron It is not difficult, just something you need to get used to. su to root, and type 'crontab -e' to edit the crontab file. But first read up on how to define things. And make sure you know how to handle vi... For those who think life is too short for vi, there is also kcron. Sir Robin -- Certitude is possible for those who only own one encyclopedia. - Robert Anton Wilson Robin Turner IDMYO Bilkent Univeritesi Ankara 06533 Turkey www.bilkent.edu.tr/~robin Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: Cron (was Re: [newbie] newbie script help)
On Sunday 04 January 2004 22:00, robin wrote: For those who think life is too short for vi, there is also kcron. and Webmin Anne -- Registered Linux User No.293302 Have you visited http://twiki.mdklinuxfaq.org yet? Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com