简体   繁体   English

在Mac OS X中运行多个不带参数的Shell脚本

[英]Running multiple shell scripts without arguments in Mac OS X

I have many scripts in a directory that all start with deploy_ (for instance, deploy_example.com ). 我的目录中有许多脚本都以deploy_ (例如, deploy_example.com )。
I usually run them one at a time by calling ./deploy_example.com . 我通常一次通过调用./deploy_example.com来运行它们。

How do I run them all, one after the other (or all at once if possible...)? 我该如何全部运行它们,一个又一个运行(如果可能,一次运行所有...)?

I've tried: 我试过了:

find deploy_* | xargs | bash

But that fails as it needs the absolute path if called like that. 但这失败了,因为这样调用它需要绝对路径。

You can do it in several ways. 您可以通过多种方式进行操作。 For example, you can do: 例如,您可以执行以下操作:

for i in deploy_* ; do bash $i ; done

您可以简单地执行以下操作:

for x in deploy*; do bash ./$x; done
find deploy_* | xargs -n 1 bash -c

Will run them all one after the other. 将一个接一个地运行它们。 Look at the man page for xargs and the --max-procs setting to get some degree of parallelism. 查看手册页中的xargs--max-procs设置,以获取一定程度的并行性。

Performed in a subshell to prevent your current IFS and positional parameters from being lost. 在子shell中执行,以防止丢失当前的IFS和位置参数。

( set -- ./deploy_*; IFS=';'; eval "$*" )

EDIT: That sequence broken down 编辑:该序列分解

(                     # start a subshell, a child process of your current shell

  set -- ./deploy_*   # set the positional parameters ($1,$2,...)
                      #   to hold your filenames

  IFS=';'             # set the Internal Field Separator

  echo "$*"           # "$*" (with the double quotes) forms a new string:
                      #   "$1c$2c$3c$4c..." 
                      #   joining the positional parameters with 'c',
                      #   the first character of $IFS

  eval "$*"           # this evaluates that string as a command, for example:
                      #    ./deploy_this;./deploy_that;./deploy_example.com
)

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

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