Df -h results in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Df -h results in a loop
# 8  
Old 05-20-2016
To explain how this works, this doesn't avoid the subshell problem, as much as put the entire relevant program inside the subshell so nothing loses context.

Last edited by Corona688; 05-20-2016 at 12:58 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 05-20-2016
Quote:
Originally Posted by RudiC
Code:
read REST; while read FS SZ AV US PC MT REST

I did not get this part of code
# 10  
Old 05-21-2016
Quote:
Originally Posted by Abu Rayane
I did not get this part of code
Code:
read REST; while read FS SZ AV US PC MT REST

The read REST; takes care of disregarding the header (first line or unwanted information):
Code:
Filesystem      Size  Used Avail Use% Mounted on

while read FS SZ AV US PC MT REST takes care of dividing the disk information for the mount point.
# 11  
Old 05-21-2016
so these ones are FS (filesystem) SZ (size) AV (avalaible) US (used) PC (????) MT (mounted)
# 12  
Old 05-21-2016
Quote:
Originally Posted by Abu Rayane
so these ones are FS (filesystem) SZ (size) AV (avalaible) US (used) PC (????) MT (mounted)
From the heading you used in post #1 in this thread:
Code:
Filesystem      Size  Used Avail Use% Mounted on

It looks to me like the reads should be changed from:
Code:
 read REST; while read FS SZ AV US PC MT REST

to:
Code:
 read REST; while read FS SZ US AV PC REST

where $US will expand to the data in the "Used" field and $PC will expand to the data in the "Use%" (i.e., used percent) field. Note that the standards don't specify the output format used in df -h output (although -h is a common option; it is an extension not required in the standards); on my system the heading used with it is:
Code:
Filesystem          Size   Used  Avail Capacity  iused     ifree %iused  Mounted on

where "Capacity" corresponds to the "Use%" heading you listed and in either case "$REST" will expand to the contents of the remaining fields (whether that is just the "Mount on" (directory on which the filesystem is mounted) as on your system, or the "iused" (number of i-nodes used), "ifree" (number of i-nodes free), "%iused" (percentage of i-nodes used), and "Mounted on" (directory on which the filesystem is mounted) fields produced by OS X df -h. If anyone else is trying to use code similar to yours, they need to adapt the arguments passed to read to assign needed fields to variables with names corresponding to the output format produced by df -h, df, or df -P (the last one being the only one with the output format specified by the standards) on their system for fields that they care about.

But, of course, since the code RudiC suggested doesn't use $US or $AV the order in which those are specified in the read command doesn't really matter in the way the script functions; the only thing that is crucial is the number of fields specified before PC (to get the percentage data needed by your script) and having at least one variable listed after PC so the remaining data on each line is not mixed in with the desired data when $PC is expanded.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 05-23-2016
So these letters (FS SZ US AV PC REST) are standardised !!!
# 14  
Old 05-23-2016
Quote:
Originally Posted by Abu Rayane
So these letters (FS SZ US AV PC REST) are standardised !!!
In the script:
Code:
df -h | {
	read REST
	while read FS SZ AV US PC MT REST
	do	useSpPer=${useSpPer}'-'$PC
		echo 'use '$useSpPer
		echo 'res '$PC
	done
	echo final $useSpPer
}

there is nothing standardized about the chosen variable names RudiC used. The only thing that matters is that the position in the list of values printed by the command df -h corresponds to the variable names used later in the script. In this specific script with the output produced by dh -h on your system, the following script would produce identical output:
Code:
df -h | {
	read junk
	while read junk junk junk junk needed junk
	do	useSpPer=${useSpPer}'-'$needed
		echo 'use '$useSpPer
		echo 'res '$needed
	done
	echo final $useSpPer
}

If there is a possibility that useSpPer might be an exported variable inherited into the environment of these scripts, it would be safer to define an initial value before the loop as in:
Code:
df -h | {
	read junk
	useSpPer=''
	while read junk junk junk junk needed junk
	do	useSpPer=${useSpPer}'-'$needed
		echo 'use '$useSpPer
		echo 'res '$needed
	done
	echo final $useSpPer
}

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

2. Shell Programming and Scripting

Narrowing sed Results in While Loop

find $SRC -type f -name *.emlx | while read FILE do if : then sed -n '/From/p' $FILE fi done > $DEST-output.txt The loop above spits out a .txt file with several lines that look like this: From: John Smith <jsmith@company.com> How can I narrow that sed result to spit out the email... (5 Replies)
Discussion started by: sudo
5 Replies

3. Shell Programming and Scripting

Loop through awk results

I have files structured in stanzas, whose title is '', and the rest couples of 'id: value'. I need to find text within the title and return the whole stanzas that match the title. The following works: awk 'BEGIN{RS="";IGNORECASE=1}/^\/' myfileI would need to count all of the occurences, though,... (7 Replies)
Discussion started by: hermes14
7 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. Shell Programming and Scripting

Concatenate Loop Results

Hi, I have the following situation: Param1Values = AAAA,BBBB Param1=$(echo $Param1Values| tr "," "\n") for x in $Param1 do db2 select X from Y where Z IN ('$x') done Obviously the above will perform the select 'x' amount of times. Is there a way in which i can... (13 Replies)
Discussion started by: RichZR
13 Replies

6. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

7. Shell Programming and Scripting

doing a for loop adding up the results

Hi there If I run a 'swap -l' on my solaris box, i get swapfile dev swaplo blocks free /dev/dsk/c1t0d0s1 54,65 8 67119560 65655144 /dev/dsk/c1t0d0s2 54,65 8 33119522 32655122 I wanted to run a for loop adding up the totals of each column 4 , excluding the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Shell Programming and Scripting

2 CMD results on the same line while rexing in a loop

Folks, I have a 3 problems. In a sh script, I call a server name from a list and rex to a distant machine to get the boot date. for i in `cat list` do (echo "$i|"; /bin/rexsh $i -l bozo -t10 who -b | cut -d" " -f14-16) >>getBootTimes.out sleep 1 done The results are on 2 lines instead... (8 Replies)
Discussion started by: linux_lou
8 Replies

9. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question