本文关于在 Linux 下如果想一次过执行目录下所有 Script 的方法。
在 Linux 下要执行目录下所有 Script, 可以逐一把 Script 敲入键盘, 例如:
$ ./script.sh
$ ./script2.sh
$ ./script3.sh
……
除了这样逐一输入外, 可以用以下三种方法一次把目录下的 Script 执行:
run-parts 指令
使用 run-parts 指令, 例如想执行 /home/opencli/test/ 目录下所有以 “.sh” 结尾的 Script, 可以用以下指令:
$ run-parts –regex ‘.*sh$’ /home/opencli/test/
run-parts 也支援正规表逹式, 例如想执行所有以 “s” 字符开头, 并以 “.sh” 结尾的档案, 可以用以下指令:
$ run-parts –regex ‘^s.*sh$’ /home/opencli/test/
find 指令
find 指令主要用途是搜寻档案, 而它的其中一个参数 “-exec”, 是可以对找到的档案执行指令, 使用方法如下:
$ find /home/opencli/test/ -maxdepth 1 -type f -executable -name ‘*.sh’ -exec {} \;
以上指令会把 /home/opencli/test/ 目录下, 所以有 “.sh” 结尾的档案执行。
如果想把目录下的子目录内的档案也一同执行, 只要将上面指令移除 “maxdepth” 选项便可以:
$ find /home/opencli/test/ -type f -executable -name ‘*.sh’ -exec {} \;
for
使用 Shell Script 的 for, 也可以做到相同的效果:
$ for f in /home/opencli/test/*.sh ; do [ -x “$f” ] && [ ! -d “$f” ] && “$f” ; done
你可能感兴趣的内容: