Skip to main content
Code Review

Return to Answer

Removed inaccurate remark
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

    As a bonus, you don't need to issue the rm commands explicitly, so you don't have to save the $EXIT_STATUS.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

    As a bonus, you don't need to issue the rm commands explicitly, so you don't have to save the $EXIT_STATUS.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    
deleted 30 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

    As a bonus, you don't need to issue the rm commands explicitly, so you don't have to save the $EXIT_STATUS.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file $TMPDIR/sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating $TMPDIR/sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

    As a bonus, you don't need to issue the rm commands explicitly, so you don't have to save the $EXIT_STATUS.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file $TMPDIR/sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating $TMPDIR/sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

    As a bonus, you don't need to issue the rm commands explicitly, so you don't have to save the $EXIT_STATUS.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

It's not guaranteed that $TMP_PROGRAM_FILE is on the $PATH, so you need to either set the $PATH or use an absolute path for $TMP_PROGRAM_FILE.

You have problems with mktemp.

  • -t option: It means different things on GNU/Linux mktemp(1) and Mac OS X mktemp(1).

    On GNU/Linux:

     -t interpret TEMPLATE as a single file name component,
     relative to a directory: $TMPDIR, if set; else the
     directory specified via -p; else /tmp [deprecated]
    

    On OS X:

     mktemp [-t prefix] template ...
     mktemp -t prefix
     -t prefix
     Generate a template (using the supplied prefix and TMPDIR if set) to create a filename tem-plate. template.
     plate.
    

    Perhaps the confusion is the reason why the GNU/Linux man page indicates that it is deprecated. I think you just want to run mktemp TEMPLATE without the -t option.

  • Cleanup: Use trap "rm $TMP_SOURCE_FILE $TMP_PROGRAM_FILE" EXIT as a more robust way to delete the temporary files, in case your script aborts before reaching the rm commands.

    As a bonus, you don't need to issue the rm commands explicitly, so you don't have to save the $EXIT_STATUS.

  • .c suffix: By writing TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c", you end up getting mktemp to create the file $TMPDIR/sourceXXXX. That file does not get cleaned up. Also, when you do cat > $TMP_SOURCE_FILE, you may be creating $TMPDIR/sourceXXXX.c, or worse, truncating an existing file.

    On GNU/Linux, a remedy would be to use the --suffix .c option. Unfortunately, the BSD/OS X version doesn't support it. One workaround is to rename the file after creation using mv -i — which might fail, but at least it's secure.

    A better workaround would be to use gcc -x c to tell GCC that it is C source code, without considering the filename extension.

    An even better solution would be...

  • Avoid $TMP_SOURCE_FILE altogether: gcc can take its source code from its standard input! Use - as the filename.

     gcc -o "$TMP_PROGRAM_FILE" -x c - <<- END_OF_SOURCE
     ...
     END_OF_SOURCE
    
default

AltStyle によって変換されたページ (->オリジナル) /