1

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"
desertpancake
  • 23
  • 1
  • 1
  • 7
  • Does this answer your question? [Check if a file is executable](https://stackoverflow.com/questions/10319652/check-if-a-file-is-executable) – ti7 Nov 03 '20 at 18:47

2 Answers2

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)

ti7
  • 16,375
  • 6
  • 40
  • 68
  • validate.sh is executable. I just used the command you provided to check. validate.sh was a script used for a different assignment, and I used the command "chmod +x" – desertpancake Nov 03 '20 at 19:00
  • @desertpancake Sorry, I was initially wrong and updated my answer a great deal with more details! Specifically `file` doesn't check permissions, but the contents of the file – ti7 Nov 03 '20 at 19:05
0

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
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18