[ NOTE: Warning ... a basic `awk' tutorial follows:

Chad, you have bitten off quite a big chew for starters.  It involves
shell scripting and knowledge of several unix tools.  This step by step
guide should get you started and produce an actual working script.
Beyond that you will need lots of practice and experimentation.

I've included no pointers to shell scripting URLS since there are
dozens on the internet ... Do a google search on `shell script
tutorial' or the like should turn up lots of hits.  The awk tool
discussed below has a full manual that is probably available on your
system under `info awk'.  If not, a google search for `awk manual'
will find one.

<[EMAIL PROTECTED]> writes:

> I am trying to write a script to modify a configuration script for 
> the jabber server. I am new to shell scripting and was wondering how 
> people would approach the following situations.

The standard unix tool `awk' can do this for you.  You'll need to
study `man awk' (or the more complete `info awk') and experiment to
get it just like you want it, but the examples below will be a big
start I hope.

The unix tool sed can do this too, but I'm more familiar with awk.

> 1. The host name is configured by the following xml tags
> 
> <host><jabberd:cmdline flag="h">localhost</jabberd:cmdline></host>
> <update><jabberd:cmdline flag="h">localhost</jabberd:cmdline></update>

> What is the best tool for replacing localhost or whatever is between 
> the two tags with the contents of the variable $server_name.

First you'll need to include some part of the subject lines between
forward slashes (Thats how awk uses regexp) so that awk will select the
correct lines:  It needs to be enough to make the lines unique to awk.

    1)    awk '/<jabberd:cmdline flag=\"h\">localhost/'

That may be enough or even more than enough if there are not similar
lines in the file.  (Notice I picked characters that are common to both
lines)

Since awk will print what it finds by default (if given no other
action) then at this point run that awk command and see if it prints
out the lines you are concerned with (just making sure we are in the
right ball park)

     2)  At the command line:
         awk '/<jabberd:cmdline flag=\"h\">localhost/' HTMLFILE <ENTER>   

(Where HTMLFILE is the name of the file containing the lines)

Then you need to tell awk what to do with these lines when it finds
them.  In this case you want to substitute the value of $server_name
for `localhost' [Note: if that value is in a shell var `$server_name'
then awk will need special quoting to recognize it (shown in following
examples ... I'll assume the script you are going to write has a line
at the top that says `server_name=WHATEVER' or that it is
passed in, in that format)

To substite things in awk there are built in functions `gsub' and
`sub'. `gsub' is for making substitutions globally, `sub' is for a
single occurence.  Here we'll proceed for a single occurence in each
line.  (Here is where the special quoting comes in. Note how
`$server_name is quoted. Also note I've escaped the double quotes
around "h" to avoid confusing the shell.)

Notice the backslash `\' at the end of the first line.  This is so awk
will know that the `sub' fucntion is to be executed againt the lines
selected by `/.../'.

        3) 
        awk '/<jabberd:cmdline flag=\"h\">localhost/\
         {sub (/localhost/, "'"$server_name"'");print $0}' HTML_FILE   

The syntax of a sub function is 
        sub (/REGEXP/,"REPLACEMENT",TARGET)
We didn't specify a target since the default is `$0' (the full line) and
that is what we are after

Now run that command and make sure it produces what you want.  Here it
gives this output, but first set the $server_name value in your shell
(xterm) by typing:

        server_name=WHATEVER

Then run the command like below:

    awk '/<jabberd:cmdline flag=\"h\">localhost/\
         {sub (/localhost/,  "'"$server_name"'");print $0}' HTML_FILE
    
Your shell will insert the secondary PS1 prompt like this:

   $ awk '/<jabberd:cmdline flag=\"h\">localhost/\
   >    {sub (/localhost/,  "'"$server_name"'");print $0}' HTML_FILE
  
        (you should see)

        <host><jabberd:cmdline flag="h">WHATEVER</jabberd:cmdline></host>
        <update><jabberd:cmdline flag="h">WHATEVER</jabberd:cmdline></update>

OK, half way home.......

Now we need to include the rest of the file in the output.  That is
done by telling awk to go to the next line and print like this:

        4)
    awk '/<jabberd:cmdline flag=\"h\">localhost/\
         {sub (/localhost/,  "'"$server_name"'");print $0;next}{print}' HTML_FILE
    
That should print out the whole file with the changes in place.

What remains to do is to replace HTML_FILE with this altered output.
This is done by having awk write the new output to a tmp file and move
the tmp file into place.  adding a line to your shell script that does
that job.

        5)
awk '/<jabberd:cmdline flag=\"h\">localhost/\
 {sub (/localhost/,  "'"$server_name"'");print $0;next}\
 {print}' HTML_FILE >  CHANGES.tmp  

mv CHANGES.tmp HTML_FILE


        6)
        Now to put all this into a script:

Shell scripts make use of the builtin numeric arguments ($1,$2,$3 etc)
to pass a file name or other variables from the command line to the
script.  You'll see how below

 Begin with a line pointing to your shell binary

cat Changes.sh

   #!/bin/sh
   ## Sample script to edit specific html files.
   ## Harry Putnam 01/25/01
   
   ## Set a variable to use in the replacements
   
   server_name=WHATEVER
   
   ## Run an awk command that uses the shell built in $1 to pass a file
   ## name for making  the changes and write them to at tmp file
   
   awk '/<jabberd:cmdline flag=\"h\">localhost/\
    {sub (/localhost/,  "'"$server_name"'");print $0;next}\
    {print}' $1 > CHANGES.tmp 
   
   ## Replace the original file with the edited version
   mv CHANGES.tmp $1

   ## done

This should get you started and give you plenty to think about.
Similar techniques can handle the rest of your posted queries.

If you have trouble .... repost with some details of the problem.



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to