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

cod*_*123 15 linux bash shell

.sh在一个文件夹中有很多脚本,并希望一个接一个地运行它们.单个脚本可以执行为:

bash wget-some_long_number.sh -H
Run Code Online (Sandbox Code Playgroud)

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

我怎么能bash wget-some_long_number.sh -H一个接一个地跑?

我理解这些内容应该有效:

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

Kir*_*gin 21

试试这个:

for f in *.sh; do  # or wget-*.sh instead of *.sh
  bash "$f" -H 
done
Run Code Online (Sandbox Code Playgroud)

它要运行,例如x1.sh,x2.sh,..., x10.sh:

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
Run Code Online (Sandbox Code Playgroud)


小智 17

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

run-parts /path/to/folder
Run Code Online (Sandbox Code Playgroud)

  • 那么方法就不那么简单了,不是吗? (2认同)