skip to Main Content

I have an assignment wherein I am required to use the file command to check if a file is an executable type or not.

The wording used in the assignment is as follows:

The file command should be used to determine generically what kind of file the argument is and in particular if the file is an executable type or not. The output of the file command will include the special "executable" keyword if the file is an executable type.

Further on in the question, it states:

  1. Use file and look for the "executable" keyword in the output to determine if the file is an executable type or not.

I have so far been unable to find any documentation that explains how to do this. When I test this on a bash script in CentOS8, it returns this as output:

validate.sh: ASCII text

Edit1/found_the_answer_edit: I tried using ti7’s idea of using a test script and it got the same output as what they had gotten. Which got me thinking. The scripts for my assignments are required to have some formatting done to them. Specifically, in order to get credit for our scripts, we must have:

#START SCRIPT
#username
``
at the beginning of the file and:

#END SCRIPT

at the end of it. I deleted these bits in my validate.sh file and when I tested it again with the file command I got the same output that I got from the test script. So the answer is that there isn't anything special. File just can't deal with a file that doesn't start with "#!/bin/bash"

2

Answers


  1. Your script validate.sh probably isn’t executable as file determines it! This is probably because the header is wrong on it

    % cat test.sh
    #!/bin/bash
    echo "foo"
    % file test.sh
    test.sh: Bourne-Again shell script text executable, ASCII text
    

    Once that’s settled, you can check for the executable keyword in the response, perhaps with grep

    % file test.sh | grep -q executable ; echo $?
    0
    % touch test-empty.sh
    % file test-empty.sh | grep -q executable ; echo $?
    1
    

    If you’re not forced to use file, using test’s -x arg may be much better as suggested here, as file doesn’t check permissions, rather that the file format is executable

    Note the test command command is actually [ or [[ with subtle differences

    [[ -x "validate.sh" ]] && echo "executable" || echo "not executable"
    

    You can check the permissions on it with ls, which lists files in a directory and optionally properties of them

    ls -lhaF
    

    You can make your script executable with chmod (trivially, chmod +x)

    Login or Signup to reply.
  2. Not sure if this is actually bash running on Windows and so maybe WSL. If it is, file will actually return that a given Windows exe file is executable.

    i.e.

    file write.exe
    
    write.exe: PE32+ executable (GUI) x86-64, for MS Windows
    

    With this in mind, you can pipe through to grep and test the output i.e.

    file write.exe | grep -q executable && echo "Executable" || echo "Not executable"
    
    Executable
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search