5

When I am building software from source on a GNU+Linux system, during the ./configure stage I frequently see the following line:

checking for suffix of executables...

How do I create such a check in a bash script?

The reason I want to know this is that I want to create a makefile in which it compiles with suffix .exe on Cygwin, but no suffix on true GNU+Linux.

5

The test is done by compiling a small dummy C program and by checking how the compiler names the output file.

The following example is a simplified version of what configure is doing

#!/bin/sh

cat << EOT > dummy.c
int main(int argc, char ** argv) {
    return 0;
}
EOT

gcc -o dummy dummy.c

if [ -f dummy.exe ] ; then
  # exe
fi

I would suggest you to use autoconf to generate a configure script and use it for your purpose.

  • However, if someone places a file called dummy.exe in the local directory but the system is actually GNU+Linux, won't this say "exe" instead of "no extension"? – Muhammad Moaz Imtiaz Jul 3 '14 at 15:52
  • @MuhammadMoazImtiaz Sure as I said this is an a simplified example. You could create a unique temporary file with mktemp and delete it afterwards. – Matteo Jul 3 '14 at 19:25
-1

This will give you the extension of the file:

FILENAME=/tmp/testfile.exe
echo "${FILENAME##*.}"

result:

exe
  • 2
    This does not answer the question. The configure script tests which is the default for an unknown system. In your case you are just retrieving the extension for a known file. And moreover your code fails if the file does not have an extension. – Matteo Jul 3 '14 at 6:02
  • But isn't he asking for a bash script solution? – captcha Jul 3 '14 at 6:04
  • Yes and this is exactly what configure (a bash script) is doing. Calling gcc in a script or calling ls is the same thing both are executable. – Matteo Jul 3 '14 at 6:06
  • Clearly out of my depth here, I should write (and compile) some more code. Thanks Matteo. – captcha Jul 3 '14 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.