简体   繁体   中英

how to fail command when the input command fails

With the below command, am trying to sort the file1 and file2 and giving the output to diff command. Sometimes sort command is failing due to temp directory space issue. Even if sort command fails its continuing with the the diff command and generating the difference.

Is there any way to stop the diff command if sort command fails due to some reason

diff <(sort file1) <(sort file2)

Also we are executing this command from a java program using the Runtime.getRuntime().exec() . How to capture the error in Java.

This is not a Java problem; bash doesn't yield an error status if a process substitution fails. In order to receive an error before even starting the diff , we could write, if you don't mind to have the files sorted permanently:

sort -o file1 file1 && sort -o file2 file2

(This would also reduce the space problem since not two temporary files need to be present.)
Then, if the sorts succeeded, we could run diff file1 file2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM