f

#### how to tell if a file is inary file or ascii file?

Hello,

Is there a way to find if a file is a binary executable file or an ASCII
executable file in unix? especially, if I have a file with
permission of --x--x--x.

Thanks,

Peter


 0
peter
1/14/2004 5:31:11 AM
comp.unix.programmer 10848 articles. 0 followers. kokososo56 (350) is leader.

13 Replies
1678 Views

Similar Articles

[PageSpeed] 13

peter wrote:

> Hello,
>
> Is there a way to find if a file is a binary executable file or an ASCII
> executable file in unix? especially, if I have a file with
>   permission of --x--x--x.

Tobias


 0
Tobias
1/14/2004 5:49:39 AM
On Wed, 14 Jan 2004 05:31:11 +0000, peter wrote:

> Is there a way to find if a file is a binary executable file or an ASCII
> executable file in unix? especially, if I have a file with
>   permission of --x--x--x.

This is non-trivial for the general case. An acceptable solution may be to
look at the first two bytes of the file. If  they are "#!" then it's a
script of some sort, otherwise assume it's a binary. Of course this simple
test fails for any number of cases. For example, a shell script may not
begin with the "#!" signature. If the file begins with a ":" some shells
may assume it's a Bourne shell script. There are other variations on that
theme but in my experience they're rare.

 0
Kurtis
1/14/2004 5:56:19 AM
In article <zu4Nb.9619$QP.4822@newssvr27.news.prodigy.com>, peter <one2001boy@yahoo.com> wrote: > Hello, > > Is there a way to find if a file is a binary executable file or an ASCII > executable file in unix? especially, if I have a file with > permission of --x--x--x. If you don't have read permission, there's no way to tell what's in it. Note that most Unix implementations can't run execute-only scripts; the interpreter has to be able to read the file. So something with those permissions would normally have to be a binary executable. I think Solaris is an exception to this; it makes use of /dev/fd to provide a way for the interpreter to read the file (it uses the same trick to implement setuid scripts without the symlink security hole). -- Barry Margolin, barmar@alum.mit.edu Arlington, MA   0 Barry 1/14/2004 6:04:16 AM Barry Margolin wrote: > In article <zu4Nb.9619$QP.4822@newssvr27.news.prodigy.com>,
>  peter <one2001boy@yahoo.com> wrote:
>
>
>>Hello,
>>
>>Is there a way to find if a file is a binary executable file or an ASCII
>>executable file in unix? especially, if I have a file with
>>  permission of --x--x--x.
>
>
> If you don't have read permission, there's no way to tell what's in it.
>
> Note that most Unix implementations can't run execute-only scripts; the
> interpreter has to be able to read the file.  So something with those
> permissions would normally have to be a binary executable.  I think
> Solaris is an exception to this; it makes use of /dev/fd to provide a
> way for the interpreter to read the file (it uses the same trick to
> implement setuid scripts without the symlink security hole).
>

Thanks for your help. Barry. Unix shell are unable
to run the unix shell scripts with permission of --x--x--x, but the
binary file with permission --x--x--x can be executed. not sure how the
unix shell can tell the differences.

Peter


 0
peter
1/14/2004 6:42:34 AM
Kurtis D. Rader wrote:

> On Wed, 14 Jan 2004 05:31:11 +0000, peter wrote:
>
>
>>Is there a way to find if a file is a binary executable file or an ASCII
>>executable file in unix? especially, if I have a file with
>>  permission of --x--x--x.
>
>
> This is non-trivial for the general case. An acceptable solution may be to
> look at the first two bytes of the file. If  they are "#!" then it's a
> script of some sort, otherwise assume it's a binary. Of course this simple
> test fails for any number of cases. For example, a shell script may not
> begin with the "#!" signature. If the file begins with a ":" some shells
> may assume it's a Bourne shell script. There are other variations on that
> theme but in my experience they're rare.

for the file permission --x--x--x, you are unable to read the first two
bytes. typically, I assume such a file was sent to system() to run it in
a unix shell, but if the file is a shell script and not binary
application, how unix shell can know it in advance and gives an error
message such as "permission denied".

Thanks,

peter


 0
peter
1/14/2004 7:20:00 AM
[This followup was posted to comp.unix.programmer]

