LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 11:09 AM   #16
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405

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, 12:29 PM   #17
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
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 12:39 PM. Reason: same solution discovered
 
  


Reply



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 10:20 AM
send an email of script output s_linux SUSE / openSUSE 19 07-25-2009 03:47 AM
send email from a script r.stiltskin Ubuntu 7 07-26-2008 12:36 AM
How to send an email within a script??? johnsanty Programming 19 09-02-2006 12:43 PM
script to send email message meluser Programming 2 03-26-2003 12:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:58 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
Open Source Consulting | Domain Registration