PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > execute command inside awk
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

execute command inside awk

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2006, 17h06   #1
relikwie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut execute command inside awk

Hi, below a code snippet that checks filesize and reports on this.

ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
print $9,"Less than 1.5 Gbytes",$5 ;
else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
} > result.txt

How can I log the output and set a parameter if size > 1.5Gb, so I can
take further action in the script.
Mail a warning.

I've been playing around, but just can grasp awk.

ty.

  Réponse avec citation
Vieux 05/11/2006, 17h41   #2
koneruarjun@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk

one of the simplest ways:
VAR=`your awk command shown below `
parse ${VAR} as needed
---Arjun
relikwie wrote:
> Hi, below a code snippet that checks filesize and reports on this.
>
> ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
> print $9,"Less than 1.5 Gbytes",$5 ;
> else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
> } > result.txt
>
> How can I log the output and set a parameter if size > 1.5Gb, so I can
> take further action in the script.
> Mail a warning.
>
> I've been playing around, but just can grasp awk.
>
> ty.


  Réponse avec citation
Vieux 05/11/2006, 17h44   #3
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk

2006-11-5, 09:06(-08), relikwie:
> Hi, below a code snippet that checks filesize and reports on this.
>
> ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
> print $9,"Less than 1.5 Gbytes",$5 ;
> else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
> } > result.txt
>
> How can I log the output and set a parameter if size > 1.5Gb, so I can
> take further action in the script.
> Mail a warning.

[...]


Let awk's exit status reflect whether the condition is met or
not:

if
ls -dLl "/${FILE}" | awk '
{
if ($5 < 1.5 * 1024 * 1024 * 1024) {
print $9,"Less than 1.5 Gbytes",$5 ;
exit(0)
} else {
print $9,"GREATER than 1.5 Gbytes - ",$5
exit(1)
}
}' > result.txt
then
action if less than 1.5 GB
else
action if at least 1.5 GB
fi

--
Stéphane
  Réponse avec citation
Vieux 05/11/2006, 17h48   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk

relikwie wrote:

> Hi, below a code snippet that checks filesize and reports on this.
>
> ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
> print $9,"Less than 1.5 Gbytes",$5 ;
> else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
> } > result.txt


ITYM:

ls -Ll "/${FILE}" |
awk '{ print $9,($5 < 1610612736 ? "Less" : "GREATER") " than 1.5
Gbytes",$5}' > result.txt

> How can I log the output and set a parameter if size > 1.5Gb, so I can
> take further action in the script.


Do you really mean "parameter" or do you mean "variable"?

> Mail a warning.


You've got shell calling awk and you're now saying you want to have awk
calling shell to send email, i.e. shell->awk->shell. You might as well
just stay in shell if that comparison is all you're doing. If you're
doing something else, let us know...

Ed.
  Réponse avec citation
Vieux 05/11/2006, 18h11   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk

2006-11-05, 11:48(-06), Ed Morton:
[...]
>> How can I log the output and set a parameter if size > 1.5Gb, so I can
>> take further action in the script.

>
> Do you really mean "parameter" or do you mean "variable"?


A variable is a special kind of parameter in the terminology
used by most shell manuals.

$? would be a parameter but not a variable for instance.

The solution I gave sets $?, another poster suggested to use a
variable $VAR to collect awk's output.

--
Stéphane
  Réponse avec citation
Vieux 05/11/2006, 18h27   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk

Stephane CHAZELAS wrote:
> 2006-11-05, 11:48(-06), Ed Morton:
> [...]
>
>>>How can I log the output and set a parameter if size > 1.5Gb, so I can
>>>take further action in the script.

>>
>>Do you really mean "parameter" or do you mean "variable"?

>
>
> A variable is a special kind of parameter in the terminology
> used by most shell manuals.


Right, so did the OP mean "on invoking awk, set a parameter to indicate
the size to be tested" or "inside awk, set a variable after the test to
be used in further processing"? The answer to my question would tell us.

> $? would be a parameter but not a variable for instance.
>
> The solution I gave sets $?, another poster suggested to use a
> variable $VAR to collect awk's output.


Right, and I suspect neither of those answers the OPs actual question.

Ed.
  Réponse avec citation
Vieux 05/11/2006, 18h56   #7
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk

On 2006-11-05, relikwie wrote:
> Hi, below a code snippet that checks filesize and reports on this.
>
> ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
> print $9,"Less than 1.5 Gbytes",$5 ;
> else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
> } > result.txt
>
> How can I log the output and set a parameter if size > 1.5Gb, so I can
> take further action in the script.


Why use awk?

set -- $( ls -Ll "/${FILE}" ) ## Do you really want the slash?
size=$5
if [ $size -lt 1610612736 ]
then
printf "%s: %d\n" "Less than 1.5 Gbytes" "$size"
true
else
printf "%s: %d\n" "Greater than or equal to 1.5 Gbytes" "$size"
false
fi > result.txt

Now you can check $? for success or failure.

> Mail a warning.
>
> I've been playing around, but just can grasp awk.
>
> ty.
>



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 05/11/2006, 19h08   #8
relikwie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: execute command inside awk


Stephane CHAZELAS schreef:

> 2006-11-5, 09:06(-08), relikwie:
> > Hi, below a code snippet that checks filesize and reports on this.
> >
> > ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
> > print $9,"Less than 1.5 Gbytes",$5 ;
> > else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
> > } > result.txt
> >
> > How can I log the output and set a parameter if size > 1.5Gb, so I can
> > take further action in the script.
> > Mail a warning.

> [...]
>
>
> Let awk's exit status reflect whether the condition is met or
> not:
>
> if
> ls -dLl "/${FILE}" | awk '
> {
> if ($5 < 1.5 * 1024 * 1024 * 1024) {
> print $9,"Less than 1.5 Gbytes",$5 ;
> exit(0)
> } else {
> print $9,"GREATER than 1.5 Gbytes - ",$5
> exit(1)
> }
> }' > result.txt
> then
> action if less than 1.5 GB
> else
> action if at least 1.5 GB
> fi


Stephane, thanks! Worked like a charm.

If I look at your adjustments, I also could have done it like this?:

ls -Ll /${FILE} | awk '{ if($5 < 1610612736)
print $9,"Less than 1.5 Gbytes",$5 ;
exit(0)
else print $9,"GREATER than 1.5 Gbytes - ",$5 }'
exit(1)
} > result.txt

if [[ $? -gt 0 ]]; then GREATER; warn; fi

anyhow envy you guys :|

ty.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13238 seconds with 16 queries