In article <zu4Nb.9619$QP.4822@newssvr27.news.prodigy.com>, one2001boy@yahoo.com says... > Hello, > > Is there a way to find if a file is a binary executable file or an ASCII > executable file in unix? especially, if I have a file with > permission of --x--x--x. > > Thanks, > > Peter You could use the command "file" file foo.sh   0 Web 1/14/2004 2:16:54 PM peter wrote: > Thanks for your help. Barry. Unix shell are unable > to run the unix shell scripts with permission of --x--x--x, but the > binary file with permission --x--x--x can be executed. not sure how the > unix shell can tell the differences. That's because you have it backward. The invoking shell simply runs exec() on the file without knowing or caring what's in it. The kernel (which can do whatever it wants) then decides whether to map it directly as a process or hand it to an interpreter. In the event it goes to an interpreter and the interpreter can't open it, that program (which may be a shell or anything else but is not the original, invoking shell) will fail. MB   0 Mohun 1/14/2004 2:53:13 PM >[This followup was posted to comp.unix.programmer] > >In article <zu4Nb.9619$QP.4822@newssvr27.news.prodigy.com>,
>one2001boy@yahoo.com says...
>> Hello,
>>
>> Is there a way to find if a file is a binary executable file or an ASCII
>> executable file in unix? especially, if I have a file with
>>   permission of --x--x--x.
>>
>> Thanks,
>>
>> Peter
>
>You could use the command "file"
>
>    file foo.sh
>


 0
johns_tst
1/14/2004 3:14:11 PM
On Wed, 14 Jan 2004 07:20:00 +0000, peter wrote:

> for the file permission --x--x--x, you are unable to read the first two
> bytes.

You're correct. I should have stated explicitly that I was responding to
the more typical case where you do have read access.

> typically, I assume such a file was sent to system() to run it in a unix
> shell, but if the file is a shell script and not binary application, how
> unix shell can know it in advance and gives an error message such as
> "permission denied".

Shells as a rule do not use system(), they use exec() (or variant such as
execve()). The kernel will open the file if the user credentials allow it
to do so. It will then examine the contents to determine whether it is a
program in a binary format (e.g., COFF or ELF) that it can execute
directly or whether it needs to spawn a "shell" process to read and
interpret the contents of the file. If the user is not allowed to execute
the file it will return EACCES or EPERM. It can return many other errors
as well. It's the caller's responsibility to emit an appropriate error
message.

Some shells will attempt to open and read the file as an optimization. If
they recognize the file as a script they can interpret they may do so
directly rather than calling upon the fork() and exec() functions to spawn
the appropriate interpreter.

 0
Kurtis
1/16/2004 4:50:04 AM
* "Kurtis D. Rader" <krader@skepticism.us>
| Some shells will attempt to open and read the file as an
| optimization. If they recognize the file as a script they can
| interpret they may do so directly rather than calling upon the
| fork() and exec() functions to spawn the appropriate interpreter.

Which shell are you referring to?  Many (most?) of my shell scripts
rely on the fact that everything done in the script has no effect to
the shell where I typed the command (i.e. change directories, altering
the environment etc).  So I would be very surprised if the command
line shell was affected by the scripts I invoke.

R'

 0
Ralf
1/16/2004 9:15:00 AM
In article <ygabrp4jluz.fsf@ozelot.akutech-local.de>,
Ralf Fassel <ralfixx@gmx.de> wrote:

> | Some shells will attempt to open and read the file as an
> | optimization. If they recognize the file as a script they can
> | interpret they may do so directly rather than calling upon the
> | fork() and exec() functions to spawn the appropriate interpreter.
>
> Which shell are you referring to?  Many (most?) of my shell scripts
> rely on the fact that everything done in the script has no effect to
> the shell where I typed the command (i.e. change directories, altering
> the environment etc).  So I would be very surprised if the command
> line shell was affected by the scripts I invoke.

This was the way that shell scripts were first implemented; recognizing
"#!" in the kernel was a later modification to the OS.  What actually
happens is that the shell forks and calls exec(); if exec() returns with
an error that indicates that the file wasn't in a format that it
understands (which originally was just binary executables, but now
includes files beginning with "#!"), the shell tries to interpret it
itself.  It's still in the forked child process, so the original shell
process is not affected by it.

The shell may also do some special recognition of the file format.  In
particular, csh looks for "!" as the first character and will exec
/bin/sh to execute it.  ! is the Bourne Shell no-op command, and this
was used in the pre-#! days as a way for csh users to execute Bourne
scripts (this "!" is what eventually evolved into the shebang).

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA

 0
