简体   繁体   中英

Detecting if a shell redirect command runs out of diskspace?

I need to do a

sort big-file | uniq > big-file.temp

called from a C program via system. Does this report an error when the file system gets full?

A man 2 "write" only returns 0 if it can't write all the data, but /dev/null always returns 0 and the bash is not reporting an error when piping into it, so i guess i will not receive an error notification.

If the disk fills up, uniq will get an error when it's writing to stdout, and it should then exit with a non-zero exit code. The return value of system() is the termination status of the last command in the pipeline. So if the command is successful, system() will return 0 , if the command is not successful it will return some non-zero value.

So you can do:

ret = system('sort big-file | uniq > big-file.temp');
if (ret != 0) {
    fprintf(stderr, "Sorting failed!\n");
    exit(1);
}

BTW, sort has a -u option to generate unique results, this is probably more efficient than sorting everything and then piping to uniq .

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