<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2015-01-06 16:01 GMT+01:00 Karl Auer <span dir="ltr"><<a href="mailto:kauer@biplane.com.au" target="_blank">kauer@biplane.com.au</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span>On Tue, 2015-01-06 at 14:53 +0100, Johnny Rosenberg wrote:<br>
> So running a script with source means that the ”#!/bin/sh” (or in my case<br>
> ”#!/bin/bash”) in MainScript.sh goes for the sourced script too,<br>
> automatically?<br>
<br>
</span>Not sure I understand the question, so I'll answer with a description of<br>
how things work ;-)<br></blockquote><div><br></div><div>Okay, great. Shoot! :P</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Terminology: The script doing the sourcing is the main script, the<br>
script being sourced is the source script.<br>
<br>
The source script is syntactically inserted into the main script at the<br>
point where it is sourced. It is not called, it is not executed, it does<br>
not get a separate shell process. It becomes part of the main script.<br>
Whatever is interpreting and executing the main script will interpret<br>
and execute the source script too.<br>
<br>
Any line in the source script that starts with a "#" is treated as a<br>
comment and ignored. "#!/bin/sh" is only meaningful if it is the very<br>
first line of a script. It is not special in a source file, because<br>
there is no way it can ever be the first line.<br>
<br>
If you run these commands...<br>
<br>
echo ". /tmp/script2.sh" > /tmp/script1.sh<br>
echo '#!/bin/dash' > /tmp/script2.sh<br>
echo "ps ax | grep \$\$ | grep -v grep | awk '{print \$5}'"<br>
>> /tmp/script2.sh<br>
chmod u+x /tmp/script1.sh /tmp/script2.sh<br>
/tmp/script2.sh ; /tmp/script1.sh<br>
<br>
...you should see this output:<br>
<br>
/bin/dash<br>
bash<br>
<br>
script2.sh, run on its own, reports dash. But when sourced by<br>
script1.sh, it reports bash. That is, the first line of script2.sh is<br>
ignored when script2.sh is sourced, even though it is the first line of<br>
a source script that is sourced in the first line of a main script.<br>
<br>
BTW this also tells you that any executable text file will be treated as<br>
a bash script by bash if it doesn't have another interpreter specified<br>
in the first line.<br>
<span><br>
> Does this mean that you can't source, for instance a Python script from a<br>
> bash script? Or does omitting that line only mean ”nothing is changed, keep<br>
> going”?<br>
<br>
</span>You can source anything you like, but if the source file is not correct<br>
for whatever is interpreting and executing the main file, then the<br>
source file will cause errors. bash can't interpret python. Yet :-) To<br>
run python you need a new process, running a python interpreter.<br></blockquote><div><br></div><div>That clarified everything, it really did. Thanks!</div><div><br></div><div><br></div><div><div>Kind regards</div><div><br></div><div>Johnny Rosenberg</div><div>ジョニー・ローゼンバーグ</div></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><div><br>
Regards, K.<br>
<br>
--<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
Karl Auer (<a href="mailto:kauer@biplane.com.au" target="_blank">kauer@biplane.com.au</a>)<br>
<a href="http://www.biplane.com.au/kauer" target="_blank">http://www.biplane.com.au/kauer</a><br>
<a href="http://twitter.com/kauer389" target="_blank">http://twitter.com/kauer389</a><br>
<br>
GPG fingerprint: 3C41 82BE A9E7 99A1 B931 5AE7 7638 0147 2C3C 2AC4<br>
Old fingerprint: EC67 61E2 C2F6 EB55 884B E129 072B 0AF0 72AA 9882<br>
<br>
<br>
<br>
--<br>
ubuntu-users mailing list<br>
<a href="mailto:ubuntu-users@lists.ubuntu.com" target="_blank">ubuntu-users@lists.ubuntu.com</a><br>
Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/ubuntu-users" target="_blank">https://lists.ubuntu.com/mailman/listinfo/ubuntu-users</a><br>
</div></div></blockquote></div><br></div></div>