如何检测命令输出是否正在unix管道中使用
How to detect whether the command output is being used in unix pipe
在 MySql 中,-e 选项在使用带有管道的输出时打印不同的格式。
没有管道
[root@localhost commands]# $MYSQL_CONNECT_STR -e 'select 0/8'
+--------+
| 0/8 |
+--------+
| 0.0000 |
+--------+
使用管道 -- 无格式。
[root@localhost commands]# $MYSQL_CONNECT_STR -e 'select 0/8'| more
0/8
0.0000
mysql 命令如何检测到输出将通过管道提供给另一个命令?
它检查 tty
查看以下链接了解更多信息
How do I detect if stdout is connected to a tty in Perl?
存在判断文件句柄特征的系统调用:
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 for 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 */
};
通过检查 st_mode 位,程序可以确定它连接的设备类型。有关完整详细信息,请参阅 man 2 fstat
。特别是,有些宏旨在与系统无关,以区分常规文件、管道、套接字等。
还有另一个系统调用可以确定文件连接是否到终端:
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor referring to a terminal.
RETURN VALUE
isatty() returns 1 if fd is an open file descriptor referring to a terminal; otherwise 0 is returned, and errno is set to indicate the error.
在 MySql 中,-e 选项在使用带有管道的输出时打印不同的格式。
没有管道
[root@localhost commands]# $MYSQL_CONNECT_STR -e 'select 0/8'
+--------+
| 0/8 |
+--------+
| 0.0000 |
+--------+
使用管道 -- 无格式。
[root@localhost commands]# $MYSQL_CONNECT_STR -e 'select 0/8'| more
0/8
0.0000
mysql 命令如何检测到输出将通过管道提供给另一个命令?
它检查 tty
查看以下链接了解更多信息
How do I detect if stdout is connected to a tty in Perl?
存在判断文件句柄特征的系统调用:
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 for 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 */
};
通过检查 st_mode 位,程序可以确定它连接的设备类型。有关完整详细信息,请参阅 man 2 fstat
。特别是,有些宏旨在与系统无关,以区分常规文件、管道、套接字等。
还有另一个系统调用可以确定文件连接是否到终端:
int isatty(int fd);
DESCRIPTION The isatty() function tests whether fd is an open file descriptor referring to a terminal.
RETURN VALUE isatty() returns 1 if fd is an open file descriptor referring to a terminal; otherwise 0 is returned, and errno is set to indicate the error.