Mailinglist Archive: opensuse (818 mails)

< Previous Next >
Re: [opensuse] different types of shell scripts
Hello,

On Thu, 22 Sep 2011, George OLson wrote:
I downloaded a program that I needed off the internet and the
installation program was a script file with the extension .sh. The
instructions from the developer said to run the script on a command
line by typing the command:

./<filename>

I thought (mistakenly) that I could run any shell script by typing
the command . <filename> (with a space between the dot and the
filename). So I tried to run it that way, and I got the error "cannot
execute binary file".

However, when I followed the developer's instructions and typed in
./<filename>, the script executed perfectly and installed the
program.

So my question is, what is the difference between running a script
file with the command

<dot><space><filename>, for example "#> . myscript.sh"

and running it with

<dot><slash><filename>, for example "#> ./myscript.sh"?

The main difference in this context is the value of $0. As other's
wrote, '.' "sources" the script in the current shell, thus, $0 stays
as it were:

$ echo 'echo $0' | . /dev/stdin
bash

Calling a script via sh PATH/file or make PATH/file executable and
then call PATH/file (where PATH may be '.'), then, $0 gets set to the
name of the _scriptfile_:

$ echo 'echo $0' >/tmp/t.sh
$ sh /tmp/t.sh
/tmp/t.sh
$ chmod 700 /tmp/t.sh
$ /tmp/t.sh
/tmp/t.sh

And, surprise surprise, self-extracting archives (shar and the like)
have a shell-script at the top and then reread themselves with an
offset (with e.g. dd) to extract the actual archive (usually a tar).

So, basically what those script do is:

dd if=$0 skip=... | tar x ...

And if $0 is not the name of the script/archive but /bin/bash, go
figure.

What exactly caused the error you got when using '. file' I don't
know, but it's likely to be a side-effect of the above mentioned.

HTH,
-dhn

--
"Recently a friend gave me a pornographic video and said that I'd like
it, but I don't know how to watch it. I don't have a pornograph."
-- Peter Moylan in alt.usage.english
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
References