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;}'

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 :)

  • 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

It is very likely that someone may present a more elegant solution but here is mine;

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

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.

  • 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

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.

  • 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

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 )
    echo Waiting one hour for copy to complete.
    sleep 3600
    # or exit if you like to
# Synced, do whatever is to be done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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