Barry
1/16/2004 6:46:36 PM
Barry Margolin wrote:
> The shell may also do some special recognition of the file format.  In
> particular, csh looks for "!" as the first character and will exec
> /bin/sh to execute it.  ! is the Bourne Shell no-op command, and this
> was used in the pre-#! days as a way for csh users to execute Bourne
> scripts (this "!" is what eventually evolved into the shebang).

Nice exposition, but I think you meant ':' instead of '!':

% !

-MB


 0
Mohun
1/16/2004 7:26:46 PM
In article <WVWNb.80617$8H.116458@attbi_s03>, Mohun Biswas <m.biswas@invalid.addr> wrote: > Barry Margolin wrote: > > The shell may also do some special recognition of the file format. In > > particular, csh looks for "!" as the first character and will exec > > /bin/sh to execute it. ! is the Bourne Shell no-op command, and this > > was used in the pre-#! days as a way for csh users to execute Bourne > > scripts (this "!" is what eventually evolved into the shebang). > > Nice exposition, but I think you meant ':' instead of '!': Yes. I knew when I was writing that post that something didn't seem quite right. :) -- Barry Margolin, barmar@alum.mit.edu Arlington, MA   0 Barry 1/17/2004 4:05:03 AM  Reply: Similar Artilces: file.exe file generation from file.m file How to generate application (*.exe) file from *.m file, which has lots of graphics. Program was written for image watermarking by using image processing toolbox. I want to send this program some where else but with out sowing the program codes. By this *.exe no need to use matlab6.5 platform. Also tell me How to generate *.p file from *.m file Which is hide the code to user but for run this program need matlab6.5 Please give me some idea about it. it is very urgent for me. I will be very great full to you "Biswajit Kar" <hibiswajitkar@rediffmail.com> wrote in message ne... file extention from file file location Hi, I am new to perl (only working on this one problem) but I program in other languages. We have an upload script written in cgi. Can anyone tell me how to get the file extension from the variable$File1 (actual file location on uploader's computer) then find out if it is equal to "txt" Thanks in advance for not telling me to read the manual. Ron -- PLEASE NOTE: comp.infosystems.www.authoring.cgi is a SELF-MODERATED newsgroup. aa.net and boutell.com are NOT the originators of the articles and are NOT responsible for their content. HOW TO POST to co...

Iteration through File.file? misses entries for which File.file?(entry) == true
Hello everyone, I'm new to Ruby, so I'm most likely making an elementary mistake. However, searching Google didn't help me find my answer, and after mucking around with irb I still haven't figured it out. I wrote a method that was intended to take all of the files in a given directory and put them into an array. Here's the code I wrote: def getFiles(dir) pwdFiles = Array.new Dir.foreach(dir) do |entry| pwdFiles.push(entry) if File.file?(entry) == true end end This works fine when called on the working directory --- that is, getFile...

How to copy a file to a file with a prefixed file name
I'm trying to write a program that will archive selected files to an archive directory. The files in the archive directory will have a prefix in the format yyyymmdd-hhmmss-filename.ext. So, if the file is called "somefile.dat", then the filename in the archive directory should be "20090522-164325-somefile.dat". I'm using Clipper's COPY FILE command to do the copy. I can't figure out how to get the prefix in front of the file name. Creating the prefix string is no problem, but I can't figure out how to tack that onto the front of the filename. If I...

Text files read multiple files into single file, and then recreate the multiple files
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one single file, but then be able to take this one single file and recreate the files I put into it. Im really at a loss as to how I go about recovering the files? obviously if i scan for a string that specifys the start and end of each file, theres the chance that the file might contain this term to which would split the files into unwanted chucks of file, which wouldnt be wanted. Any ideas? code snippets...

write file + input file + write file
Hi, I want to write some latex command to a file (file1), input the file at the end of the document. These command shall write to another file (file2) and at a second run I use the file2. Whatever I have tried nothing works and I don't known why. The goal is to save a set of special commands and run them at the end of the document, because they use some values which are only available at the end! Can anybody please help me with that? Thanks in advance! I should be something like: \begin{document} \usepackage{style} \input{file2} %file exists from a previous run \end{document} style.sty:...

File share, File Sharing, File Host
http://fileuplo.de File share, File Sharing, File Host -- questo articolo e stato inviato via web dal servizio gratuito http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it ...

