LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-15-2011, 12:09 PM   #16
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387

Hi,
Quote:
Originally Posted by doru View Post
Yes it does.
No it doesn't ....

Using the script you posted (post #2) altered to show the content of the mountedon variable inside and outside the loop:
Code:
$ cat tets.sh
#!/bin/bash

df -Ph |awk '{ print $5 " " $1 " " $6 }' | while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon1=$(echo $outstring | awk '{ print $3 }' )

  if [[ $percentuse -ge 90 ]]; then
    mountedon=$mountedon\ $mountedon1
  fi
  echo "[IN LOOP] mountedon : $mountedon"
done
echo "[OUSIDE LOOP] mountedon : $mountedon"

$ ./tets.sh
[IN LOOP] mountedon : 
[IN LOOP] mountedon : 
[IN LOOP] mountedon :  /data/Movie
[IN LOOP] mountedon :  /data/Movie /data/Sounds
[OUSIDE LOOP] mountedon : 
You need to do as shown by Reuti (post #11) :
Code:
while read outstring;
do

  ---- code goes here ----

done < <(df -Ph | awk '{ print $5 " " $1 " " $6 }')
 
1 members found this post helpful.
Old 09-18-2011, 01:29 PM   #17
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Rep: Reputation: 16
Quote:
Originally Posted by druuna View Post
Hi,
No it doesn't ....

Using the script you posted (post #2) altered to show the content of the mountedon variable inside and outside the loop:
You are right, my script is wrong. However, the problem is not caused by any encapsulation while would offer, but by the fact that, as part of a pipeline, while is run in a subshell.

To avoid writing to disk, I would suggest this form:
Code:
#!/bin/bash

while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon1=$(echo $outstring | awk '{ print $3 }' )

  if [[ $percentuse -ge 50 ]]; then
    mountedon=$mountedon\ $mountedon1
  fi
  echo "[IN LOOP] mountedon : $mountedon"
done < <(df -Ph | awk '{ print $5 " " $1 " " $6 }')
echo "[OUSIDE LOOP] mountedon : $mountedon"
Thank you for drawing my attention to this bug.

Now I see that you posted the same solution. How can you post a solution if you don't explain the cause of the bug correctly?

Last edited by doru; 09-18-2011 at 01:39 PM. Reason: same solution discovered
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to send email... bfloeagle Linux - General 5 10-13-2009 11:20 AM
send an email of script output s_linux SUSE / openSUSE 19 07-25-2009 04:47 AM
send email from a script r.stiltskin Ubuntu 7 07-26-2008 01:36 AM
How to send an email within a script??? johnsanty Programming 19 09-02-2006 01:43 PM
script to send email message meluser Programming 2 03-26-2003 01:35 AM


All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration