'ප්‍රභවය' කරන්නේ කුමක්ද?


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

එය පවතින අතර එය ක්‍රියාත්මක කළ හැකිය. උබුන්ටු වල ඒ පිළිබඳ කිසිදු ලේඛනයක් නොමැති ඇයි? එය කරන්නේ කුමක්ද? ඒ පිළිබඳ ලියකියවිලි ස්ථාපනය කරන්නේ කෙසේද?



62
ඔබට අමතක වුනා $ type source source is a shell built-in
bnjmn

3
මගේ කවචය මෙය ආපසු ලබා දුන්නා $ whatis source source (1) - bash built-in commands, see bash(1). එසේම, man sourceමාව BASH_BUILTINS(1)මිනිසාගේ පිටු වෙත ගෙන යයි. මෙය ෆෙඩෝරා btw හි ඇත, එම ඩෙබියන් පැකේජ ලේඛනගත නොවන්නේ මන්දැයි නොදැන.
arielnmz

5
@lesmana, නියම සබැඳිය. මෙම සම්බන්ධිත පිළිතුර මෙම ප්‍රශ්නයට වඩාත් ගැඹුරු පිළිතුරයි.
ස්කොට්

6
"උදව් මූලාශ්‍රය" උත්සාහ කරන්න
ජේසර්

Answers:


496

sourceයනු වත්මන් කවචය තුළ තර්කයක් ලෙස සම්මත කරන ලද ගොනුවේ අන්තර්ගතය ක්‍රියාත්මක කරන bash shell ගොඩනඟන විධානයකි . එයට .(කාල ​​පරිච්ඡේදය) සමාන පදයක් ඇත .

සින්ටැක්ස්

. filename [arguments]

source filename [arguments]

8
sourceඑය bash විශේෂිත විධාන හෝ වෙනත් ෂෙල් වෙඩි ද එය මොනවාද? (මම ඉල්ලන්නේ නිවැරදිව ටැග් ලබා
ගැනීමටයි

2
අෆයික්, sourceබෝර්න් කවචයේ සිට ඇති අතර එම නිසා එහි පැවත එන්නන් සියල්ලන්ගේම විය හැකිය. en.wikipedia.org/wiki/Bourne_shell . සෑම ෂෙල් වෙඩි සඳහාම sourceවිධානයක් නොමැති බව මම දනිමි .

14
ag නාගුල්, sourceබෝර්න් කවචයේ නොතිබුණි, එය බොහෝ කලකට පසුව පැමිණි GNU දිගුවකි. මුල් සහ තවමත් අතේ ගෙන යා හැකි සින්ටැක්ස් (POSIX) යනු "dot" විධානය භාවිතා කිරීමයි, එනම් .ඒ වෙනුවට. sourceටයිප් කිරීමට වැඩි කාලයක් සහ අමතර වටිනාකමක් නොමැති නිසා මම පුද්ගලිකව කිසි විටෙකත් භාවිතා නොකරමි . නවකයින් සඳහා ස්ක්‍රිප්ට් වඩාත් කියවිය හැකි කරවීම එහි ප්‍රධාන අරමුණ යැයි මම සිතමි.
jlliagre

21
@jlliagre මගේ පුද්ගලික “මූලාශ්‍රය ඇත්තේ මන්දැයි පැහැදිලි කරන්න” යනු sourceඑය වඩාත් විස්තරාත්මකව පමණක් නොව එය යතුරු ලියනයකට වඩා වෙනත් දෙයක් සේ පෙනේ. මම විද්‍යුත් විධානයන්හි තාක්ෂණික විධානයන් යවන විට මිනිසුන් කාල පරිච්ඡේදය / තිත මඟ හැර ඇත.
පොහොසත් හෝමොල්කා

3
මෙම විධානය සඳහා එක් පොදු භාවිතයක් sourceවන්නේ බොහෝ විට විචල්‍ය පැවරුම් අඩංගු “වින්‍යාස ගොනුවක්” තුළට ෂෙල් ස්ක්‍රිප්ට් එකක් යෙදීමයි . විචල්ය පැවරුම් පසුව ස්ක්‍රිප්ටයේ ඉතිරි දේ පාලනය කරයි. ඇත්ත වශයෙන්ම, හොඳ ස්ක්‍රිප්ටයක් පෙර විචල්‍යයන් සංවේදී පෙරනිමි වලට පෙර සැකසෙනු ඇත source, නැතහොත් අවම වශයෙන් වලංගු අගයන් පරික්ෂා කරයි.
ලෝරන්ස් සී

295

පරෙස්සම් වෙන්න! ./ඒවා sourceඑක හා සමාන නොවේ .

  • ./scriptස්ක්‍රිප්ට් ක්‍රියාත්මක කළ හැකි ගොනුවක් ලෙස ධාවනය කර එය ක්‍රියාත්මක කිරීම සඳහා නව කවචයක් දියත් කරයි
  • source scriptවත්මන් ෂෙල් පරිසරය තුළ ගොනු නාමයෙන් විධාන කියවා ක්‍රියාත්මක කරයි

සටහන: ./scriptනොවේ . script, නමුත් . script==source script

/ubuntu/182012/is-there-a-difference-between-and-source-in-bash-after-all?lq=1


27
ඔබ මිශ්‍ර වෙමින් පවතී ./command සහ. ස්ක්‍රිප්ට්. source-command.-command ට සමාන වේ. ./Meh භාවිතා කරමින් වත්මන් ඩිරෙක්ටරියේ ස්ක්‍රිප්ට් / ද්විමය නම් මෙහ් ධාවනය කරන්න, සහ ප්‍රභවය සමඟ කිසිදු සම්බන්ධයක් නැත. -විධානය. ඔබේ සබැඳියේ පිළිතුරෙන් පැහැදිලි කර ඇති පරිදි.
ජොකීම් එලොෆ්සන්

3
@ ජොකිම් එලොෆ්සන් එය සබැඳියේ සඳහන් කර ඇත, නමුත් වරදවා වටහා ගැනීම වළක්වා ගැනීම සඳහා මම පිළිතුර වෙනස් කරමි. කරුණාකර එය නිවැරදි කරන්න.
damphat

6
පිළිගත් පිළිතුර ද මේ දෙසට යොමු කිරීම වැදගත් ය, මක්නිසාද යත් මොහොතකට මා සිතුවේ./ == source == .
ඩැනියෙල් එෆ්

92

'Type' විධානය දැන ගැනීම ප්‍රයෝජනවත් වේ:

> type source
source is a shell builtin

යමක් කවචයක් සාදන විට එය කිරීමට කාලයයි man bash.


1
කියවීමේදී සෑම විටම අලුත් දෙයක් දැන ගන්න man)

