|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|