在单个脚本中运行多个Shell脚本 [英] Run multiple shell scripts in a single script

查看:78
本文介绍了在单个脚本中运行多个Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有50个Shell脚本:proc_0.sh,proc_500.sh,proc_1000.sh ... proc_25000.sh.

I have 50 shell scripts: proc_0.sh, proc_500.sh, proc_1000.sh...proc_25000.sh.

它们每个都执行相同的操作,但是对不同的文件.

Each of them perform same operation but on different files.

我想在一个主shell脚本中一个接一个地执行所有这些shell脚本.

I would like to execute all these shell scripts in a single master shell script, one after another.

这是我到目前为止尝试过的:

Here is what I have tried so far:

#!/bin/bash/

cd $(pwd)

i=0

for i in {0..25000..500}
do
     chmod +x proc_${i}.sh
     bash proc_${i}.sh
done

exit

现在,我对此不太确定,但似乎这些脚本正在并行运行或以某种方式混合在一起.我之所以这样说,是因为当我分别运行其中一些脚本时,它们会给出正确的结果,但是以上述方式运行时却无法给出正确的结果.这些脚本正在执行的程序也可能有问题.

Now, I am not sure about this but it seems that these scripts are running in parallel or getting mixed up in some way. I say this because when I run a few of these scripts individually, they give correct results but not when run in the way mentioned above. It could also be that there might be a problem with the program these scripts are executing.

那么,有谁能告诉我这是否是一个又一个地运行多个shell脚本的正确方法?这将帮助我缩小问题的范围.

So, could anyone please tell me if this is the right way to run multiple shell scripts one after another? It would help me narrow down the problem.

谢谢

推荐答案

不,您的操作方式有点混乱.

No, it is a bit messy how you are doing it.

#!/bin/bash/

丢下最后一个斜杠,否则将不起作用

Drop the last slash or it won't work

cd $(pwd)

这与 cd相同. .cd到当前目录是伪造的.从脚本中删除它.

this is the same as cd . .cd to the current directory is bogus. Delete this from your script.

i=0

您也可以删除它:它后面是for循环,该循环已经初始化$ i

You can delete this also: It is followed by a for loop which already initializes $i

 chmod +x proc_${i}.sh

每次该脚本运行时,您都在显式设置执行标志.没什么害处,但是一次就够了.

You are explicitly setting the execute flag EVERY time this script runs. It does no harm but once is enough.

bash proc_${i}.sh

您不需要使用"bash"语句.无论如何,它都不会运行,因为您忘记了命令的路径.除非从/usr/local/bin之类的目录中运行此文件,否则:始终使用路径!

You don't need to use the "bash" statement. Regardless of this, it will not run because you forgot the path to your command. Unless you are running this from a directory like /usr/local/bin: Always use a path !

不,您的脚本不是不是并行运行.你忘了给他们做背景.

And no, your scripts are not running in parallel. You forgot to background them.

尝试以下方法:

#!/bin/bash

for i in {0..25000..500}
do
     /path/to/proc_${i}.sh &   #<- don't forget to replace "/path/to" with the
done                           #   real directory where the proc_ files reside

wait

这将使您的计算机一次充斥着50个进程.

This will get your computer flooded with your 50 processes at once.

运行 top 可以查看它们的运行情况.

run top to see them running.

最好将所有这些proc脚本排在一个子目录中,然后运行:

It would be best if you had all these proc scripts together in one subdirectory exclusively and then run:

#!/bin/bash

for i in "/path/to/procscripts/"*
do
    "$i" &
done

wait

这将使您自己更灵活,更轻松.

This would make it more flexible and more easy on yourself.

然后使所有脚本都可执行即可:

Also making all your scripts executable could then be easier:

chmod a+x "/path/to/procscripts/"*

这篇关于在单个脚本中运行多个Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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