21
ඔබට ද භාවිතා කළ හැකිය help {builtin-name}, එනම් help source.
ලෝරන්ස් සී

1
helpසෑම තැනකම ක්‍රියා නොකරයි (අවම වශයෙන් zsh වලින්). typeකරයි.
කුමාර්හර්ෂ්

4
විස්තාරණය කිරීම සඳහා: ඔබ බාෂ් භාවිතා කරන්නේ නම් සහ ඔබ දන්නවා නම් (සමහර විට 'වර්ගය' හරහා) එය ගොඩනංවන ලද විධානයක් නම්, එවිට 'උදව්' මඟින් පේළි 4,184 ක් හරහා ගමන් නොකර ඔබට අවශ්‍ය ලියකියවිලි ඡේදයට කෙලින්ම ලැබෙනු ඇත. man bash 'පෙළ.
රොන් බර්ක්

40

. (කාල ​​පරිච්ඡේදයක්) යනු වත්මන් කවචයේ තර්කයක් ලෙස සම්මත කරන ලද ගොනුවකින් විධාන ක්‍රියාත්මක කරන බාෂ් ෂෙල් බිල්ට් විධාන වේ. 'ප්‍රභවය' යනු '.' යන්නට සමාන පදයකි.

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.

33

'ප්‍රභවය' යනු 'හි දිගු අනුවාදයයි.' විධානය. කඩිනම් විමසුමේදී කෙනෙකුට කළ හැක්කේ:

source ~/.bashrc

වත්මන් ධාවන බැෂ් සඳහා ඔබගේ (වෙනස් කළ?) බැෂ් සැකසුම නැවත පූරණය කිරීමට.

කෙටි අනුවාදය වනුයේ:

. ~/.bashrc

මෑන් පිටුව:

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

1
මෙය පිළිගත් පිළිතුර විය යුතුය.
පීටර් මෝර්ටෙන්සන්

29

sourceවිධානය ලබා තිර රචනය ඉටු (ක්රියාවෙහි යෙදවිය අවසරය අනිවාර්ය නොවේ ) තුළ වත්මන් අතර, ෂෙල් පරිසරය ./ඉටු සපයා ක්රියාත්මක දී තිර රචනය නව ෂෙල්.

sourceවිධානයට සමාන පදයක් . filenameඇත.

එය වඩාත් පැහැදිලි කර ගැනීම සඳහා, අන්වර්ථය සැකසෙන පහත පිටපත දෙස බලන්න.

make_alias

#! /bin/bash

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

දැන් අපට මෙම ස්ක්‍රිප්ට් ක්‍රියාත්මක කිරීමට තේරීම් දෙකක් තිබේ. නමුත් සමග පමණක් විකල්පය එක්, වර්තමාන ෂෙල් සඳහා කැමති හෙවත් මේ විකල්ප දෙකක් අතර නිර්මාණය කිරීම ද කළ හැක.

විකල්ප 1: ./make_alias

පළමුව ස්ක්‍රිප්ට් ක්‍රියාත්මක කළ හැකි කරන්න.

chmod +x make_alias

ක්‍රියාත්මක කරන්න

./make_alias

සත්‍යාපනය කරන්න

alias

ප්‍රතිදානය

**nothing**

අපොයි! අලියා නව කවචය සමඟ ගොස් ඇත.

