`
e_soft
  • 浏览: 48768 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

"if `tty >/dev/null 2>&1`"

 
阅读更多
#!/bin/bash

if `tty >/dev/null 2>&1` ; then
        echo "interactive"
else
        echo "non-interactive"
fi
---------------------------- cut here ----------------------------
The "if `tty >/dev/null 2>&1`" statement is one of those cute little Unix tricks that requires a little explanation. Knowing that the tty command will either yield a string such as "/dev/pts/2" or will issue the phrase "not a tty", we need to determine which response we get to determine whether or not we have a controlling terminal.


=One way we could do this would be to construct an (excuse the pun!) awkward command such as this:


if [ `tty | awk -F/ '{print $2}'` == "dev" ]; then
This command would strip out the string "dev" from "/dev/pts/2" or, in the case of a non-interactive execution, yield a null string. This would work, but the command used in our skeletal script is considerably more efficient. Let's look at it again:



if `tty >/dev/null 2>&1` ; then
By tossing out the output from the tty command, the "tty >/dev/null 2>&1" command leaves us with only the return code from the tty command to evaluate. If the tty command is run interactively (i.e., with a controlling terminal), the return code is 0 (success). When run non-interactively (i.e., no controlling terminal), the return code is 1. Our simple logic boils down to "if true, do this, otherwise". This is, of course, the essence of any if statement, but we have arrived at our determination of true/false with very little processing and very little coding.

Thanks to Brian Hatch for recommending use of the tty command for determining whether a script is being run interactively.

分享到:
评论

相关推荐

    服务器部署中间件及依赖包的脚本,在centos 7.6系统可用

    dd if=/dev/tty bs=1 count=1 2> /dev/null stty -raw stty echo stty $SAVEDSTTY } wt(){ SCRIPT=$(whiptail --title "CheckList" --menu \ "Choosed deploy mode" 15 60 2 \ "01-single" "Single deploy ...

    HACMP安装手册.pdf

    /usr/sbin/no -o extendednetstats=0 >> /dev/null 2>&1 /usr/sbin/no -o ipsendredirects=0 /usr/sbin/no -o ipforwarding=0 fi ``` **2. 串口配置:** - **添加串口设备:** - 使用`#smittytty`命令添加...

    RHCE-文件系统介绍

    例如,将"hello"发送到pts/1终端只需在pts/0上执行echo hello > /dev/pts/1。 设备文件的管理还包括对硬件操作,如备份和恢复分区表。使用fdisk -l可以列出硬盘分区信息,而dd命令可用于备份MBR(主引导记录),如...

    Linux Shell常用技巧

    if grep hello TestFile > /dev/null then echo "Found" else echo "NOT Found" fi ``` 这里使用 `> /dev/null` 的目的是隐藏 `grep` 命令的输出结果。如果 `grep` 找到了匹配项,则输出会被丢弃,而不会显示在...

    linux内核 0.11版本源码 带中文注释

    #include <linux/tty.h> // tty 头文件,定义了有关tty_io,串行通信方面的参数、常数。 #include <linux/sched.h> // 调度程序头文件,定义了任务结构task_struct、第1 个初始任务 // 的数据。还有一些以宏的形式...

    Quectel_LTE&5G_USB_Driver_V2.1.zip

    //ttyUSB0 -> ../../devices/soc0/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1:1.0/ttyUSB0/tty/ttyUSB0 pchar = strstr(syspath, ":1.0/tty"); //MDM if (pchar == NULL) { pchar = strstr(sys...

    ap6212a0_a33_sc3817r_神舟验证版本_借用nvram_ap6210这个配置文件_20170626_1834没有外层目录.7z

    [ 3.968638] [mmc]: sdc2 set ios: clk 25000000Hz bm PP pm ON vdd 3.3V width 1 timing LEGACY(SDR12) dt B [ 3.968734] [mmc]: mclk 0xf1c20090 0xc100000b [ 3.989421] Bluetooth: BNEP filters: protocol ...

    ap6212a0_bb16v3_sina33验证通过BT的功能_wifi部分有问题_20170626_1148没有外层目录.7z

    [ 3.968638] [mmc]: sdc2 set ios: clk 25000000Hz bm PP pm ON vdd 3.3V width 1 timing LEGACY(SDR12) dt B [ 3.968734] [mmc]: mclk 0xf1c20090 0xc100000b [ 3.989421] Bluetooth: BNEP filters: protocol ...

    linux-0.11 [内核源代码带英文注解]

    #include <linux/tty.h> #include <linux/sched.h> #include <linux/head.h> #include <asm/system.h> #include <asm/io.h> #include <stddef.h> #include <stdarg.h> #include <unistd.h> #include <fcntl.h> #...

    Shell脚本中实现把输入的密码转换为*(星号)的方法

    dd if=/dev/tty bs=1 count=1 2>/dev/null # 恢复终端模式为规范模式,并开启回显 stty cbreak echo } ``` #### 知识点四:核心逻辑 接下来,我们来看整个脚本的核心部分,这部分负责循环读取用户输入,并将...

    shell脚本实现输入密码显示星号

    char=$(dd if=/dev/tty bs=1 count=1 2>/dev/null) stty cbreak echo # 检查是否按下了回车键 if [ "$char" = "" ]; then echo # 输出换行符以美化输出格式 break fi # 将输入的字符添加到密码变量中 ...

    linux全志R16的linux系统编译的资料_20170502_1655.7z

    下载 3,425 kB,耗时 2秒 (1,303 kB/s) Selecting previously unselected package libencode-locale-perl. (正在读取数据库 ... 系统当前共安装有 213805 个文件和目录。) Preparing to unpack .../libencode-locale...

    ap6212a0_a33_sc3817r_服务器验证通过_bt已经通了_wifi需要修改配置_需要再次验证_20170626_1549.7z

    [ 3.968638] [mmc]: sdc2 set ios: clk 25000000Hz bm PP pm ON vdd 3.3V width 1 timing LEGACY(SDR12) dt B [ 3.968734] [mmc]: mclk 0xf1c20090 0xc100000b [ 3.989421] Bluetooth: BNEP filters: protocol ...

    linuxc串口编程

    if (ptty == NULL) { printf("Failed to open serial port.\n"); return -1; } // 设置波特率为9600 setTTYSpeed(ptty, B9600); // 设置数据位、校验位和停止位 setTTYParity(ptty, CS8, NOPARITY, ...

    linux串口编程说明

    if (ret > 0 && FD_ISSET(fd, &rfds)) { ssize_t len = read(fd, buf, 100); } ``` 以上就是在Linux下进行串口编程的主要内容。通过理解这些基础知识,开发者可以更加灵活地在实际项目中运用串口通信技术。

    !!!!ap6212a0_a33_sc3817r_验证通过_修正wifi的配置文件为nvram_ap6212.txt

    [ 3.968638] [mmc]: sdc2 set ios: clk 25000000Hz bm PP pm ON vdd 3.3V width 1 timing LEGACY(SDR12) dt B [ 3.968734] [mmc]: mclk 0xf1c20090 0xc100000b [ 3.989421] Bluetooth: BNEP filters: protocol ...

    Ubuntu20.04开启root账户的方法步骤

    mesg n 2> /dev/null || true ``` 修改为: ``` tty -s&&mesg n || true ``` 6. **测试root登录**: 完成上述步骤后,重启或注销系统,然后在登录界面选择“未列出”,输入`root`作为用户名,再输入你在第一...

    shell脚本1

    - **特殊文件**:在Linux系统中,有一些特殊的文件,如 `/dev/null`、`/dev/tty` 等。 - `/dev/null`:用于丢弃不需要的数据。 - `/dev/tty`:代表当前终端设备。 - **环境变量配置**:在用户的主目录下,通常有一...

    copy_and-paste

    dd if=/dev/tty bs=1 count=$(tput linenum) 2>/dev/null | xsel -ib ``` 要粘贴,可以使用`xsel`或`pbpaste`(在MacOS上): ``` xsel -opb | bash ``` 3. **使用tmux或screen**:这些终端多路复用器提供了...

    嵌入式系统中守护进程的编写及调试

    1. **屏蔽控制终端信号**:守护进程不应受到控制终端(TTY)操作的影响,因此需要忽略与TTY交互相关的信号,如SIGTTOU、SIGTTIN、SIGTSTP和SIGHUP。这些信号通常用于控制终端的输入输出和会话管理,忽略它们可以确保...

Global site tag (gtag.js) - Google Analytics