object files, header files, source files?
I can't find any books describing the anatomy of C/C++ programs. What is an object file? Can it be generated from a header file?, what is linking?, why does a source file need an object file? etc. I have looked in The C programming Language, The C++ Programming Language, Accelerated C++, GNU make etc but they don't describe these basic subject. Any literature that covers these subjects? saneman wrote: > I can't find any books describing the anatomy of C/C++ programs. What is > an object file? Can it be generated from a header file?, what is > linking?, why does a s...

Unifying files and no files.
Is there an idiom that folds the no-files case into the files-exist case so they can be handled with a single mechanism? $bash --version GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc.$ ls $chmod a-x * chmod: cannot access *': No such file or directory$ I could do this $chmod a-x * 2>/dev/null$ but that just hides the error; there are still two cases (the exit status tells the tale). I could do this $chmod a-x * 2>/dev/null ; :$ but that's more hiding. This works $files=$(...

Why not FILE instead of FILE*?
Why is the standard C type for file handles FILE* instead of FILE? AFAIK the type FILE is a pre-defined typedef for some other type anyway. So why not make it instead a typedef for a *pointer* to that type? For one thing, that would stop people trying to poke around at the insides of a FILE. For another, it could allow for cases where the "real" type behind FILE is not a pointer to anything. -- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "It sure is cool having money and c...

Random file in a file
I am trying to take a random file from a folder what can I do to get random to find a file but not numbers? On Tue, 20 Oct 2015 19:24:03 +0000, Baldvin Vigfússon wrote: > I am trying to take a random file from a folder what can I do to get > random to find a file but not numbers? You could get a list of files in the folder, generate a random integer between 1 and the number of files, then use that RN to index the file list. Bruce ...

Is file a font file?
How can detect a file without extension is font file or not in cocoa? or any other I tried with CFStringRef fontNam=CFStringCreateWithCString(NULL,[getfile UTF8String],kCFStringEncodingMacRoman); ATSFontRef frefarr= ATSFontFindFromName((CFStringRef)[getArray objectAtIndex:rowcount] ,kATSOptionFlagsDefault); But frefarr gets nil any thing wrong please help, with thanks Excuse my language. Yabber <eyabber@gmail.com> wrote: > How can detect a file without extension is font file or not in cocoa? > > or any other > I tried with > > CFStringRef...

How to rebuild the dll file, I only have the dll file, no m file
when I load a dll built by older version matlab, it said Invalid MEX-file ,it's not a valid win32 application. From the matlabcentral, it says to rebuild this mex file then it can be loaded by matlab with version newer than 7 how to rebuild this mex file ? Now I do not have the source m code, but only the dll file xueqian xueqian wrote: > when I load a dll built by older version matlab, it said Invalid > MEX-file ,it's not a valid win32 application. From the matlabcentral, > it says to rebuild this mex file then it can be loaded by matlab with > version newer than 7 > ...

File.renameTo(File)
Thanks, I'm running 1.5 version of java and I'm simply attempting to move a file from a source directory to a target directory. It seems the renameTo works fewer times than it does. I print the absolute path to ensure they're correct and they are. Someone mentioned there's a renameTo bug. Is there a workaround? I'm thinking of exec'ing a process and running a dos move. Thank again. Sly On Tue, 4 Apr 2006 20:38:18 -0400, ":-o" <fcrutch@save-net.com> wrote, quoted or indirectly quoted someone who said : > >It seems the renameTo works few...

Web resources about - how to tell if a file is inary file or ascii file? - comp.unix.programmer

IBM 5100 - Wikipedia, the free encyclopedia
The IBM 5100 Portable Computer was a portable computer introduced in September 1975, six years before the IBM PC . It was the evolution of a ...

21 Feb 1941 - FILM WORLD Today's Shows
Today's Shows TODAY "The Great Dictator," which has attracted the biggest attendances in the history of the Plaza Theatre, will commence the ...

Tonight: Executive Chef Cristeta Comerford Becomes 2010 Chicago Chef of The Year
The White House has one piece of undiluted good ne ws after Election 2010, and of course it comes from the kitchen... UPDATE: Another laud for ...

￼Personal Projects and Free Traits: Personality and Motivation Reconsidered
Abstract I review a social ecological model of human development that stimulates recon- sideration of some traditional views in personality and ...