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 01-16-2011, 11:50 PM   #1
newbie01.linux
LQ Newbie
 
Registered: Oct 2009
Posts: 15

Rep: Reputation: 0
Am confused - The all important . (period) - ????


Hi,

My apologies if this question is so trivial ... I guess there really is a room for dummies ...

Anyway, just wanting to know if someone can please explain what the dot (.) infront of the command or script does and why it works and does not work in the following?


Code:
server01(oracle)[]/u01/bin/oracle/server01$: db01env
ksh: db01env: cannot execute
server01(oracle)[]/u01/bin/oracle/server01$: . db01env
Set up for db01 database (using: /opt/oracle/9.2.0.7)
oracle:db01> ls -l /u02/bin/oracle/db01env
-rw-r--r--   1 root     other       1621 Jun 26  2005 /u02/bin/oracle/db01env
I did a which db01env and it says it is not found even though /u02/bin/oracle is included in the PATH environment variable. I believe this is because db01env is not executable and the which command only checks for executables, is that a correct assumption/guess?

Am not having issues at the moment but just curious how a single dot could make such a big difference that it can make or break stuff

Any response or explanation is very much appreciated.

Thanks in advance.
 
Old 01-16-2011, 11:56 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Have a look at 'help source' as source and dot are equivalent on most systems.
 
Old 01-17-2011, 04:41 AM   #3
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Smile

First thing,
The "which" command searches in the locations defined by PATH global environment variable.
If u put a path in PATH variable, the system will try to execute that file.

Second thing,
Some binaries contains built in mechanism for the directory check. You can only execute the binary, if u are in, its own directory.(I guess, and I passed through this)

Try to check for relevant .conf and .log files of corresponding binary.
 
Old 01-17-2011, 12:06 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Terse answer:

Using the dot notation (so-called "sourcing") reads the lines in the specified file and executes them in your current shell. Without the dot (an assuming the name you specify isn't a shell keyword, function or alias), the PATH is searched for a file which is executable and matches the name you specified and a new process is spawned with that file as the executable / script.


Verbose answer:

When you do this:
Code:
$ . myfile
... if "myfile" exists in the current directory, the contents are read into your interactive shell and each is executed as if you had typed it at the prompt. So, for example, if myfile contains
Code:
#!/bin/bash
date
myvariable="myvalue"
your interactive shell will try to execute "#!/bin/bash" first (which is a comment and therefore does nothing). Then the "date" command will be called (outputting the current system date and time). Finally, the value "myvalue" will be assigned to the variable "myvariable".

The last line is really helpful in understanding the difference between sourcing "myfile" and executing it. When you execute a file which contains #!/some/path as the first line, the system create a new process for /some/path, and then uses the rest of the file as standard input for that program. If /some/path is a shell like the one you happen to be using as your interactive shell then the difference between sourcing and executing the file is subtle but important!

To understand the difference you must know that one process cannot [directly] alter the memory of it's parent (at least not without the other process explicitly arranging to allow this). So when your interactive shell spawns a new shell to interpret the file, any commands which alter the memory state of the new process do not alter the memory state of the interactive shell. The assignment of "myvalue" to the variable "myvariable" is an example of this... In the following example, I'm going to assume "." is in your PATH. i.e. when you type "myfile" without the ., it will be found in the current working directory.

Code:
$ ls -l myfile
-rwxr-xr-x 1 mouse mouse 37 2011-01-17 18:04 myfile*
$ cat myfile
#!/bin/bash
date
myvariable=myvalue

$ echo "myvariable is: $myvariable"
myvariable is: 
$ myfile
Mon Jan 17 18:05:02 GMT 2011
$ echo "myvariable is: $myvariable"
myvariable is: 
$ . myfile
Mon Jan 17 18:05:09 GMT 2011
$ echo "myvariable is: $myvariable"
myvariable is: myvalue
Note that the first time myfile is called (without the .), the value of myvariable is not altered in your interactive shell. The second time it is.

I hope that makes things clearer.
 
  


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
please help,timeout in all connections after a period taktaz32 Linux - Networking 5 01-16-2011 02:47 AM
DNS Timeout Period sefs Linux - Networking 4 03-27-2009 06:45 AM
Confused nay Very Confused chrystlenight SUSE / openSUSE 3 08-28-2007 05:57 PM
Linux suxs period. MatchStick General 24 08-15-2005 04:01 PM
Cannot compile... period. element Linux - Software 6 03-27-2003 04:36 PM

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

All times are GMT -5. The time now is 05:06 PM.

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