awk - user function?

Here's a snippit from a script I'm working on

srvctl config database | \
awk -F\" '{print "srvctl config database -d " $1 " -a" \
         RS "echo =================================================="}' \
    > /tmp/x$$.sh
chmod 777 /tmp/x$$.sh
/tmp/x$$.sh

As you can see, I'm piping the output of' 'srvctl' to awk, where I construct other commands that get written to a file, then execute that file. It occurs that I should be able to eliminate that intermediate file and execute the commands directly from awk, but after struggling with some of the on-line tutorials as well as my trusty Unix in a Nutshell, I'm coming up dry. Here's my latest attempt ...

srvctl config database | \
awk -F\" 'function rptcfg(dbsid) {srvctl config database -d dbsid -a} \
          {print rptcfg($1) \
           RS "echo =================================================="}'

But it only produces a blank line for each of the (2) results from the 'srvctl' command:

echo ==================================================

echo ==================================================

you may try with system function in awk.

Closer, but it appears that it is trying to do that in a secondary environment that does not inherit the first:

srvctl config database | \
awk -F\" '{system(srvctl config database -d $1 -a)} \
          {print  \
           RS "echo =================================================="}'

results in

sh: 00: command not found

echo ==================================================
sh: 00: command not found

echo ==================================================

You might be able to just use man xargs (linux).

Something like:

srvctl config database | awk -F\" '{print $1}' | xargs -I%SUB% echo srvctl config database -d %SUB% -a

The echo is just so you can check if it works first.

You're making this way to hard. If your script above works, the following should work just as well without using the temp file:

srvctl config database |
awk -F\" '{print "srvctl config database -d " $1 " -a" \
         RS "echo =================================================="}' |
sh
1 Like

Closer yet. Now how do I get my break line ("==================" back in?

---------- Post updated at 12:03 PM ---------- Previous update was at 12:01 PM ----------

I like the elegance, but that has the same problem as the suggestion from Pamu ..

So run your original script and show us the contents of /tmp/x$$.sh when it is working.

What operating system are you using. (I.e., what is the output from the command uname -a ).

I think you'd need to run it in a subshell:

srvctl config database | awk -F\" '{print $1}' | xargs -I%SUB% sh -c 'srvctl config database -d %SUB% -a; echo ============;'

(or just write a 2-line script for xargs to call instead) but if you're having an issue with the environment not being inherited then you're going to have the same problem as with the other suggested solutions.

EDIT: Although, if inheriting the environment is actually the problem then executing your original /tmp/x$$.sh shouldn't work either.

pamu's suggestion will work if you put the system commands (except the $1) in double quotes. Run Don Cragun's proposal with sh -x and post the resulting output.

Sorry, I should have stated the OS version up front.
Oracle Linux 5.6 x86-64:

oracle:$ uname -a
Linux vbxxxxx.vbdomain 2.6.18-238.1.1.0.1.el5 #1 SMP Thu Jan 20 23:52:48 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

The intermediate file looks like this:

oracle:$ ls -ltr /tmp/x*.sh
-rwxrwxrwx 1 oracle oinstall 187 Oct  4 14:43 /tmp/x17412.sh

2013-10-04 14:44:45
oracle:$ cat /tmp/x17412.sh
srvctl config database -d zzzzzzvb -a
echo ==================================================
srvctl config database -d bbbbbbvb -a
echo ==================================================

What's the output from running Don's suggestion with sh -x ?

1 Like

Following on the rest of the suggestions and revisiting this solution, the addition of the '-x' switch on the 'sh' command was the key. Also had to redirect errout to the bit bucket, else the commands themselves ('srvctl' and 'echo')were also included in the ouput. Final solution is

srvctl config database | \
awk -F\" '{print "srvctl config database -d " $1 " -a" \
        RS "echo =================================================="}' | \
      sh -x 2>/dev/null

I have a couple of other scripts I can go back and retrofit to include this technique as well. Plus the increase in my own understanding.

Thanks to everyone for participating.

All -x does is turn on command trace. It shouldn't make any difference to the execution.