LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2005, 11:21 PM   #1
in_touch
LQ Newbie
 
Registered: Jun 2005
Posts: 2

Rep: Reputation: 0
Question Question about the UNIX "." (period) command.


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.

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

So...I imagine that the shell has to know if a file is a shell script or not,in order to use or not the "." command"..

right?


Thanks a lot!
 
Old 06-15-2005, 11:59 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Yes...

You're absolutely correct:

1. Yes, the "." is used by many *nix shells to execute the shell script in the current shell rather than creating a subshell). This is the only way, for example, you can read a script that defines variables in your current shell.

2. And yes, the first argument after the "." must be the name of a shell script. It cannot be a binary.

Why?

Because the shell is expecting a text script (not an executable binary) ... and it tries to *read*, and then to *parse* that script. And of course you don't parse a binary: you execute it.

Try it:
- vi hello.c =>
#include <stdio.h>

int
main (int argc, char *argv[])
{
return printf ("Hello world!\n");
}

- cc -o hello hello.c

- ./hello =>
Hello world!
<= SO FAR, SO GOOD. WE'VE GOT A BINARY, AND WE CAN EXECUTE IT.

- od -cx hello =>0000000 177 E L F 001 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
457f 464c 0101 0001 0000 0000 0000 0000
0000020 002 \0 003 \0 001 \0 \0 \0 \0 203 004 \b 4 \0 \0 \0
...
<= YOU'LL NOTICE THAT THE FIRST 4 BYTES OF ANY LINUX EXECUTABLE
ARE THE CHARACTERS "\0x7fELF"

- . ./hello =>
bash: ELF: command not found
<= AND YOU'LL SEE THAT THE "BASH" SHELL TRIED TO PARSE THE
"TEXT" IN THE (BINARY!) FILE "HELLO" ... AND FAILED TO UNDERSTAND IT

Your mileage may vary depending on exactly which shell you're using, and exactly how you type the command. But the bottom line is "Yes, the '.' requires a shell script."

Hope that helps .. PSM
 
Old 06-16-2005, 12:22 AM   #3
in_touch
LQ Newbie
 
Registered: Jun 2005
Posts: 2

Original Poster
Rep: Reputation: 0
thanks so much!
i was really confused but your answer really makes sense!


but i have one more question.

what happens when we just type "prompt> ./<some-shell-script"?

obviously our shell forks a new shell to execute the script.

however,how does the shell know how to handle the file?
it might be a a c program or simply a script.

does it examine again the first bytes of the file to determine the file type?

thanks a lot!
 
Old 06-16-2005, 01:01 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Think of ". some_shell_script" as more of an include cmd, rather than actually running a program.
 
Old 06-16-2005, 03:03 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by in_touch
what happens when we just type "prompt> ./<some-shell-script"?
obviously our shell forks a new shell to execute the script.

however,how does the shell know how to handle the file?
it might be a a c program or simply a script.
As you rpobably know, the "dot" in "./somescript" is a totally different one from when your do ". somescript".

". somescript" includes the shell-commands in the same shell process, and "./somescript" or "./some_c_program" runs the mentioned program in a new (child-) process of the shell.

". somescript": here the dot is a shell command.
"./somescript": here the dot indicates the directory where the program or script to run resides.

"./somescript": here the shell tries to run (execute) somescript, and if the file "somescript" has execute permissions (the x's you see when you do "ls -l") it will run. The shell check the first bytes, and if they are "#!" it acutally executes the binary executable (ELF file, say "C-program") mentioned in the same line. If the bytes are not "#!" then try to make the system (kernel) execute it directly as a binary executable.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about "converting" from SCO Unix to Linux... severedhead Linux - Software 4 09-15-2009 03:08 PM
"Could not init font path element""Unix /: 7100 removing from list/ zameer_india Linux - Networking 7 07-03-2006 06:11 AM
trying to write code for UNIX command "which" live_dont_exist Programming 11 03-15-2005 12:16 PM
the best "unix like" or "unix" operating system melinda_sayang General 12 12-03-2004 02:50 AM
useradd problem with period "." on RH9.0 atcg Linux - Distributions 0 01-02-2004 01:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration