Solved

testing file types with if statement

Posted on 2002-03-16
4
152 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Optimize bash script 20 456
How to import linux vhd to sccvmm 1 150
What is the timeframe of this cron job? 2 130
BASH script to modify crontab? 3 47
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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

13 Experts available now in Live!

Get 1:1 Help Now