bash 在单个脚本中运行多个 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20356583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run multiple shell scripts in a single script
提问by rohit
I have 50 shell scripts: proc_0.sh, proc_500.sh, proc_1000.sh...proc_25000.sh.
我有 50 个 shell 脚本:proc_0.sh、proc_500.sh、proc_1000.sh...proc_25000.sh。
Each of them perform same operation but on different files.
他们每个人都执行相同的操作,但对不同的文件。
I would like to execute all these shell scripts in a single master shell script, one after another.
我想在一个主 shell 脚本中一个接一个地执行所有这些 shell 脚本。
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.
现在,我不确定这一点,但似乎这些脚本并行运行或以某种方式混合在一起。我这样说是因为当我单独运行这些脚本中的一些时,它们会给出正确的结果,但在以上述方式运行时却不会。也可能是这些脚本正在执行的程序存在问题。
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.
那么,谁能告诉我这是否是一个接一个运行多个 shell 脚本的正确方法?这将帮助我缩小问题的范围。
Thanks
谢谢
回答by thom
No, it is a bit messy how you are doing it.
不,你的做法有点混乱。
#!/bin/bash/
Drop the last slash or it won't work
删除最后一个斜杠,否则将不起作用
cd $(pwd)
this is the same as cd .
.cd to the current directory is bogus. Delete this from your script.
这和cd .
.cd 到当前目录是假的一样。从您的脚本中删除它。
i=0
You can delete this also: It is followed by a for loop which already initializes $i
你也可以删除它:它后面是一个已经初始化 $i 的 for 循环
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
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 !
您不需要使用“bash”语句。无论如何,它不会运行,因为您忘记了命令的路径。除非您从 /usr/local/bin 之类的目录中运行它:始终使用路径!
And no, your scripts are notrunning in parallel. You forgot to background them.
不,您的脚本不是并行运行的。你忘了背景他们。
try this instead:
试试这个:
#!/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
This will get your computer flooded with your 50 processes at once.
这将使您的计算机一次充斥着 50 个进程。
run top
to see them running.
跑去top
看他们跑。
It would be best if you had all these proc scripts together in one subdirectory exclusively and then run:
最好将所有这些 proc 脚本放在一个子目录中,然后运行:
#!/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/"*