Bash script to read file and alter lines depending on create date
Hey guys,
I need to have a bash script, but it's bothering me for days since I can't get it work. It needs to do the following: Read all user.conf files in a certain directory and it's subfolder (up to 1 level below). The script will then read the files and search for the following line: date_created=Mon Jan 01 00:00:00 0000 if, the current date is 1 month ahead of the data in the file. Or! The script looks at the files "last mofidied" date and check if it's older than 1 month then the script should continue with the following. Search for line: package=xxx and if it contains xxx then the script will change the following line: suspended=no to suspended=yes and saves the file. OK, so now for my second thing, I need a batch script to reverse the above to as before the change. I know this forum is not to script free scripts, and therefor; I am willing to pay. Also I would like to know if I don't get a loop when looking for the "last modified" date since the script changes the "modified date"! If you already have such a script (doubtable) you can always mail me or add me on msn. |
Hello and welcome to LQ.
I know this forum is not to script free scripts, and therefor; I am willing to pay. Actually it is and there's posted a few every day. If somebody asks nicely there's more than a few people willing to help. You should however show you're willing to invest yourself. In other words asking for (good: "I'd like help with X", bad: "Gimme X") handouts is considered reply unworthy and asking for help with homework is not allowed by the LQ Rules. Willingness to pay is even worse IMNSHO because that's not what the LQ Community is about. If you want to pay please consider contributing to LQ. That said I need to have a bash script, but it's bothering me for days since I can't get it work. posting what you got helps people help you quicker, easier. |
Ok that sounds fair unSpawn, The thing I already managed to get is to search for files, but that's off a tutorial posted here. I'm not trying to let others solve the problems, it's just way out of my league. LQ is a great support to the community (a community by itself) Really appreciate the things you do.
|
You can locate files prior to a certain day with the -mtime option of find. The first option could be -maxdepth <depth> to limit how many directory levels that find recurses.
If you extract the creating date from the file, use the "date" command with the -d '<extracted-date>' argument and the -s '%s' option to make subtraction easier. For example: Code:
echo $(( ( $(date +'%s') - $(date +'%s' -d 'Mon Jul 01 00:00:00 2007'))/60/60/24 )) man -t date | lpr I would highly recommend installing the source for the findutils package and producing a PostScript or PDF manual from the texinfo source to produce the "Finding Files" (find.pdf) manual. The find command is one of those swiss army knife commands that you can't do without. Extracting the dates from the files could be done in awk or sed. Grep is usually used to return a line containing the file. |
Ok, i now know to find the files, anyone else with some suggestions? I hate to be a bother, but i'd rather have someone make the script as i'm very new to this. Thanks in advance guys.
|
Code:
#!/bin/sh II. Jschiwal gave you some solid hints about stuff you should read up on. I can imagine it's a daunting for a scripting newbie, but how else could you validate or correct a script someone has offered you? It may even Do Stuff that it doesn't advertise. III. These could be helpful too: Code:
function help() { echo "Bash scripting guides: |
Thank you unspawn, it makes more sense to me now. Unfortunatly the script didn't work, as you've feared. I get the following error:
Code:
+ TARGETDIR=/usr/local/directadmin/data/users/ |
That's no error, that's *help*. It shows you what arguments you should provide.
|
Ok i'm getting closer. But it appears that the script doesn't change anything after 1 month is passed.
Code:
+ read FILEN |
Since you show persistence I rewrote the script a wee bit. (OK, the hardcoding part is stupid, I should use "getopt" instead but it'll do for now.) To see what changed save it and then "diff --side-by-side oldscript newscript | less". It does not remove the backups on failure but YMMV(VM) still.
Code:
#!/bin/sh |
Hmmm i get the following error:
Code:
telehosting3.sh: got in /usr/local/directadmin/data/users/testje/user.conf: changing: install: invalid option -- u Code:
grep -q ^package="${TARGET}" "${FILEN}" So i was wondering, isn't it possible to write the script like: find /usr/local/directadmin/data -name *user.conf* touch (the above) (open vi editor) ?date_created (compare to current date) some if and else statement which resolves to the below (if positive, 1 month old else die). Look for package=telehosting if it contains that package change suspended=no to suspended=yes :wq! and repeat for the next file in line. Unfortunatly i'm not a big star when it comes to vi I know it's possible to replace a line when it contains a match but the date thing is still blurry to me. |
Code:
#!/bin/bash - |
Thank you so much unspawn, that worked for me :) I'll see what I can do about supporting this great website/community. Again thank you.
|
Quote:
http://www.gnu.org/software/gawk/manual/ learn this: it 's worth to do you ll be glad of this powerful tool |
All times are GMT -5. The time now is 11:56 PM. |