bash 运行文件夹中的所有 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41079143/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:30:59  来源:igfitidea点击:

Run all shell scripts in folder

linuxbashshell

提问by code123

I have many .shscripts in a single folder and would like to run them one after another. A single script can be execute as:

.sh在一个文件夹中有许多脚本,并且想一个接一个地运行它们。单个脚本可以执行为:

bash wget-some_long_number.sh -H

Assume my directory is /dat/dat1/files

假设我的目录是 /dat/dat1/files

How can I run bash wget-some_long_number.sh -Hone after another?

我怎样才能bash wget-some_long_number.sh -H一个接一个地运行?

I understand something in these line should work:

我知道这些行中的某些内容应该有效:

for i in *.sh;...do ....; done

for i in *.sh;...do ....; done

回答by Kirill Bulygin

Use this:

用这个:

for f in *.sh; do  # or wget-*.sh instead of *.sh
  bash "$f" -H 
done

If you want to stop the whole execution when a script fails:

如果您想在脚本失败时停止整个执行:

for f in *.sh; do
  bash "$f" -H || break  # execute successfully or break
  # Or more explicitly: if this execution fails, then stop the `for`:
  # if ! bash "$f" -H; then break; fi
done

It you want to run, e.g., x1.sh, x2.sh, ..., x10.sh:

你想运行,例如,x1.sh, x2.sh, ..., x10.sh

for i in `seq 1 10`; do
  bash "x$i.sh" -H 
done

回答by Hamza Jabbour

there is a much simpler way, you can use the run-partscommand which will execute all scripts in the folder:

有一种更简单的方法,您可以使用run-parts将执行文件夹中所有脚本的命令:

run-parts /path/to/folder

回答by Caucasian Malaysian

I ran into this problem where I couldn't use loops and run-parts works with cron.

我遇到了这个问题,我无法使用循环和运行部分与 cron 一起工作。

Answer:

回答:

foo () {
    bash -H  
    #echo 
    #cat 
}
cd /dat/dat1/files #change directory
export -f foo #export foo
parallel foo ::: *.sh #equivalent to putting a & in between each script

You use GNU parallel, this executes everything in the directory, with the added buff of it happening at a lot faster rate. Not to mention it isn't just with script execution, you could put any command in the function and it'll work.

您使用 GNU 并行,这会执行目录中的所有内容,并以更快的速度增加它的增益。更不用说它不仅仅是脚本执行,你可以在函数中放置任何命令,它会起作用。