LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looping a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/looping-a-bash-script-486097/)

SoulGrind 09-22-2006 04:09 PM

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!

pljvaldez 09-22-2006 04:19 PM

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.

SoulGrind 09-22-2006 04:32 PM

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,

SoulGrind 09-22-2006 04:42 PM

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

Leisy 09-22-2006 05:40 PM

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.

pljvaldez 09-22-2006 06:03 PM

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



All times are GMT -5. The time now is 08:01 AM.