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

Que fait la "source" ?

Advertisement
$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]

Il existe, et il est exécutable. Pourquoi n'y a-t-il pas de documentation à ce sujet dans Ubuntu ? Que fait-il ? Comment puis-je installer de la documentation à ce sujet ?

Advertisement

Réponses (9)

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

Attention ! ./ et source sont pas tout à fait les mêmes.

  • ./script exécute le script comme un fichier exécutable, en lançant un nouveau shell pour l'exécuter
  • source script lit et exécute les commandes à partir du nom de fichier dans l'environnement current shell

Note : ./script n'est pas . script, mais . 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

Il est utile de connaître la commande “type” :

> type source
source is a shell builtin

chaque fois qu'un shell est créé, il est temps de le faire man bash.

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

. (un point) est une commande intégrée de bash shell qui exécute les commandes à partir d'un fichier passé en argument, dans le shell courant. source" est un synonyme de “…”.

De la page de manuel 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.
32
32
32
2009-09-24 10:46:01 +0000

source" est la version longue de la commande “. A l'invite de bash, on peut faire :

source ~/.bashrc

pour recharger votre paramètre de bash (modifié ?) pour la bash en cours.

La version courte serait :

. ~/.bashrc

La page de manuel :

. 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.
28
Advertisement
28
28
2015-03-27 13:57:39 +0000

La commande source exécute le script fourni (la permission d'exécution n'est pas obligatoire** ) dans l'environnement de l'interpréteur de commandes current, tandis que ./ exécute le script exécutable fourni dans un nouveau interpréteur de commandes. Les commandes

source ont un synonyme . filename.

Pour être plus clair, regardez le script suivant, qui définit l'alias.

make_alias

#! /bin/bash

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

Maintenant nous avons deux choix pour exécuter ce script. Mais avec une seule option, l'alias souhaité pour le shell actuel peut être créé parmi ces deux options.

Option 1 : ./make_alias

Rendre le script exécutable en premier.

chmod +x make_alias

Exécuter

./make_alias

Vérifier

alias

Produire

**nothing**

*Whoops ! * L'alias a disparu avec le nouveau shell.

Passons à la deuxième option.

Option 2 : source make_alias

Exécuter

source make_alias

ou

. make_alias

Vérifier

alias

Sortie

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

Ouais L'alias est activé.

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

En cas de doute, la meilleure chose à faire est d'utiliser la commande 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

Tapez la commande “help source” dans votre shell.

Vous obtiendrez la sortie suivante :

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

Tiré du Linux Documentation Project, Advanced Bash Scripting Guide, Chapter 15 - Internals Commands and Builtins :

source , . (dot command) : Cette commande, lorsqu'elle est invoquée depuis la ligne de commande, exécute un script. Dans un script, un nom de fichier source charge le nom de fichier du fichier. La commande source d'un fichier (dot-command) importe le code dans le script, en l'ajoutant au script (même effet que la directive #include dans un programme C). Le résultat net est le même que si les lignes de code “sourcées” étaient physiquement présentes dans le corps du script. Cela est utile dans les situations où plusieurs scripts utilisent un fichier de données ou une bibliothèque de fonctions communs. Si le fichier source est lui-même un script exécutable, il s'exécutera, puis rendra le contrôle au script qui l'a appelé. Un script exécutable source peut utiliser un retour à cette fin.

Ainsi, pour ceux qui sont familiers avec le langage de programmation C, la recherche d'un fichier a un effet similaire à la directive #include.

Notez également que vous pouvez passer des arguments de position au fichier source, comme :

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

Avec le source, vous pouvez passer des variables ou des fonctions d'un autre fichier dans votre script et les utiliser sans avoir à les écrire à nouveau.

F.I :

#!/bin/bash

source /etc/environment

source /myscripts/jetty-common/config/jetty-functions.sh

Cheers

Advertisement