Solved

testing file types with if statement

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux rpm install 5 228
Problem to line 1 60
How to redirect a serial com to a KVM machine on Proxmox 3 436
usage of %{name} in rpm spec file 4 52
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…
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…
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 …

828 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