Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
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 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 at 19:25

This will give you the extension of the file:

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

result:

exe
share|improve this answer
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 at 6:02
    
But isn't he asking for a bash script solution? –  captcha Jul 3 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 at 6:06
    
Clearly out of my depth here, I should write (and compile) some more code. Thanks Matteo. –  captcha Jul 3 at 6:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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