0

I'm working on script which should do something if application is running or not Also there should be a timer of checking(60 seconds) The problem is that "if" statement which is checking "count" doesn't work.

Here is a script:

#!/bin/bash

osascript -e "do shell script \"

COUNT="0"

while true;
do
    if (ps aux | grep Skype | grep -v grep > /dev/null)
    then
        echo "RUNNING" > /Users/someuser/Desktop/RUNNING.txt
    else
        echo "STOPPED" > /Users/someuser/Desktop/STOPPED.txt
    fi

    sleep 1


    if (("$COUNT" > "60"))
    then
        exit 0
    fi

done

\" with administrator privileges"
Serge
  • 2,031
  • 3
  • 33
  • 56

3 Answers3

0

You are mixing osascript and shell script, why don't you just use shell script?

here is something that may work, if you do.

This is just a demo of how you update the Count variable with the number of elapsed seconds.

 COUNT=0
 datum=$(/bin/date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s")
 sleep 5
 diff=$(expr $(/bin/date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s") - $datum)
 COUNT=$(expr $COUNT + $diff )
 echo COUNT:$COUNT

I just "sourced" it, you'll have to add the sheebang line #!/bin/bash

Your second problem is the if test, it should/could be if [ $COUNT -gt 60 ]; then I hope this helps, the bash manual can be accessed by man bash in your terminal window.

McUsr
  • 1,400
  • 13
  • 10
0

You're defining the osascript in double quotes. That allows the $COUNT variable to be expanded before the osascript program even starts. Use single quotes or a quoted here-doc instead

#!/bin/bash
osascript <<'END_OSA'
    do shell script '
        for (( c=0; c <= 60; c++ )); do
            if ps aux | grep -q '[S]kype'; then
                echo "RUNNING" > /Users/someuser/Desktop/RUNNING.txt
            else
                echo "STOPPED" > /Users/someuser/Desktop/STOPPED.txt
            fi
            sleep 1
        done
    ' with administrator privileges
END_OSA

Hmm, my refactoring removed all $vars completely.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I receive an error: 20:21: syntax error: Expected “given”, “in”, “of”, expression, “with”, “without”, other parameter name, etc. but found unknown token. (-2741) when try to run this script – Serge Mar 04 '15 at 20:28
  • Sorry, I don't know osascript. You're on your own, or ask another question here or at https://apple.stackexchange.com/ – glenn jackman Mar 04 '15 at 20:45
  • I wonder if the single quotes following "do shell script" should be double quotes. Try that – glenn jackman Mar 04 '15 at 20:52
0

Here's how I would write a bash script with a counter. You can use this to turn it into an osascript command. You'll notice in your code you do not increment the counter so it never gets to 60.

Good luck.

#!/bin/bash

COUNT=1

while true;
do
    sleep 1
    echo "Counter: $COUNT"

    if (( $COUNT >= 20 ))
    then
        exit 0
    fi

    COUNT=$((COUNT+1))
done
regulus6633
  • 18,848
  • 5
  • 41
  • 49