3

I want to redirect the output of a program to a file with variable name and, at the same time, redirect the possible error. This is the script so far:

#!/bin/bash

echo "Se ejecutará el PET-linux.x con PET.inp como input, debe indicarse el nombre del archivo output."
echo "Nombre del archivo .out: "
read outfile

./PET-linux.x < PET.inp > ${outfile}.out 2> /dev/null

echo $?

If I run the program knowing that it would make an error, I get this in the terminal:

samuel@samuel-P5Wx6:~/Documents/Fisica/19-20/Radiactividad/Prácticas/Practicas-MontCarlo/PET/pruebas$ ./ejecutarPET.sh 
Se ejecutará el PET-linux.x con PET.inp como input, debe indicarse el nombre del archivo output.
Nombre del archivo .out: 
test
./ejecutarPET.sh: line 7: 25715 Floating point exception(core dumped) ./PET-linux.x < PET.inp > ${outfile}.out 2> /dev/null
136

I know why this error happens, but I want it to not appear on the terminal like that. Can't I use 2> at the same time as >?

Thank you.

  • The shell running the script is reporting the error, not the PET-linux.x program itself. The redirection only applies to the PET-linux.x program. – icarus May 6 at 23:04
3

As has been pointed out in the comments, it is your shell script reporting the floating point exception from your application.

Modify the PET-linux.x line as follows, to catch the exception and exit silently. You can test for the exception by checking the return status for 136 and logging it as appropriate. (If you don't want the reporting, just omit that second line. I've included it so that you can see how you would identify a floating point exception programmatically if you needed to do so.)

( trap 'exit' SIGFPE; ./PET-linux.x < PET.inp > "$outfile.out" ) 2>/dev/null
[[ $? -eq 136 ]] && echo "Uh oh we caught a floating point exception" >&2
| improve this answer | |

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.