mysql - How to detect whether the command output is being used in unix pipe -
in mysql -e option prints different formatting when used output pipe.
without pipe
[root@localhost commands]# $mysql_connect_str -e 'select 0/8' +--------+ | 0/8 | +--------+ | 0.0000 | +--------+
with pipe -- no formatting.
[root@localhost commands]# $mysql_connect_str -e 'select 0/8'| more 0/8 0.0000
how mysql command detecting output going fed command through pipe ?
there system call determine characteristics of file handle:
int fstat (int fd, struct stat *buf); struct stat { dev_t st_dev; /* id of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user id of owner */ gid_t st_gid; /* group id of owner */ dev_t st_rdev; /* device id (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize file system i/o */ blkcnt_t st_blocks; /* number of 512b blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };
by inspecting st_mode bits, program can determine type of device connected to. see man 2 fstat
full details. in particular, there macros meant system independent distinguish between regular files, pipes, sockets, etc.
there system call determines whether file connection terminal:
int isatty(int fd);
description isatty() function tests whether fd open file descriptor referring terminal.
return value isatty() returns 1 if fd open file descriptor referring terminal; otherwise 0 returned, , errno set indicate error.
Comments
Post a Comment