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

Co robi 'źródło'?

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

Istnieje, i jest uruchomiony. Dlaczego nie ma o tym żadnej dokumentacji w Ubuntu? Co to robi? Jak mogę zainstalować dokumentację na jego temat?

Advertisement

Odpowiedzi (9)

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

Bądź ostrożny! ./ i sourcenie do końca takie same.

  • ./script uruchamia skrypt jako plik wykonywalny, uruchamiając nową powłokę, aby go uruchomić
  • source script odczytuje i wykonuje polecenia z nazwy pliku w środowisku biegowej powłoki

Uwaga: ./script nie jest . script, ale . 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

Przydatna jest znajomość polecenia ‘type’:

> type source
source is a shell builtin

ilekroć coś jest wbudowaną powłoką, czas zrobić man bash.

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

. (a period) jest to polecenie wbudowane powłoki bash , które wykonuje polecenia z pliku przekazanego jako argument, w bieżącej powłoce. ‘source’ jest synonimem ‘.’.

Od strony man man basha:

. 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’ to długa wersja polecenia ‘.’. Na zachętę bash można zrobić:

source ~/.bashrc

aby przeładować (zmienić?) ustawienia basha dla aktualnie uruchomionego basha.

Krótka wersja to:

. ~/.bashrc

Strona 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 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

Polecenie source wykonuje podany skrypt (uprawnienie wykonywalne jest nieobowiązkowe ) w biegunowym środowisku powłoki, natomiast ./ wykonuje podany krypt wykonywalny w nowej powłoce.

source komenda ma synonim . filename.

Aby uczynić to bardziej przejrzystym, zajrzyj do poniższego skryptu, który ustawia alias.

zrobić `alias
#! /bin/bash

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

Teraz mamy dwie możliwości wykonania tego skryptu. But with only one option, the desired alias for current shell can be created among these two options.

Option 1: ./make_alias

Make script executable first.

chmod +x make_alias

Execute

./make_alias

Verify

alias

Output

**nothing**

**Whoops! ## Alias is gone with the new shell.

Let’s go with the second option.

Option 2: source make_alias

Execute

source make_alias

or

Verify

. make_alias

Output

alias

Yeah Alias is set.

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

W razie wątpliwości, najlepiej użyć polecenia 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

Wpisz polecenie “help source” w swojej powłoce.

Otrzymasz wyjście w następujący sposób:

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

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

source , . (polecenie kropka): To polecenie, po wywołaniu z linii poleceń, wykonuje skrypt. W obrębie skryptu, źródłowa nazwa pliku ładuje nazwę pliku. Pozyskanie pliku (polecenie kropkowe) importuje kod do skryptu, dołączając do niego (ten sam efekt, co dyrektywa #include w programie C). Wynik netto jest taki sam, jak gdyby “źródłowe” linie kodu były fizycznie obecne w treści skryptu. Jest to użyteczne w sytuacjach, gdy wiele skryptów wykorzystuje wspólny plik danych lub bibliotekę funkcji. Jeżeli plik źródłowy jest sam w sobie plikiem wykonywalnym, to zostanie uruchomiony, a następnie zwróci kontrolę do skryptu, który go wywołał. Pozyskany skrypt wykonywalny może w tym celu użyć zwrotu.

Tak więc, dla tych, którzy znają język programowania C, pozyskanie pliku ma efekt podobny do dyrektywy #include.

Zauważ również, że do pozyskanego pliku można przekazać argumenty pozycyjne, takie jak:

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

Ze źródłem możesz przekazać zmienne lub funkcje z innego pliku do swojego skryptu i używać ich bez konieczności ponownego ich zapisywania.

F.I:

#!/bin/bash

source /etc/environment

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

Cheers

Advertisement