LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-31-2008, 09:48 PM   #1
splunk
Member
 
Registered: Jan 2007
Location: PA, USA
Distribution: Ubuntu Jaunty (9.04)
Posts: 31

Rep: Reputation: 16
Run commands found in plain text file


I have a backup script the gets the list of files and directories to backup from a separate file (named dailyfilelist). Something I am trying to do is have the backup script run commands found in 'dailyfilelist'.

Sample dailyfilelist:
/a/path/to/here
/some/dir/$(uname -r)
/etc/init.d
/a/tar/file/from/somewhere/$(hostname)_$(date +%Y_%b_%d).tar.gz

I know how to read each line one by one but am having trouble getting the backup script to properly handle the lines that have a command in them.

What I want to happen
Code:
1) Backup script reads contents of dailyfilelist one line at a time
2a) Check if line has any commands in it.
2b) Run any commands found in line and modify line with command output
   "/some/dir/$(uname -r)" should then be "/some/dir/2.6.23.9"
   "/a/path/to/$(hostname)_$(date +%Y_%b_%d).tar.gz" should then be "/a/path/to/myserver_2008_Jan_31.tar.gz"
3) Write (modified) line to temp file
After the above the temp file will look like this:
/a/path/to/somewhere
/some/dir/2.6.23.9
/etc/init.d
/a/tar/file/from/somewhere/myserver_2008_Jan_31.tar.gz

The backup script will then read the temp file to get the actuall list of files and directories to backup

How many different ways can I accomplish this task?

Thanks for reading this far
 
Old 01-31-2008, 10:52 PM   #2
budword
Member
 
Registered: Apr 2003
Location: Wisconsin
Distribution: Switched to regualr Ubuntu, because I don't like KDE4, at all. Looks like vista on crack.....
Posts: 675

Rep: Reputation: 31
Errr....I'm a little confused. Why don't you just have a simple shell script back up the files you want backed up ? Why do you want to use two files ? And have one grabbing commands and info from the other one ? A simple bash script can do exactly what you want to do, unless I'm completely misunderstanding what you are up to. So...ummm....up to anything weird ?

David
 
Old 01-31-2008, 10:54 PM   #3
splunk
Member
 
Registered: Jan 2007
Location: PA, USA
Distribution: Ubuntu Jaunty (9.04)
Posts: 31

Original Poster
Rep: Reputation: 16
Getting closer.

This script uses awk to filter out the commands.

Code:
#!/bin/bash

awk ' BEGIN{FS="[,]"}
{
  for ( i=1 ; i<=NF;i++ ){
    if ( match($i,"uname -r") ) {
         n=split( substr($i , RSTART) , a,")")
         print a[1],a[2]
    }else if ( match($i,"hostname") ) {
         n=split( substr($i , RSTART) , a,")")
         print a[1],a[2]
    }else if ( match($i, "date +%Y_%b_%d") ) {
         n=split( substr($i , RSTSRT) , a,")")
         print a[1],a[2]
    }
  }
}' "dailyfile"
Contents of dailyfilelist:
/a/path/to/here
/some/dir/$(uname -r)
/etc/init.d
/a/path/to/$(hostname)_$(date +%Y_%b_%d).tar.gz

Script output is:
uname -r
hostname _$(date +%Y_%b_%d

Script output should be:
uname -r
hostname
date +%Y_%b_%d

Now I need to figure out how to run those commands and add the command output to the string. Also note that some lines will have multiple commands in them.
 
Old 01-31-2008, 10:55 PM   #4
splunk
Member
 
Registered: Jan 2007
Location: PA, USA
Distribution: Ubuntu Jaunty (9.04)
Posts: 31

Original Poster
Rep: Reputation: 16
@budword

The backup script will be used by multiple servers. Each server has different files and directories that need to be backed up, hence the separate files.
 
Old 01-31-2008, 11:11 PM   #5
budword
Member
 
Registered: Apr 2003
Location: Wisconsin
Distribution: Switched to regualr Ubuntu, because I don't like KDE4, at all. Looks like vista on crack.....
Posts: 675

Rep: Reputation: 31
I'm afraid I still don't see why you need 2 files. If one of the files needs to be specific to each server, because each server needs different files backed up, why not just do one script per a server ? It would be much easier. Unless there is something else I'm missing ?

David

P.S. Did someone force a perl hacker to come up with this as a bash script ?

Hi, I found this....My backup needs aren't this complex, but this might be just the sort of thing you need.

http://sourceforge.net/project/downl...ar.gz&25707427

David

Last edited by budword; 01-31-2008 at 11:28 PM.
 
Old 02-01-2008, 12:52 PM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

I ran across this the other day:
Quote:
OSSP rc is a generic run-command processor. Its primary function is to assemble a temporary script from excerpts of one or more run-commmand [sic] files which are built out of text snippets grouped into sections. The user specifies the desired parts to use and also controls the order of assembly. The program is not tied to a particular syntax for neither the run-command section tags nor the contained scripts. By default, the assembled script is executed by a specified interpreter (usually a shell), but it can also be written into a temporary file for evaluation inside a calling shell, or even just printed to stdout for further processing.

-- part of toolset at http://www.ossp.org/pkg/tool/
Best wishes ... cheers, makyo
 
Old 02-02-2008, 11:15 AM   #7
splunk
Member
 
Registered: Jan 2007
Location: PA, USA
Distribution: Ubuntu Jaunty (9.04)
Posts: 31

Original Poster
Rep: Reputation: 16
Very close to solution

The script below will parse each line in the file 'dailyfile' and run the first command one each line. The commands must be enclosed in backticks (`).

Code:
#!/bin/bash

awk ' BEGIN{FS="[\n]"}
{
  for ( i=1 ; i<=NF;i++ ){
    if ( match($i,"`" ) ) {
         n=split( substr($i , RSTART) , a,"`")
         system(a[2])
    }
  }
}' "dailyfile"
Contents of 'dailyfile'
Code:
/a/path/to/here
/some/dir/`uname -r`
/etc/init.d
/a/path/to/`hostname`_`date +%Y_%b_%d`.tar.gz
One problem remaining is the last line in 'dailyfile'. There are 2 commands on this line but only the first command (`hostname`) gets run.

Hope this helps someone
 
  


Reply

Tags
backup, config, parse


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
.ram file shows as text/plain biosnacky Linux - Newbie 8 01-07-2007 08:21 PM
CMS for plain text rblampain Linux - Software 3 12-14-2005 10:40 PM
not a plain text file wazza4610 Linux - Newbie 1 11-22-2005 04:20 AM
cannot execute some commands as an plain user! minike Slackware 5 09-03-2004 06:34 PM


All times are GMT -5. The time now is 04:50 PM.

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