![脚本文件名以前导数字来确定执行顺序的用例](https://linux22.com/image/1062044/%E8%84%9A%E6%9C%AC%E6%96%87%E4%BB%B6%E5%90%8D%E4%BB%A5%E5%89%8D%E5%AF%BC%E6%95%B0%E5%AD%97%E6%9D%A5%E7%A1%AE%E5%AE%9A%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F%E7%9A%84%E7%94%A8%E4%BE%8B.png)
我正在寻找文档或用例,其中 Linux 中有一个包含 shell 脚本的目录,其中每个文件名都以数字开头,并且文件按特定顺序运行。这个约定叫什么?在哪里使用?
例如:
0001-motd.sh
0002-proxy.sh
0300-ssh.sh
我知道我以前见过这个,只是不确定它在哪里或叫什么。
答案1
听起来你指的是run-parts
NAME
run-parts - run scripts or programs in a directory
SYNOPSIS
run-parts [--test] [--verbose] [--report] [--lsbsysinit] [--regex=RE]
[--umask=umask] [--arg=argument] [--exit-on-error] [--help] [--version]
[--list] [--reverse] [--] DIRECTORY
run-parts -V
DESCRIPTION
run-parts runs all the executable files named within constraints
described below, found in directory directory. Other files and direc‐
tories are silently ignored.
If neither the --lsbsysinit option nor the --regex option is given then
the names must consist entirely of ASCII upper- and lower-case letters,
ASCII digits, ASCII underscores, and ASCII minus-hyphens.
If the --lsbsysinit option is given, then the names must not end in
.dpkg-old or .dpkg-dist or .dpkg-new or .dpkg-tmp, and must belong to
one or more of the following namespaces: the LANANA-assigned namespace
(^[a-z0-9]+$); the LSB hierarchical and reserved namespaces
(^_?([a-z0-9_.]+-)+[a-z0-9]+$); and the Debian cron script namespace
(^[a-zA-Z0-9_-]+$).
If the --regex option is given, the names must match the custom
extended regular expression specified as that option's argument.
Files are run in the lexical sort order (according to the C/POSIX
locale character collation rules) of their names unless the --reverse
option is given, in which case they are run in the opposite order.
答案2
我不知道这种方法/技巧是否适用于您的情况。但是,如果我理解正确的话,我认为这会有效。
[feddy@localhost ~]$ mkdir test
[feddy@localhost ~]$ cd test
[feddy@localhost test]$ ls
[feddy@localhost test]$ vi 0001-ko.sh
[feddy@localhost test]$ cp 0001-ko.sh 0002-ko.sh
[feddy@localhost test]$ cp 0001-ko.sh 0004-ko.sh
[feddy@localhost test]$ cp 0001-ko.sh 0005-ko file.sh
[feddy@localhost test]$ cp 0001-ko.sh 0008-ko.sh
[feddy@localhost test]$ ls
0001-ko.sh 0002-ko.sh 0004-ko.sh 0005-ko file.sh 0008-ko.sh
[feddy@localhost test]$ for i in *
> do
> bash "$i"
> done
file 0001-ko.sh
file 0002-ko.sh
file 0004-ko.sh
file 0005-ko file.sh
file 0008-ko.sh
[feddy@localhost test]$
或者
$ find . -iname "*.sh"|while read f; do bash "$f"; done
file ./0001-ko1.sh
file ./0002-ko1.sh
file ./0005-ko1 file.sh
file ./0005-ko1.sh
您可以编写一个脚本并使用它来按顺序(根据文件名中包含的数字,即 xxxx-abcdef)执行任意文件夹中的脚本。
如果我理解错了,请纠正我。