Bash
run-parts(8) 實用程序的用途
至少在基於 Debian 的系統下,debianutils
run-parts
包中有一個實用程序,可用於各種腳本。例如在 /etc/X11/Xsession 中。它將執行在目錄中找到的所有執行檔。為什麼需要執行元件而可以與選項或實用程序一起使用?另外,什麼被認為是執行檔?看起來它不只是檢查文件權限:find``-perm``test``run-parts
# run-parts --list --lsbsysinit /etc/X11/Xsession.d | tail -1 /etc/X11/Xsession.d/90x11-common_ssh-agent # ls -l /etc/X11/Xsession.d/90x11-common_ssh-agent -rw-r--r-- 1 root root 629 2010-11-02 23:17 /etc/X11/Xsession.d/90x11-common_ssh-agent # head /etc/X11/Xsession.d/90x11-common_ssh-agent # $Id: 90x11-common_ssh-agent 305 2005-07-03 18:51:43Z dnusinow $ # This file is sourced by Xsession(5), not executed. STARTSSH= SSHAGENT=/usr/bin/ssh-agent SSHAGENTARGS= if has_option use-ssh-agent; then if [ -x "$SSHAGENT" ] && [ -z "$SSH_AUTH_SOCK" ] \ #
您可以使用
find
代替run-parts
,沒有辦法顯示哪個更好。但我認為 usingrun-parts
更短(更少打字)並使您的腳本更易於維護。一個例子是/etc/crontab
:# /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
debianutils
對於第二個問題,您可以在原始碼中找到答案。在文件run-parts.c
中,第 198 行:/* Execute a file */ void run_part(char *progname) { .... args[0] = progname; execv(progname, args); error("failed to exec %s: %s", progname, strerror(errno)); exit(1); .... }
你可以看到
run-parts
使用execv
系統呼叫。因此,如果您的文件不是二進制執行檔或Interpreter script
,execv
則無法執行該文件。筆記
- 什麼是
Interpreter script
:從
man execve
, 部分Interpreter scripts
:Interpreter scripts An interpreter script is a text file that has execute permission enabled and whose first line is of the form: #! interpreter [optional-arg] The interpreter must be a valid pathname for an executable which is not itself a script. If the filename argument of execve() specifies an interpreter script, then interpreter will be invoked with the following arguments: interpreter [optional-arg] filename arg... where arg... is the series of words pointed to by the argv argument of execve(). For portable use, optional-arg should either be absent, or be specified as a single word (i.e., it should not contain white space); see NOTES below.
- 您可以在此處查看 debianutils 原始碼。