Df -h results in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Df -h results in a loop
# 1  
Old 05-19-2016
Df -h results in a loop

Hello everyone,

I am doing a check of the disk space using df -h, I want to combine the result in break line; but the result after while/done is empty:

Code:
# df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs           20G   14G  4.6G  75% /
/dev/root        20G   14G  4.6G  75% /
devtmpfs        993M  4.0K  993M   1% /dev
none            199M  2.7M  196M   2% /run
none            5.0M     0  5.0M   0% /run/lock
none            994M   72K  994M   1% /run/shm
/dev/sda2       894G  840G  9.2G  99% /home

disk.sh
Code:
useSpPer=''

df -h | awk 'NR>2 { print $1 " " $5 }' | while read output;
do

#echo $output
res=$(echo $output | awk '{ print $2}')

useSpPer=${useSpPer}'-'$res

echo 'use '$useSpPer
echo 'res '$res

done
echo 'final '$useSpPer

final prints empty result

Code:
# sh disk.sh 
use -75%
res 75%
use -75%-1%
res 1%
use -75%-1%-2%
res 2%
use -75%-1%-2%-0%
res 0%
use -75%-1%-2%-0%-1%
res 1%
use -75%-1%-2%-0%-1%-99%
res 99%
final

I am expecting a final result like in the last 'use':
Code:
use -75%-1%-2%-0%-1%-99%

to be
Code:
final 75%-1%-2%-0%-1%-99%

Thanks in advance

Last edited by RudiC; 05-19-2016 at 03:54 AM.. Reason: Added code tags.
# 2  
Old 05-19-2016
Hi,
the effect you see is caused by the way you feed your loop. The pipe creates a subshell which has its own copies of the variables, the ones in the parent shell are not affected by actions in the subshell.
Process substitution is one way to fix this problem:
Code:
useSpPer=''

while read output; do
   res=$(echo $output | awk '{ print $2}')
   useSpPer=${useSpPer}'-'$res
   echo 'use '$useSpPer
   echo 'res '$res
done < <(df -h | awk 'NR>2 { print $1 " " $5 }')
echo 'final '$useSpPer

This User Gave Thanks to cero For This Post:
# 3  
Old 05-19-2016
That behaviour is due to the loop being executed in a subshell, and the variable's contents is not given back to the parent shell.
# 4  
Old 05-19-2016
Try this pure shell:
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; }
use -28%
res 28%
use -28%-28%
res 28%
use -28%-28%-42%
res 42%
use -28%-28%-42%-3%
res 3%
final -28%-28%-42%-3%

This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-19-2016
When you pipe something to a block, the block runs in a sub-shell.
Variables in the block are not copied back to the main shell.

A while loop is such a block.
In RudiC example the { } is the block.
In cero example the pipe is avoided; the <( ) needs bash or zsh.

Last edited by MadeInGermany; 05-19-2016 at 04:48 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 05-19-2016
Thank you guys for your effort, but I found a little mistake in cero code:

Quote:
Originally Posted by cero
Code:
useSpPer=''

while read output; do
   res=$(echo $output | awk '{ print $2}')
   useSpPer=${useSpPer}'-'$res
   echo 'use '$useSpPer
   echo 'res '$res
done < <(df -h | awk 'NR>2 { print $1 " " $5 }')
echo 'final '$useSpPer


This is the mistake

Code:
str.sh: line 8: syntax error near unexpected token `<'
str.sh: line 8: `done < <(df -h | awk 'NR>2 { print $1 " " $5 }')'

# 7  
Old 05-19-2016
Quote:
Originally Posted by Abu Rayane
Thank you guys for your effort, but I found a little mistake in cero code:




This is the mistake

Code:
str.sh: line 8: syntax error near unexpected token `<'
str.sh: line 8: `done < <(df -h | awk 'NR>2 { print $1 " " $5 }')'

Only Bash, Zsh and Ksh93 supports process substitution, which is what the <(...) means. Since you are invoking it with sh, judging by this # sh disk.sh , I suggest you use the command grouping

Here's an example using your original post #1

Code:
df -h | awk 'NR>2 { print $1 " " $5 }' |
{
    while read output
    do
        res=$(echo $output | awk '{ print $2}')
        useSpPer=${useSpPer}'-'$res

        echo 'use '$useSpPer
        echo 'res '$res
    done
    echo 'final '$useSpPer
}

This User Gave Thanks to Aia 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