$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]
Esiste ed è eseguibile. Perché non c'è alcuna documentazione su di esso in Ubuntu? Che cosa fa? Come posso installare la documentazione su di esso?
source
è un comando built-in di shell bash che esegue il contenuto del file passato come argomento,nella shell corrente. Ha un sinonimo in .
(punto).
Sintassi
. filename [arguments] source filename [arguments]
Stai attento! ./
e source
sono non proprio uguali .
./script
esegue lo script come un file eseguibile, avviando un new Shell per eseguirlosource script
legge ed esegue comandi da nomefile in corrente Shell environmentNota: ./script
non è . script
, ma . script
== source script
È utile conoscere il comando 'tipo':
> type source
source is a Shell builtin
ogni volta che qualcosa è integrato in una shell è tempo di fare man bash
.
. (un punto) è un comando incorporato in bash Shell che esegue i comandi da un file passato come argomento, nella Shell corrente. 'source' è un sinonimo di '.'.
Dalla pagina man di Bash:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current Shell
environment and return the exit status of the last command exe‐
cuted from filename. If filename does not contain a slash, file
names in PATH are used to find the directory containing file‐
name. The file searched for in PATH need not be executable.
When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the posi‐
tional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read.
'source' è la versione lunga di '.' comando. Sul prompt di bash si può fare:
source ~/.bashrc
per ricaricare le impostazioni di bash (modificate?) per la bash in esecuzione.
La versione breve sarebbe:
. ~/.bashrc
La pagina man:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current Shell environment and
return the exit status of the last command executed from filename. If
filename does not contain a slash, file names in PATH are used to find the
directory containing filename. The file searched for in PATH need not be
executable. When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option to the short
builtin command is turned off, the PATH is not searched. If any arguments
are supplied, they become the positional parameters when filename is
executed. Otherwise the positional parameters are unchanged. The return
status is the status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or cannot be
read.
Il comando source
esegue lo script fornito (il permesso eseguibile è non obbligatorio ) nell'ambiente current Shell, mentre ./
esegue lo script eseguibile fornito in nuovo Conchiglia.
Il comando source
ha un sinonimo . filename
.
Per rendere più chiaro, dare un'occhiata al seguente script, che imposta l'alias.
#! /bin/bash
alias myproject='cd ~/Documents/Projects/2015/NewProject'
Ora abbiamo due scelte per eseguire questo script. Ma con only una opzione, l'alias desiderato per la shell corrente può essere creato tra queste due opzioni.
./make_alias
Rendi prima eseguibile lo script.
chmod +x make_alias
./make_alias
alias
**nothing**
Whoops! Alias è andato con la nuova Shell.
Andiamo con la seconda opzione.
source make_alias
source make_alias
o
. make_alias
alias
alias myproject='cd ~/Documents/Projects/2015/NewProject'
Sì Alias è impostato.
In caso di dubbio, la cosa migliore da fare è usare il comando info
:
[[email protected] ~]# info source
BASH BUILTIN COMMANDS
Unless otherwise noted, each builtin command documented in this section
as accepting options preceded by - accepts -- to signify the end of the
options. The :, true, false, and test builtins do not accept options
and do not treat -- specially. The exit, logout, break, continue, let,
and shift builtins accept and process arguments beginning with - with-
out requiring --. Other builtins that accept arguments but are not
specified as accepting options interpret arguments beginning with - as
invalid options and require -- to prevent this interpretation.
: [arguments]
No effect; the command does nothing beyond expanding arguments
and performing any specified redirections. A zero exit code is
returned.
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current Shell
environment and return the exit status of the last command exe-
cuted from filename. If filename does not contain a slash, file
names in PATH are used to find the directory containing file-
name. The file searched for in PATH need not be executable.
When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the posi-
tional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read.
Digitare il comando "help source" nella Shell.
Otterrai risultati come questo:
source: source filename [arguments]
Execute commands from a file in the current Shell.
Read and execute commands from FILENAME in the current Shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
Va notato che sebbene sia un comando fantastico, né source
né la sua stenografia di .
dovrebbero source più di unofile, che significa
source *.sh
o
. script1.sh script2.sh
will notwork.
Possiamo ricorrere all'utilizzo di for
loops, ma emetterebbe l'eseguibile molte volte, creando comandi multipli o un problema.
Conclusione: source
non accetta più file come input. L'argomento deve essere uno.
Che fa schifo a IMHO.
Dal Linux Documentation Project, Advanced Bash Scripting Guide,
Capitolo 15 - Comandi interni e incorporati :
source ,. (punto di comando):
Questo comando, invocato dalla riga di comando, esegue uno script. All'interno di uno script, un nome file di origine carica il nome file del file. Sourcing di un file (dot-command) importa il codice nello script, aggiungendolo allo script (stesso effetto della direttiva #include in un programma C). Il risultato netto è lo stesso come se le linee di codice "originate" fossero fisicamente presenti nel corpo della sceneggiatura. Ciò è utile in situazioni in cui più script utilizzano un file di dati o una libreria di funzioni comuni.
Se il file originario è esso stesso uno script eseguibile, verrà eseguito, quindi restituirà il controllo allo script che lo ha chiamato. Uno script eseguibile originario può utilizzare un ritorno per questo scopo.
Quindi, per chi ha familiarità con il linguaggio di programmazione C, l'acquisizione di un file ha un effetto simile alla direttiva #include
.
Si noti inoltre che è possibile passare argomenti posizionali al file di provenienza, ad esempio:
$ source $filename $arg1 arg2
Con il sorgente puoi passare variabili o funzioni da un altro file al tuo script e usarle senza doverle scrivere di nuovo.
F.I:
#!/bin/bash
source /etc/environment
source /myscripts/jetty-common/config/jetty-functions.sh
Saluti