Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to write a script that checks on the status of something. If the status "Copying" then exit (or retry in 60 minutes if we want to get fancy.) If all there is nothing returned (based on my second command, ie, everything is in sync) then do something else.

If copying; exit or retry in 60 If NULL; do more things.

root@box:~$ ./prog status
  PyDsk /home       In Sync
  PyDsk /trax       Copying 85% complete
  PyDsk /trax2      In Sync
  PyDsk /trax3      Copying 97% complete
  PyDsk /traxnl     Copying 50% complete
  PyDsk /traxnl2    Copying 69% complete
  PyDsk /traxnl3    Copying 56% complete
  PyDsk /traxnl4    In Sync
  PyDsk /traxnv     In Sync
  PyDsk /traxnv2    In Sync
  PyDsk /ulog       In Sync



root@box:~$ ./prog status | grep complete | awk '{print $3;}'
Copying
Copying
Copying
Copying
Copying

edit; I'm open to any other way of getting this done. I'm not married to how I've started things... it's just the only way I could think of. If there's something more elegant, I'll take it :)

edit2; redacted. I'm going dark.

share|improve this question
1  
It's hard for me to tell what your question is. What isn't working, what's going wrong? The else part? –  user unknown Feb 2 '12 at 13:07
    
I don't have anything at all. I need help with the whole thing. Sorry if that wasn't clear. –  LVLAaron Feb 2 '12 at 13:52
    
Well, you're asking for something pretty standard, why don't you start with a regular bash scripting tutorial? –  rahmu Feb 2 '12 at 14:00
    
It doesn't matter. I am annoyed and what patrician has below is exactly what I wanted. If there is nothing returned from the 2nd command I have listed, then execute something. If there is stuff still "Copying" just wait for a while. Someone obviously understood it perfectly clearly. –  LVLAaron Feb 2 '12 at 14:02
add comment

3 Answers

up vote 2 down vote accepted

I'm new to the bash programming. Therefore, it is very likely that someone may present a more elegant solution but here is mine;

#!/bin/bash
count=`./prog status | grep complete | awk '{print $3}' | wc -l`
zero="0"
if [ "$count" -eq "$zero" ]
then echo "all done."
else echo "wait a little more and try again."
fi

Count variable is the number of copy operations. If count is equal to zero, which means there is no copy operation in progress, then the script prints all done, otherwise it prints wait a little more and try again. You can change the lines containing echo with your own commands. Hope it helps.

share|improve this answer
    
Thank you. I appreciate your help. –  LVLAaron Feb 2 '12 at 14:02
    
grep -c complete could substitute grep|awk|wc pipeline. –  enzotib Feb 2 '12 at 14:39
    
Thank you @enzotib I didn't know about the -c option of the grep command. –  patrician Feb 2 '12 at 23:43
add comment

You could do something simpler like this:

[[ -n $(./prog status | awk '/complete/ {print $3}') ]] && echo "foo"

This line will only echo "foo" if the output of your command is empty.

share|improve this answer
2  
grep pattern | awk '{commands}' could be abbreviated as awk '/pattern/ {commands}'. –  enzotib Feb 2 '12 at 15:22
    
Yes, I didn't even think about that. I should pay more attention. I edited the answer, but you shouldn't hesitate to do that yourself :) –  rahmu Feb 2 '12 at 15:54
add comment

Complete seems to apear only in status Copying. So it does not matter which of both is being checked.

while ( ./prog status|grep -qw complete 2>/dev/null )
  do
    echo Waiting one hour for copy to complete.
    sleep 3600
    # or exit if you like to
done
# Synced, do whatever is to be done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.