LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-24-2008, 09:10 AM   #1
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
Execution of non-executable files ?


I noticed in a particular rc file that I have that it executes a file without execution permissions. I was then able to recreate the command from the command line, and it executed. What is going on here?

The file .mctdhrc has only read permissions.
Code:
$ cd ~
$ ./.mctdhrc       # DOES NOT EXECUTE
$ . ~/.mctdhrc     # EXECUTES
Why does the space in between the . and the file name allow the file to be executed? Can I prevent this behavior? I have certain files that are non-executable for a reason.
 
Old 06-24-2008, 09:13 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The command . (dot) stands for source. It does not execute a command but includes the content of the file in the current shell or script. In other words . executes the commands inside the file in the current shell environment.
 
Old 06-24-2008, 09:18 AM   #3
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 67
Ah. So if I'm in bash, it will only work for bash scripts?
 
Old 06-24-2008, 09:21 AM   #4
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
The . is the shell command source. The source command, when invoked from the command line, executes a script regardless of the sourced file permissions AFAIK.

Usually it is used within a script, to source <file-name>. It then loads the file <file-name> and effectively imports code into the script, appending to the script (same effect as the #include directive in a C program). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This is useful in situations when multiple scripts use a common data file (like an rc file for example) or function library.

Other than not sourcing a file, I don't know of any way to prevent the source command from not working.
 
Old 06-24-2008, 11:58 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by Meson View Post
Ah. So if I'm in bash, it will only work for bash scripts?
In /bin/sh you have only . (dot). In /bin/bash, /bin/ksh, you can use either . or source. In C-shells, /bin/csh, /bin/tcsh you have only source.
 
Old 06-26-2008, 03:11 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
And of course sourcing of a cshell style file won't work
in bash or sh. But then people with more than half a brain
cell won't use csh for scripting, anyway :}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
.cache/session files execution place santhosh.linux Programming 0 06-28-2006 06:43 AM
executable files prophetk Linux - Software 2 06-25-2006 08:39 AM
Converting perl files to executable mac files mrozkan Programming 0 04-16-2002 09:56 AM
Executable Files Acar Linux - General 1 02-22-2002 10:59 AM
Stopping before the Execution of the startup files !! sachin77 Linux - Newbie 0 04-25-2001 03:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration