Bash read/write file descriptors — seek to start of file

前端 未结 7 1819
暗喜
暗喜 2020-12-08 08:48

I tried to use the read/write file descriptor in bash so that I could delete the file that the file descriptor referred to afterward, as such:

F=$(mktemp)
ex         


        
7条回答
  •  悲哀的现实
    2020-12-08 09:20

    If you ever do happen to want to seek on bash file descriptors, you can use a subprocess, since it inherits the file descriptors of the parent process. Here is an example C program to do this.

    seekfd.c

    #define _FILE_OFFSET_BITS 64
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char* argv[])
    {
        /* Arguments: fd [offset [whence]]
         * where
         * fd: file descriptor to seek
         * offset: number of bytes from position specified in whence
         * whence: one of
         *  SEEK_SET (==0): from start of file
         *  SEEK_CUR (==1): from current position
         *  SEEK_END (==2): from end of file
         */
        int fd;
        long long scan_offset = 0;
        off_t offset = 0;
        int whence = SEEK_SET;
        int errsv; int rv;
        if (argc == 1) {
            fprintf(stderr, "usage: seekfd fd [offset [whence]]\n");
            exit(1);
        }
        if (argc >= 2) {
            if (sscanf(argv[1], "%d", &fd) == EOF) {
                errsv = errno;
                fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
                exit(1);
            }
        }
        if (argc >= 3) {
            rv = sscanf(argv[2], "%lld", &scan_offset);
            if (rv == EOF) {
                errsv = errno;
                fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
                exit(1);
            }
            offset = (off_t) scan_offset;
        }
        if (argc >= 4) {
            if (sscanf(argv[3], "%d", &whence) == EOF) {
                errsv = errno;
                fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
                exit(1);
            }
        }
    
        if (lseek(fd, offset, whence) == (off_t) -1) {
            errsv = errno;
            fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
            exit(2);
        }
    
        return 0;
    }
    

提交回复
热议问题