2009-09-24 10:35:48 +0000 2009-09-24 10:35:48 +0000
604
604
Advertisement

Cosa fa la 'fonte'?

Advertisement
$ 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'è documentazione a riguardo in Ubuntu? Cosa fa? Come posso installare la documentazione a riguardo?

Advertisement

Risposte (9)

289
289
289
2013-07-11 13:24:07 +0000

Attenzione! ./ e source sono non proprio uguali.

  • ./script esegue lo script come file eseguibile, lanciando una nuova shell per eseguirlo
  • source script legge ed esegue comandi dal nome del file nell'ambiente current shell

Nota: ./script non è . script, ma . script == source script https://askubuntu.com/questions/182012/is-there-a-difference-between-and-source-in-bash-after-all?lq=1

92
92
92
2013-01-09 15:45:47 +0000

È utile conoscere il comando ‘tipo’:

> type source
source is a shell builtin

ogni volta che qualcosa è un guscio che si costruisce è il momento di fare man bash.

40
Advertisement
40
40
2009-09-24 10:48:52 +0000

. (un punto) è un comando bash shell built-in che esegue i comandi da un file passato come argomento, nella shell corrente. sorgente’ è un sinonimo di ‘.’.

Dalla pagina man di Bash 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 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.
32
32
32
2009-09-24 10:46:01 +0000

source’ è la versione lunga del comando ‘. Sul prompt bash si può fare:

source ~/.bashrc
``` ```
. ~/.bashrc

per ricaricare l'impostazione bash (modificata?) per il bash corrente in esecuzione.

Versione breve sarebbe:

. 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 shopt
    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.

La pagina man:

&001

28
Advertisement
28
28
2015-03-27 13:57:39 +0000

Il comando source esegue lo script fornito (il permesso eseguibile è non obbligatorio ) nell'ambiente della shell corrente, mentre ./ esegue lo script eseguibile fornito in una shell nuovo. Il comando

source ha un sinonimo . filename.

Per essere più chiari, dare un'occhiata al seguente script, che imposta l'alias.

make_alias

#! /bin/bash

alias myproject='cd ~/Documents/Projects/2015/NewProject'
``` ```
chmod +x make_alias

Ora abbiamo due scelte per eseguire questo script. Ma con solo un'opzione, l'alias desiderato per la shell corrente può essere creato tra queste due opzioni.

Opzione 1: ./make_alias

Rendere prima eseguibile lo script.

./make_alias

Esegui

alias
``` ```
**nothing**

Verifica

source make_alias

Output

. make_alias

*Whoops! * L'alias è andato con il nuovo guscio.

Andiamo con la seconda opzione.

Opzione 2: source make_alias

Opzione 2: &007

Esegui

alias

o

alias myproject='cd ~/Documents/Projects/2015/NewProject'

Verifica

&001

Uscita

&001

Si L'alias è impostato.

10
10
10
2015-11-11 05:44:49 +0000

In caso di dubbio, la cosa migliore da fare è usare il comando info :

[root@abc ~]# 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.
5
Advertisement
5
5
2015-09-19 14:14:57 +0000

Digitate il comando “help source” nella vostra shell.

Otterrete un output 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.
4
4
4
2017-06-12 00:13:34 +0000

Dal Linux Documentation Project, Advanced Bash Scripting Guide, Capitolo 15 - Internals Commands and Builtins :

source , . (dot command): Questo comando, quando viene richiamato dalla riga di comando, esegue uno script. All'interno di uno script, un nome di file sorgente carica il nome del file. Il sourcing di un file (dot-command) importa il codice di importazione nello script, aggiungendolo allo script (stesso effetto della direttiva #include in un programma C). Il risultato netto è lo stesso che se le linee di codice “sourced” fossero fisicamente presenti nel corpo dello script. Questo è utile in situazioni in cui più script utilizzano un file di dati o una libreria di funzioni comuni. Se il file sorgente è esso stesso uno script eseguibile, allora verrà eseguito, quindi restituirà il controllo allo script che lo ha chiamato. Uno script eseguibile di origine può usare un ritorno per questo scopo.

Quindi, per chi ha familiarità con il linguaggio di programmazione C, l'origine di un file ha un effetto simile alla direttiva #include.

Si noti anche che si possono passare argomenti di posizione al file di origine, come:

$ source $filename $arg1 arg2
0
Advertisement
0
0
2018-11-28 08:58:53 +0000

Con source potete passare variabili o funzioni da un altro file al vostro script e utilizzarle senza doverle scrivere di nuovo.

F.I:

#!/bin/bash

source /etc/environment

source /myscripts/jetty-common/config/jetty-functions.sh
``` &001 


Cheers
Advertisement