Solved

testing file types with if statement

Posted on 2002-03-16
4
153 Views
Last Modified: 2010-04-22
Hi all, I want to know how to test if a file is a text file or any other type in a "if" statement (In a shell script) ?
ex: if [ test.wkr is text ] then do....
0
Comment
Question by:mabell
  • 2
4 Comments
 
LVL 4

Expert Comment

by:ct.smith
ID: 6873561
This sort of thing, if available, will be specific to the API you're using.  Most languages don't offer anything beyond opening a file as text or binary, which is decided at compile time.

Anyways, you migth have some luck if you look for references to handling or discovering mime types in whatever API/library/language you're using.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 6873984
man test will give you all the answers, the usage of this test depends on your shell (mainly csh vs. sh variants), for example to test a pipe:

   if [ -p file ] then echo is PIPE

If you menan something like "content is word .doc" format vs. "content is plain ASCII" format, use the file command: man file
(keep in mind that the result of the file command highly depends on the quality of your /etc/magic file.
0
 

Author Comment

by:mabell
ID: 6876967
Ok thx :)
I do this (to test if a file is a text file) :
fileTest=$(file $1/$2|awk '{printf $3}')
if [ $fileTest = "text," ]
then
...

were $1 is a directory and $2 is a file passed in parameters
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6877950
oops, take care with
     file $1/$2|awk '{printf $3}'
it may produce fals positives, for example see:
     awk '($5 == "text"){print}' /etc/magic
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now