දෙවන විකල්පය සමඟ යමු.

විකල්ප 2: source make_alias

ක්‍රියාත්මක කරන්න

source make_alias

හෝ

. make_alias

සත්‍යාපනය කරන්න

alias

ප්‍රතිදානය

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

ඔව් අන්වර්ථය සකසා ඇත.


10

සැකයක් ඇති විට, කළ හැකි හොඳම දේ 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.

ඔබට RTFM ට වඩා වැඩි යමක් සැපයිය හැකිද?
පීටර් මෝර්ටෙන්සන්

5

ඔබේ කවචයේ "help source" විධානය ටයිප් කරන්න.

ඔබට මේ ආකාරයට ප්‍රතිදානය ලැබෙනු ඇත:

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.

5

ලිනක්ස් ප්‍රලේඛන ව්‍යාපෘතියෙන්, උසස් බෑග් ස්ක්‍රිප්ටින් මාර්ගෝපදේශය,
15 වන පරිච්ඡේදය - අභ්‍යන්තර විධාන සහ බිල්ටින් :

මූලාශ්‍රය , . (dot command):
මෙම විධානය විධාන රේඛාවෙන් ආයාචනා කළ විට ස්ක්‍රිප්ට් එකක් ක්‍රියාත්මක කරයි. ස්ක්‍රිප්ට් එකක් තුළ, ප්‍රභව ගොනු නාමයක් ගොනු ගොනුවේ නම පටවනු ලැබේ. ගොනුවක් මූලාශ්‍ර කිරීම (dot-command) ස්ක්‍රිප්ටයට කේත ආනයනය කරයි, ස්ක්‍රිප්ටයට එකතු වේ (සී වැඩසටහනක # ඇතුළත් කිරීමේ නියෝගයට සමාන බලපෑමක්). ශුද්ධ ප්‍රති result ලය, කේතයේ “මූලාශ්‍ර” රේඛා භෞතිකව ස්ක්‍රිප්ටයේ ශරීරයට සමාන වූවාක් මෙනි. බහු ස්ක්‍රිප්ට් පොදු දත්ත ගොනුවක් හෝ ක්‍රියාකාරී පුස්තකාලයක් භාවිතා කරන අවස්ථාවන්හිදී මෙය ප්‍රයෝජනවත් වේ.
උපුටා ගත් ගොනුව ක්‍රියාත්මක කළ හැකි ස්ක්‍රිප්ටයක් නම්, එය ක්‍රියාත්මක වනු ඇත, පසුව එය හැඳින්වූ ස්ක්‍රිප්ටයට පාලනය නැවත ලබා දෙන්න. උපුටා ගත් ක්‍රියාත්මක කළ හැකි ස්ක්‍රිප්ටයක් මේ සඳහා නැවත පැමිණීමක් භාවිතා කළ හැකිය.

එබැවින්, සී ක්‍රමලේඛන භාෂාව හුරුපුරුදු අය සඳහා, ගොනුවක් ලබා ගැනීම විධානයට සමාන බලපෑමක් ඇති කරයි #include.

ගොනුව උපුටා ගැනීම සඳහා ස්ථානීය තර්ක ඉදිරිපත් කළ හැකි බව සලකන්න.

$ source $filename $arg1 arg2

මෙම පිළිතුර පෙර පිළිතුරු 9 ට වඩා වෙනස් වන්නේ කෙසේද?
ස්ටීවන් රවුච්

2
මා කලින් සඳහන් නොකළ තවත් තොරතුරු ප්‍රභවයක් සහ අමතර තොරතුරු එකතු කරමි.
ඇලෙක්සැන්ඩ්‍රෝ ඩි ඔලිවෙයිරා

1
sourceඑය තර්ක හෝ භාවිත කළ හැකි බව මම දැන සිටියේ නැත return.
ජෝ

2

එය වුවද භයානක විධානය වීම බව, වත් සඳහන් කළ යුතු ය sourceහෝ එහි අයුරෙන් .නොකරමි මූලාශ්රය එක් වඩා ගොනු, එහි තේරුම

source *.sh

හෝ

. script1.sh script2.sh

ඇත නැත වැඩ.

අපට forලූප භාවිතයෙන් පසුබසිනු ඇත , නමුත් එය ක්‍රියාත්මක කළ හැකි වාර ගණනක් නිකුත් කරයි, බහු විධානයන් නිර්මාණය කරයි.

නිගමනය: sourceබහු ගොනු ආදානය ලෙස නොගනී. තර්කය එකක් විය යුතුය.

එය IMHO උරා බොයි.


0

ප්‍රභවය සමඟ ඔබට වෙනත් ගොනුවකින් විචල්‍යයන් හෝ ක්‍රියාකාරකම් ඔබගේ ස්ක්‍රිප්ටයට යැවිය හැකි අතර ඒවා නැවත ලිවීමකින් තොරව භාවිතා කළ හැකිය.

FI:

#!/bin/bash

source /etc/environment

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

චියර්ස්

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.