Bash IF语句不';无法在脚本中正常工作
我正在编写一个脚本,如果应用程序正在运行或者没有运行,它应该做些什么。还应该有一个检查计时器(60秒)。问题是,检查“count”的“if”语句不起作用 下面是一个脚本:Bash IF语句不';无法在脚本中正常工作,bash,applescript,osascript,Bash,Applescript,Osascript,我正在编写一个脚本,如果应用程序正在运行或者没有运行,它应该做些什么。还应该有一个检查计时器(60秒)。问题是,检查“count”的“if”语句不起作用 下面是一个脚本: #!/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"
#!/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"
您正在混合osascript和shell脚本,为什么不直接使用shell脚本呢 如果你这样做的话,这里有一些可能有效的方法 这只是一个演示如何用经过的秒数更新Count变量
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
我只是“源代码”它,你必须添加sheebang行#/bin/bash
第二个问题是if测试,它应该/可能是if[$COUNT-gt 60];然后
我希望这有帮助,bash手册可以通过终端窗口中的
manbash
访问。您正在用双引号定义osascript。这允许在osascript程序启动之前展开$COUNT
变量。改用单引号或此处引用的文档
#!/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
#/bin/bash
osascript下面是我如何编写带有计数器的bash脚本。您可以使用此命令将其转换为osascript命令。你会注意到在你的代码中你没有增加计数器,所以它永远不会达到60
祝你好运
#!/bin/bash
COUNT=1
while true;
do
sleep 1
echo "Counter: $COUNT"
if (( $COUNT >= 20 ))
then
exit 0
fi
COUNT=$((COUNT+1))
done
“shell脚本”如何知道它应该使用bash?为什么要运行bash脚本来运行osascript来运行bash脚本?为什么不首先在bash中这样做呢?我收到一个错误:20:21:语法错误:预期为“给定”、“in”、“of”、表达式、“with”、“without”、其他参数名等,但发现未知标记。(-2741)尝试运行此脚本时抱歉,我不知道该脚本。你可以自己问,或者在这里或在我想知道“DoShell脚本”后面的单引号是否应该是双引号。试试看