1833642 Members
4819 Online
110062 Solutions
New Discussion

df script problem

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

df script problem

HI,
I would like to obtain the filesystem's allocated size, its usage and its number of free space. I'm reading a list of filesystems from a text file.

for i in `cat $1`
do
df -k|awk '{x=$2-$3}{print $7, $2, $3, x}'>> /tmp/restore/FSlist/$1.txt
grep -v "Mounted" /tmp/restore/FSlist/$1.txt
>> /tmp/restore/FSlist/$1.txt
done

I would like to remove the string "Mounted 1024 blocks Free 1024" on the file passwd by $1.txt and rewritting the same file with the removed string pattern.

However, the string above still appears in $1.txt file as:

Mounted 1024 blocks Free 1024
/ fs37/nwdv.crtr.1 17890 7887 888
... ( and the list continues with the same pattern appearing for each filesystem size displayed.

How do I remove the string "Mounted 1024 blocks Free 1024" from the file specified in $1?

Also I would need to insert the following attributes for each columns of the display:

Filesystem Disk-Allocated Disk-Used Disk-Free

Could someone show me how I could do this?

Thanks.
14 REPLIES 14
Michael Tully
Honored Contributor

Re: df script problem

What's wrong with the 'bdf' command??
Anyone for a Mutiny ?
Chern Jian Leaw
Regular Advisor

Re: df script problem

Michael,

I need to also run this script on an IBM AIX platform. I believe bdf does not exists on AIX platform.

Any other ideas?

Thanks.
steven Burgess_2
Honored Contributor

Re: df script problem

Hi Chern

Try

for i in `cat $1`
do
echo "Filesystem Disk-Allocated Disk-Used Disk-Free" >> >> /tmp/restore/FSlist/$1.txt
df -k|awk '{x=$2-$3}{print $7, $2, $3, x}'| grep -v Mounted >> tmp/restore/FSlist/$1.txt
done

I'm not at a workstation at the moment so can't test the results but give it a try

If it's not ok , post the results

HTH

Steve
take your time and think things through
Bart Beeren
Advisor

Re: df script problem

For removing the string "Mounted 1024 blocks Free 1024" you can do the following:

grep -v 'Mounted 1024 blocks Free 1024' FileThatHasToBeChanged > ChangedFile

This gives you a new file without the unwanted string

BB
Life isn´t as simple as it seems
Chern Jian Leaw
Regular Advisor

Re: df script problem

Steve, BB,

I've tried with both your ways, and it did eliminate the "Mounted 1024 blocks Free 1024" pattern and displays the format :
Filesystem Disk-Allocated Disk-Used Disk-Free

However, it displays the format:
Filesystem Disk-Allocated Disk-Used Disk-Free
at every output for each filesystem i.e:

Filesystem Disk-Allocated Disk-Used Disk-Free
/fs12/shark/ 577263 89877 90987
Filesystem Disk-Allocated Disk-Used Disk-Free
/fs12/shark.ct/ 577298 89877 90887
(and the pattern continues)

Also the output above gets repeated over and over again.

My script had been to modified as follows:
echo >>"Filesystem Disk-Allocated Disk-Used Disk-Free"
for i in `cat $1`
do
df -k|awk '{x=$2-$3}{print $7, $2, $3, x}'>> /tmp/restore/FSlist/$1.txt
grep -v "Mounted" /tmp/restore/FSlist/$1.txt
>> /tmp/restore/FSlist/$1.txt
done

Could someone point my mistake please? I need to solve it urgently.

Thanks.
steven Burgess_2
Honored Contributor

Re: df script problem

Hi Chern

Sorry I though you wanted each entry to have the header

Filesystem Disk-Allocated Disk-Used Disk-Free

Which I why I put the echo "Filesystem Disk-Allocated Disk-Used Disk-Free" >>

Inside the loop

Just take it outside of the loop before the
for i in `cat $1`

The only thing i can see wrong is you have created variable $i which is basically every entry in your $1

Try this

echo "Filesystem Disk-Allocated Disk-Used Disk-Free" >> /tmp/restore/FSlist/$1.txt

for i in $(cat $1)
do
df -k $i |awk '{x=$2-$3}{print $7, $2, $3, x}'>> /tmp/restore/FSlist/$1.txt
grep -v "Mounted" /tmp/restore/FSlist/$1.txt
>> /tmp/restore/FSlist/$1.txt
done

HTH

Steve




take your time and think things through
steven Burgess_2
Honored Contributor

Re: df script problem

Chern

Whoops! missed out the grep -v

for i in $(cat $1)
do
df -k $i |awk '{x=$2-$3}{print $7, $2, $3, x}'| grep -v mounted >> /tmp/restore/FSlist/$1.txt
done

HTH

Steve

take your time and think things through
Ralph Grothe
Honored Contributor

Re: df script problem

Hi Chern,

I don't want to get into your parsing problem details, as already the others have engaged in, nor do I want to dissuade you from writing a kind of generic shell script disk free stat.

Just would like to ask if Perl would be an option to you?
I experienced whenever I've had to write something generic which is supposed to run on a multitude of platforms the easiest sollution would call for a Perl script (Perl isn't only limitted to Unices, but also runs on Win32, Mac...)

Just to draw your attention to some useful CPAN modules for your kind of "problem".
Have a look at

http://www.cpan.org/modules/by-module/Filesys/Filesys-DiskFree-0.06.readme

or

http://www.cpan.org/modules/by-module/Filesys/Filesys-DiskSpace-0.05.readme

Madness, thy name is system administration
Chern Jian Leaw
Regular Advisor

Re: df script problem

Steve,
The output layout is better now, having the format:
Filesystem Disk-Allocated Disk-Used Disk-Free

and no more repeated outputs. However, instead of displaying details for a particular input file which contains a list of filesystems, it also displays the disk-usage of other filesystems which is not specified in the input file, but within the system.

The input file to $1 looks is as follows:
cat $1
/circuits/layout
/stor/physical
/stor/circuit_design

I'm running the script on a fileserver.

Any ideas on how I should solve it?

Thanks.



steven Burgess_2
Honored Contributor
Solution

Re: df script problem

Chern

Lets say the file that contains your filesystems is in /tmp called /tmp/fsystem

Then at the beginning of your script

FILES=/tmp/fsystem

for i in $(cat $FILES)
do
df -k $i |awk '{x=$2-$3}{print $7, $2, $3, x}'| grep -v mounted >> /tmp/restore/FSlist/$1.txt
done

That way the script should only look at the contents of your file /tmp/fsystem

HTH

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: df script problem

Chern

How did you get on with your script ? Did the last input work for you ?

Steve
take your time and think things through
H.Merijn Brand (procura
Honored Contributor

Re: df script problem

Use 'di' available from http://www.gentoo.com/di/di-3.6c.tar.gz runs on both AIX and HP-UX and is a wonderfull replacement for df and bdf.

If you want parsable output, use 'mi' instead (linked to 'di')
Enjoy, Have FUN! H.Merijn
Jordan Bean
Honored Contributor

Re: df script problem


I simply use

df -klP $(cat $1)

on both HPUX and Linux. I imagine it will also work on AIX and Solaris.

Sanjay_6
Honored Contributor

Re: df script problem

Hi,

Try some of the scripts mentioned / attached in the link below,

http://forums.itrc.hp.com/cm/QuestionAnswer/0,,0x026250011d20d6118ff40090279cd0f9,00.html

There are a lot of useful scripts in that link.

Hope this helps.

regds