Jump to content
  • Advertisement
Sign in to follow this  
law_order

Question about the UNIX "." (period) command...

This topic is 4878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have a aquestion about the unix "." (period" command) as far as i know,that command is used to execute a shell script within the current shell,instead of creating a new subshell to execute the script. Now... When i use that command with any of my shell scripts,it works fine. however,when i use it with any other executable file,it says "command not found". That happens even when the programm i'm trying to execute is withing my "PATH" directories. So.. Is this command used ONLY for shell scripts? If yes,how does the shell distinguish between shell scripts and other executable files? I'm asking that.because i always use "chmod +x' on my scripts to make them executable... Thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
According to help:
Quote:

.: . filename [arguments]
Read and execute commands from FILENAME and return. The pathnames
in $PATH are used to find the directory containing FILENAME. If any
ARGUMENTS are supplied, they become the positional parameters when
FILENAME is executed.

Specifically, this part answers your question:
Quote:

Read and execute commands from FILENAME and return.


Normal executable files are already executed like this, probably using one of the exec routines.

Share this post


Link to post
Share on other sites
thanks a lot bytecoder...

i'm sorry but i don't fully get it...

ok..
it seems that the period command is used only for shell scripts.

but again...since i turn my script' executable bit on,how does the shell now that this is specifically a shell script and not " just another " executable file?

Thanks again

Share this post


Link to post
Share on other sites
The shell doesn't "execute" the file given after the "." command, in terms of handing it off to the OS and saying "go run this". It READS it, each line. Then it performs each command listed in the file, one at a time.

Share this post


Link to post
Share on other sites
oh god...i feel so stupid...

i underestand that the shell actually "reads" the scripts and interpretes the commands.

However,why when i type ". <some-exec-file> i get a "command not found" and when i type ". <some-shell-script>" it works fine?

i mean..the shell should underestand when a particular executable is a shell script or not,in order to know if the "." command can be used or not...
right?

Share this post


Link to post
Share on other sites
It doesn't understand anything.

It reads the first line, which is likely 'ELF<binary data>'(if I remeber n?x executable format correctly) and tries to run it, then doesn't find a command named 'ELF<binary data>' so it tells you that it didn't find that command.

OR, it might be intelligent, and just see the binary data in the executable instead of text and spits out the error right away.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If I'm getting your question right, the answer is probably called "magic numbers". For instance:


$ cat >foo.sh #!/bin/sh
> echo "hello world!"
> EOF
$ file foo.sh
foo.sh: Bourne shell script text executable
$ cat >foo.c #include
> int main() { printf("hello world!\n"); return 0; }
> EOF
$ cc -o foo foo.c
$ file foo
foo: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0, dynamically linked (uses shared libs), not stripped


The shell knows that "foo.sh" is a shell script (as opposed to an ELF executable) because of its magic number. It's basically a file signature, a pattern of bytes that identifies a format. Scripts, for instance, start with a shebang (#!). If you remove it from our "foo.sh", and then run "file" on it:


$ cat >foo.sh echo "hello world!"
> EOF
$ file foo.sh
foo.sh: ASCII text


Somehow, if you mark it as an executable (+x), it will still execute correctly. My guess is that it's passed to /bin/sh by default, but I wouldn't count on this behavior being a standard or even a convention.

Note that "file" uses more than just the magic numbers to identify a file's type.


Hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The board stripped some symbols.


$ cat >foo.sh<<EOF
> #!/bin/sh
> echo "hello world!"
> EOF
$ file foo.sh
foo.sh: Bourne shell script text executable
$ cat >foo.c<<EOF
> #include <stdio.h>
> int main() { printf("hello world!\n"); return 0; }
> EOF
$ cc -o foo foo.c
$ file foo
foo: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0, dynamically linked (uses shared libs), not stripped





$ cat >foo.sh echo<<EOF
> "hello world!"
> EOF
$ file foo.sh
foo.sh: ASCII text




Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!