如果只运行一个实例,请继续脚本? [英] Continue script if only one instance is running?

查看:96
本文介绍了如果只运行一个实例,请继续脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这很令人尴尬。我正在编写快速脚本,我无法弄清楚为什么这个语句不起作用。

  if [$(pidof -x test.sh | wc -w)-eq 1];然后回声通过; fi 

我也尝试使用反向代码而不是$()但它仍然无效。



你能看出它有什么问题吗? pidof -x test.sh | wc -w 如果我在脚本中运行它会返回1,所以我看不出任何理由为什么基本上如果[1 -eq 1] 不会过去。



非常感谢!

解决方案

Jefromi是正确的;这是我认为你想要的逻辑:

 #!/ bin / bash 
#这是test.sh

if [$(pidof -x test.sh | wc -w)-gt 2];然后
echo超过1
退出
fi

echo只有一个;做任何事......


now this is embarrassing. I'm writing quick script and I can't figure out why this statement don't work.

if [ $(pidof -x test.sh | wc -w) -eq 1 ]; then echo Passed; fi

I also tried using back-ticks instead of $() but it still wouldn't work.

Can you see what is wrong with it? pidof -x test.sh | wc -w returns 1 if I run it inside of script, so I don't see any reason why basically if [ 1 -eq 1 ] wouldn't pass.

Thanks a lot!

解决方案

Jefromi is correct; here is the logic I think you want:

#!/bin/bash
# this is "test.sh"

if [ $(pidof -x test.sh| wc -w) -gt 2 ]; then 
    echo "More than 1"
    exit
fi

echo "Only one; doing whatever..."

这篇关于如果只运行一个实例,请继续脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