shell scripting
hi
i'm trying to write a shell script that reports the amount of disks space used and available. but when i try to add them brings an error. Code:
df -h | grep /dev/hda1 | awk '{print $3}' i'm looking something that would see if the last letter is G or M and do accordingly. if i use Code:
df -h | grep /dev/hda1 | awk '{print $3}' | awk -F 'G| ' ' {print $1}' but what about when the disk goes to M ??? any thoughts appreciated |
Use df without -h, or with -k, so that the units are the same for each line.
|
true,
but if i use it like this when i divide them i get 6,14965 how i can keep only the 6,1 or the 6,14? thanx a lot |
Hi, mitsos.
One way is to use a little bit of c: Code:
#!/usr/bin/gawk -f Code:
% ./a1 /dev/null |
Hi.
There's also a stand-alone version of printf ... cheers, makyo Code:
#!/bin/bash Code:
% ./s1 |
I don't think you can do floating point calc in bash. Try Korn shell (#!/bin/ksh)
Code:
#!/bin/ksh Code:
1270824 k used |
thanx a lot for your replies
that helped a lot! |
Hi, naflan.
Your ksh is better than my pdksh. I'm using "@(#)PD KSH v5.2.14 99/07/13.2" and the arithmetic didn't seem to work. I hate it when that happens. So if I'm not using awk / perl , etc., I usually end up using bc. That makes scripts more cryptic unfortunately, but the upside is that it's portable. (In the adaptation of your script, I used other disks, since I don't have an hda.) ... cheers, makyo % cat s1 Code:
#!/bin/bash Code:
./s1 |
Hi.
I thought I had posted this, but perhaps I logged out before the final submit. My apologies if it got lost in the twisty pathways and just re-surfaced. This uses more of the ability of awk, since that was in use earlier, and I thought it useful to show an alternative ... cheers, makyo Code:
#!/bin/bash Code:
% ./s2 |
Just an alternative, in Python
Code:
import statvfs |
Hi, mitsos.
OK, with these versions, I hope you get the idea that there are lots of methods to solve a problem. Experimentation can be fun if you have the time to devote to it. Some observations: I liked to see that ksh is still in use, even if I couldn't get pdksh to work as did naflan. You don't always need to use additional scripting methods if you are OK using utilities like bc and printf. If you do employ other scripting languages, there are many to choose from. I often reach for perl, but the python that ghostdog74 posted was impressive in its brevity, and I liked that. I usually think of awk, perl, python, and shell as elderly, hackerly, orderly, and controllerly. No one posted a ruby script, but I suspect those will begin to show up in the future (and I'll need to think of a ly-word to describe that :) ). The script below is intended to show that one can have defaults for often-used cases, but allow the flexibility of calling with different arguments. Sometimes the environment might not allow that (for example, if you need to also run code on Windows), but if it does, one can make a tool useful and general. This is clearly not the only way to do this, but it's relatively simple: Code:
#!/bin/sh Code:
% ./s4 Code:
% ./s4 sda6 sda7 |
Heres `dfspace` from a shell implemetation on Solaris. It takes the thrill out of developing but is here ASIS. Note the definition of "Blksize" and "Mbyte" as an example. Note the comment. <Quote>Filesystem may be 1K bytes/block, but, df uses 512 bytes/block.<UnQuote>
end #!/bin/sh #ident "@(#)adm:dfspace 1.1.2.5" # # dfspace - d(isk) f(ree) space # Calculate the available disk space in all mounted filesystems # with the exception of pseudo file systems such as /proc and /dev/df. # # Alternately, report on filesystems/devices specified on cmd-line. # Filesystem may be 1K bytes/block, but, df uses 512 bytes/block. # /bin/df -t $* | awk ' BEGIN { FS=":"; free = -1; Blksize=512; Mbyte=1048576; CONST = Blksize / Mbyte } { if (free == -1) { # free is toggled every other line. split($1,fsptr,"("); FSYS=fsptr[1] if (NF == 3) { split($3,freeptr," "); free=freeptr[1]+0; } else { split($2,freeptr," "); free=freeptr[1]+0; } if( free == 0 && substr(freeptr[1],1,1) != "0" ) { free = -1; next; } next; } split($2,allocptr," "); alloc = allocptr[1]+0; if (alloc == 0) alloc = 1; # avoid division by zero if (free >= .005) { TFREE= (free * CONST) - .005; # force rounding down. } else { TFREE = (free * CONST); } if (alloc >= .005) { TALLOC= (alloc * CONST) - .005; # force rounding down. } else { TALLOC = (free * CONST); } PCT=free * 100 / alloc; if (TFREE <=0) TFREE=0; if (TALLOC <=0) TALLOC=0; if (FSYS !~ /^\/proc/ && FSYS !~ /^\/dev\/fd/) { if (PCT < 25.00) { printf("%c%c%c%c",27,91,49,109) }; printf ("%s: Disk space: %#6.2f MB of %#6.2f MB available (%#5.2f%%).\n", FSYS, TFREE, TALLOC, PCT); if (PCT < 25.00) { printf("%c%c%c%c",27,91,48,109) }; } Cumfree += free; Cumalloc += alloc; free = -1; # reset flag/variable for next set of two lines } END { if (Cumalloc > 0) { CumPct=Cumfree * 100 / Cumalloc; if ((Cumfree * CONST) >=.005) { Cumfree= (Cumfree * CONST) - .005; # force rounding down. } else { Cumfree= (Cumfree * CONST) ; } if ((Cumalloc * CONST) >= .005 ) { Cumalloc= (Cumalloc * CONST) - .005; # force rounding down. } else { Cumalloc= (Cumalloc * CONST) ; } printf ("\nTotal Disk Space: %#6.2f MB of %#6.2f MB available (%#5.2f%%).\n", Cumfree, Cumalloc, CumPct); } }' # end of disk space calculation. |
All times are GMT -5. The time now is 10:44 PM. |