Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executing commands with awk field

Status
Not open for further replies.

sinebubble

IS-IT--Management
Jun 13, 2002
9
US

How to get this to work? I want to execute a command on a field (du -adko $6 | sort -nr | head -10)...

df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk '{ if ( $5 > '$PAGE_NUMBER' ) { print "ALERT: "$6 " is " $5 "% full on "} else if ( $5 > '$MAIL_NUMBER' ) { print "WARNING: "$6 " is " $5 "% full on "; system(du -adko $6 | sort -nr | head -10);); } }'
 
Hi sinebubble,

I unravelled your code and it looked like this:

df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk '{
if ( $5 > '$PAGE_NUMBER' )
{
print "ALERT: "$6 " is " $5 "% full on "
}
else if ( $5 > '$MAIL_NUMBER' )
{
print "WARNING: "$6 " is " $5 "% full on ";
system(du -adko $6 | sort -nr | head -10);
);
}
}'


I think you have an extra close parenthesis-semicolon ");" kicking around.

I am also wondering if the system() call should be outside of the 'else if' clause.

Anyway, to answer your specific question about coding the system call, it looks to me like you just need some quotes:

system("du -adko " $6 " | sort -nr | head -10");


Hope this helps.
Grant
 
Hi sinebubble,

By the way, I am suspicious of the reference to $PAGE_NUMBER and $MAIL_NUMBER.

It looks like these are supposed to be shell variables. I can't test it right now, but I'm pretty sure you can't reference them directly from within awk. (Excuse me in advance if you know otherwise).

Here is how I think you might want your code to look:

df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk '{
if ( $5 > p_num )
{
print "ALERT: "$6 " is " $5 "% full on "
}
else if ( $5 > m_num )
{
print "WARNING: "$6 " is " $5 "% full on ";
}
system("du -adko " $6 " | sort -nr | head -10");
}' p_num=$PAGE_NUMBER m_num=$MAIL_NUMBER


Good luck,
Grant
 
Grant,
$PAGE_NUMBER and $MAIL_NUMBER are defined in the script (I'm only showing the serious part of the script). Anyway, I tried the below and it didn't execute the du command.

df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk
'{
if ( $5 > '$PAGE_NUMBER' )
{
print "ALERT: "$6 " is " $5 "% full on "
}
else if ( $5 > '$MAIL_NUMBER' )
{
print "WARNING: "$6 " is " $5 "% full on ";
system("du -adko "$6" | sort -nr | head -10");
}
}'
 
df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk '
{
if ( $5 > '$PAGE_NUMBER' )
{
print "ALERT: "$6 " is " $5 "% full on "
}
else if ( $5 > '$MAIL_NUMBER' )
{
print "WARNING: "$6 " is " $5 "% full on ";
system("du -adko "$6" | sort -nr | head -10");
}
}' vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hi sinebubble,

Do we know for sure that it is going into the 'else if' block so that it is reaching the system() call? ie:

else if ( $5 > '$MAIL_NUMBER' )
{
print &quot;WARNING: &quot;$6 &quot; is &quot; $5 &quot;% full on &quot;;
system(&quot;du -adko &quot;$6&quot; | sort -nr | head -10&quot;);
}

Also, assuming that it is making it to the system() call, what is the value of $6 at that moment?

Incidentally, it may be the Tek-Tips display window that is at fault here, but it doesn't look like the spacing is correct. It should be:
system(&quot;du -adko &quot; $6 &quot; | sort -nr | head -10&quot;);

Also, I am familiar with the 'du' command, but I don't recognize the '-adko' switches. Do you know for certain that they are valid switches?

Waiting for your reply...
Grant.
 
Grant, the awk portion of the script works fine and the du command I list also works, but I want to put them together and that is where it doesn't work. The $6 is a field containing the disk slice that I want to run du against.

Can I pass $6 to the system (du...) command?
 
Don't mean to but in:

awk ' BEGIN {
$6 = &quot;/home&quot;
system(&quot;du -aDklS &quot; $6 &quot;| sort -nr | head -n10 2>/dev/null&quot;);
}'

awk ' {
$6 = &quot;/home&quot;
system(&quot;du -aDklS &quot; $6 &quot;| sort -nr | head -n10 2>/dev/null&quot;);
}' < <(echo &quot;&quot;)

Both gave me reasonable output...

Maybe the problem is the file formatting of $6?
 
du -aDklS is Linux, perhaps? I'm running this under Solaris, so I use du -adko.

Anyway, running this under Solaris does not give the output of the system command:

#!/bin/sh

df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk '
{
if ( $5 > 10 )
{
print &quot;ALERT: &quot;$6 &quot; is &quot; $5 &quot;% full on &quot;
}
else if ( $5 > 5 )
{
print &quot;WARNING: &quot;$6 &quot; is &quot; $5 &quot;% full&quot;;
system(&quot;du -adko /var | sort -nr | head -10&quot;);
}
}'
 
Hi sinebubble,

I wonder if you have thought of trying it using getline, instead of system? Eg:


#!/bin/sh

df -k | grep -v cdrom | grep -v : | sed 1d | sed 's#%##' | awk '
{
if ( $5 > 10 )
{
print &quot;ALERT: &quot;$6 &quot; is &quot; $5 &quot;% full on &quot;
}
else if ( $5 > 5 )
{
print &quot;WARNING: &quot;$6 &quot; is &quot; $5 &quot;% full&quot;;
while( ( &quot;du -adko /var | sort -nr | head -10&quot; | getline cmdoutput ) > 0 )
{
print cmdoutput;
}
}
}'


Hope this helps.
Grant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top