<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic check file in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959927#M77030</link>
    <description>1. how to use file command to check a file whether is binary file or C object files?&lt;BR /&gt;&lt;BR /&gt;2. how can we insulate a variable from any chracter that may follow it</description>
    <pubDate>Sat, 26 Apr 2003 13:59:48 GMT</pubDate>
    <dc:creator>neocosmic</dc:creator>
    <dc:date>2003-04-26T13:59:48Z</dc:date>
    <item>
      <title>check file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959927#M77030</link>
      <description>1. how to use file command to check a file whether is binary file or C object files?&lt;BR /&gt;&lt;BR /&gt;2. how can we insulate a variable from any chracter that may follow it</description>
      <pubDate>Sat, 26 Apr 2003 13:59:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959927#M77030</guid>
      <dc:creator>neocosmic</dc:creator>
      <dc:date>2003-04-26T13:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: check file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959928#M77031</link>
      <description>you can use the command "find"&lt;BR /&gt;ex:find / -type b|wc -l&lt;BR /&gt;Here are the usage as follows.&lt;BR /&gt;&lt;BR /&gt;    -type c                  True if the type of the file is c, where c is&lt;BR /&gt;                               one of:&lt;BR /&gt;                                    f    Regular file&lt;BR /&gt;                                    d    Directory&lt;BR /&gt;                                    b    Block special file&lt;BR /&gt;                                    c    Character special file&lt;BR /&gt;                                    p    FIFO (named pipe)&lt;BR /&gt;                                    l    Symbolic link&lt;BR /&gt;                                    s    Socket&lt;BR /&gt;                                    n    Network special file&lt;BR /&gt;                                    M    Mount point&lt;BR /&gt;</description>
      <pubDate>Mon, 28 Apr 2003 00:18:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959928#M77031</guid>
      <dc:creator>Stanley_8</dc:creator>
      <dc:date>2003-04-28T00:18:07Z</dc:date>
    </item>
    <item>
      <title>Re: check file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959929#M77032</link>
      <description>1) You have to trap the output of the command, and compare it against a string or regular expression in some manner.&lt;BR /&gt;&lt;BR /&gt;e.g.&lt;BR /&gt;&lt;BR /&gt;MYFILE=/path/to/file&lt;BR /&gt;if [ "$(expr "$(file ${MYFILE})" : '.*: \(.*\)')" = "ASCII C program text" ]&lt;BR /&gt;then&lt;BR /&gt;  echo "C code"&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;lets break this down a bit.&lt;BR /&gt;&lt;BR /&gt;The core of this is the 'file' command:&lt;BR /&gt;&lt;BR /&gt;file ${MYFILE}&lt;BR /&gt;&lt;BR /&gt;This executes the 'file' command on the file listed above, in this case '/path/to/file'.&lt;BR /&gt;&lt;BR /&gt;If it's C source code, it will return something like this:&lt;BR /&gt;&lt;BR /&gt;/path/to/file: ASCII C program text&lt;BR /&gt;&lt;BR /&gt;We then pass this as an argument to the 'expr' command:&lt;BR /&gt;&lt;BR /&gt;expr "/path/to/file: ASCII C program text" : '.*: \(.*\)'&lt;BR /&gt;&lt;BR /&gt;This will do some regular expression pattern matching, breaking it up at the space after the : (Regular Expressions are a whole different topic!).  It returns the string after the space, so this will return:&lt;BR /&gt;&lt;BR /&gt;ASCII C program text&lt;BR /&gt;&lt;BR /&gt;We then use the 'if' command to check the final output.&lt;BR /&gt;&lt;BR /&gt;Throughout this, I use the $() inbuilt bash function to launch sub-shells to execute the commands inline.  This is an easy way to get the STDOUT of one command into a variable, or other things, such as here - an argument to another command.&lt;BR /&gt;&lt;BR /&gt;This is one method to do what you are asking.  You should hopefully be able to get a few ideas of other ways to do this.&lt;BR /&gt;&lt;BR /&gt;2) The example above shows you how to do this.  It's the ${} method of referencing variables:&lt;BR /&gt;&lt;BR /&gt;${MYNAME}&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;&lt;BR /&gt;$MYNAMEis = a variable called 'MYNAMEis'&lt;BR /&gt;&lt;BR /&gt;${MYNAME}is = the contents of variable 'MYNAME' followed directly by the word is.&lt;BR /&gt;&lt;BR /&gt;The variable referencing like this also has many other functions in bash and ksh usage.  The above example could be written differently, like this:&lt;BR /&gt;&lt;BR /&gt;MYFILE=/path/to/file&lt;BR /&gt;MYTYPE=$(file ${MYFILE})&lt;BR /&gt;if [ "${MYTYPE#*: }" = "ASCII C program text" ]&lt;BR /&gt;then&lt;BR /&gt;  echo "C code"&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;This isn't all that portable however, only working on "Bourne Again" and "Korn" shells, but it is possible.&lt;BR /&gt;&lt;BR /&gt;Shell scripting is fun stuff isn't it? :)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Also, I've noticed that over the last few days that you've been asking a fair number of questions.  How about showing your thanks by assigning some points to the many people who've taken time out to give you a hand.  I'm sure they'd appreciate it.</description>
      <pubDate>Mon, 28 Apr 2003 01:49:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/check-file/m-p/2959929#M77032</guid>
      <dc:creator>Stuart Browne</dc:creator>
      <dc:date>2003-04-28T01:49:14Z</dc:date>
    </item>
  </channel>
</rss>

