LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
LinkBack Search this Thread
Old 09-22-2006, 04:09 PM   #1
SoulGrind
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Rep: Reputation: 0
Looping a bash script


Here's the deal - Although I've been around Linux for several years, I'm still a scripting n00b in many ways.

What's going on...

I am running Red Hat Enteprise Linux 4.x on a Dell PowerEdge 1750. Attached to it is a Dell PowerVault 220S configured as an 800 GB RAID 5. I am currently transferring 700+ GB of data from a TerraVault via a mounted SMB share to the 800 GB RAID 5.

I am currently monitoring the progress manually via the following command shell command:

[rhe4 ~]$ df -lh | grep "/dev/sdb1"
/dev/sdb1 808G 194G 573G 26% /vol1

What I would ultimately like to do is the following:

I'd like to append the output to file every 3 minutes until 730G has been reached. A time and date stamp would be nice too. Something along the lines of:

Fri Sep 22 14:07:14 PDT 2006
/dev/sdb1 808G 194G 573G 26% /vol1

Fri Sep 22 14:10:34 PDT 2006
/dev/sdb1 808G 194G 573G 26% /vol1

Obviously I need to create some sort of while-do-loop but not really sure how to go about doing this and I'm not versed in sed/awk, ergo - the reason I need a little help here.

Any takers?

Thanks in advance!
 
Old 09-22-2006, 04:19 PM   #2
pljvaldez
Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Squeeze (x86)
Posts: 6,092

Rep: Reputation: 269Reputation: 269Reputation: 269
Put this in a file filename.sh
Code:
#!/bin/sh
while true
do
  date
  df -lh | grep "/dev/sdb1"
  sleep 180
done
Then chmod +x filename.sh.

The sleep command is in seconds, so sleep 180 is 3 minutes. To kill it you'll have to CTRL+C. Otherwise, you can add a variable before the while loops starts and then increment it within the loop and exit when it has run a certain number of times.

Last edited by pljvaldez; 09-22-2006 at 04:21 PM.
 
Old 09-22-2006, 04:32 PM   #3
SoulGrind
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Stepping through...

Quote:
Originally Posted by pljvaldez
Put this in a file filename.sh
Code:
#!/bin/sh
while true
do
  date
  df -lh | grep "/dev/sdb1"
  sleep 180
done
Then chmod +x filename.sh.

The sleep command is in seconds, so sleep 180 is 3 minutes.
jvaldez - thanks for your attempt - I think something more needs to be done however...

I got the filename, setting the environment...

while true... hmmm... but what are we comparing it to? While -WHAT- is true?

From what I can see...

it's going to do the following...

while true... (while the following is true...)
do (do the following...)
date - outputs date to stdout
df -lh | grep "/dev/sdb1" - outputs drive info to stdout
sleep 180 - pauses for 180 seconds (3 minutes)
done - end loop

Once the script reaches the done marker, there's nothing left to be "true" as we haven't compared the current data size x against the maximum (730G). Also, since the next itteration would be incremented by 3 minutes, it would no longer be true, but false. Also, we haven't appended anything to a log file...

We're missing 2 key components.

So in my seudo/quazi code here's the logic I am seeing...

Start loop
Append date to copystat.log
Append df -lh | grep "/dev/sdb1" to copystat.log
check df -lh | grep "/dev/sdb1" for current drive capacity
if current drive capacity is less than 730G loop after 3 minutes
else if current drive capacity is 730G or more, end loop

Hope this helps.

I'm not sure as to how to write a script to accomodate this logic.

Thanks agian,
 
Old 09-22-2006, 04:42 PM   #4
SoulGrind
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Original Poster
Rep: Reputation: 0
I stand corrected

Quote:
Originally Posted by pljvaldez
Put this in a file filename.sh
Code:
#!/bin/sh
while true
do
  date
  df -lh | grep "/dev/sdb1"
  sleep 180
done
Then chmod +x filename.sh.

The sleep command is in seconds, so sleep 180 is 3 minutes. To kill it you'll have to CTRL+C. Otherwise, you can add a variable before the while loops starts and then increment it within the loop and exit when it has run a certain number of times.
I stand corrected - You script is working beautifully - other than output to file and testing against the destination size.

So this is better than nothing.

Thanks again
 
Old 09-22-2006, 05:40 PM   #5
Leisy
Member
 
Registered: Sep 2006
Location: Brno, Czech Republic
Distribution: Fedora
Posts: 56

Rep: Reputation: 15
Code:
#!/bin/bash

echo -n Enter limit:
read LIMIT
while true
do
  REST=`df -lh | grep "/dev/hda2" | awk '{print $4;}'`

  if [[ $REST =~ "[0-9]*M" ]]
  then
    # cut M from size
    REST=${REST%M}
  
    if [[ "$REST" -lt "$LIMIT" ]]
    then
      # exit loop
      break
    fi

    # add stuff to file
    echo -n `date` >> copystat.log
    echo `df -lh | grep "/dev/hda2"` >> copystat.log
  fi

  sleep 3
done
If I understand you, you can try this script.
 
Old 09-22-2006, 06:03 PM   #6
pljvaldez
Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Squeeze (x86)
Posts: 6,092

Rep: Reputation: 269Reputation: 269Reputation: 269
Here's another crack (probably not the most elegant, but it worked for me assuming there's no way to jump from 729G to 731G in 3 minutes time)
Code:
#!/bin/sh

#global size limit
LIMIT="730G"

while true
do
  date > date.txt
  df -lh | grep "/dev/sdb1" > df-out.txt
  
  #create single line with date and df output
  paste date.txt df-out.txt >> logfile.log
  
  i=`awk '{ print $3 }' df-out.txt`
  
  #exit loop if size from df -lh = LIMIT
  if [`echo $i` = `echo $LIMIT`]; then
      exit
  fi

  #wait 3 minutes (180 seconds) then run again
  sleep 180

done
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM
shell script looping ust Linux - General 2 01-19-2005 04:05 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
Bash case structure (looping) ravvar Programming 4 10-07-2003 07:07 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


All times are GMT -5. The time now is 12:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration