简体   繁体   English

在bash脚本中如何在这种情况下执行多个if,else语句?

[英]How to do multiple if, else statements in this case in bash script?

Right now I am running a few programs with bash scripts (with Cygwin). 现在,我正在使用bash脚本(与Cygwin)一起运行一些程序。

Basically what I am doing is after the program is starting, a loop is ran that checks that the program is still running. 基本上,我正在做的是在程序启动后执行一个循环,以检查程序是否仍在运行。

I was doing: 我在做:

while true
do
    if [ "$(ps -W | grep -w name | gawk '{print $8,$9}' | gawk -F \\ '{print $4}')" == 'program' ];then 
        sleep 1
    else
        "start program" (whatever is needed here)
    fi
done

But I started to realize having such a script multiple times is just causing unnecessary system resources to be used. 但是我开始意识到多次拥有这样一个脚本只会导致不必要的系统资源被使用。

I tried doing an if then, elif, but it never goes past the first if. 我曾尝试过if if if elif,但从未超越过第一个if。

I need it to go "alright the first if is negative, try the next, go to the end, start over". 我需要它“首先,如果是负数,再尝试下一个,到底,重新开始”。

Here is the copy of my script and I forgot to say I was using Cygwin but that really doesn't change anything cause it seems to still use normal bash scripting just maybe different paths to start files. 这是我的脚本的副本,我忘了说我在使用Cygwin,但实际上并没有任何改变,因为它似乎仍然使用普通的bash脚本,只是启动文件的路径不同。 http://pastebin.com/s8ZdPQMn and yes the h. http://pastebin.com/s8ZdPQMn ,是的。 is not there I just can't seem to edit the pastebin. 不存在,我似乎无法编辑pastebin。

My overall plan is check that first SRPro is running, check the next, etc, only triggering if it's detected one is not running. 我的总体计划是检查第一个SRPro是否正在运行,检查下一个,等等,仅在检测到没有运行时才触发。

EDIT: I solved it. 编辑:我解决了。 Not exactly sure why but in my original single file per program, gawk printing $4 at the end gave me what I wanted, but for some reason when doing it this way, it turned to $5. 不完全确定为什么,但是在我每个程序的原始单个文件中,gawk在最后打印4美元给了我我想要的东西,但是由于某种原因,它变成了5美元。 So changing $4 to $5 made the script work. 因此,将$ 4更改为$ 5使脚本起作用。

EDIT: One really strange issue though is, it will work minutes on end, then all the sudden get confused at times, and start 7 copies of one program or something. 编辑:一个真正奇怪的问题是,它将在几分钟内结束工作,然后突然之间有时会感到困惑,并启动一个程序或某物的7个副本。 Also it can be random on which it starts. 也可以是随机的。

You might find the wait command (try help wait from a bash prompt) useful. 您可能会发现wait命令(从bash提示符尝试help wait )很有用。 It's unclear exactly what you want, but as an example, here's a basic respawn function: 目前尚不清楚您到底想要什么,但是作为示例,这是一个基本的重生函数:

$ respawn () {
>   while true
>   do
>     "${@}" &
>     wait ${!}
>     echo "respawning ..."
>   done
> }
$ respawn some_program arg1 arg2 etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM