#!/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.
分享到:
相关推荐
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 ...
/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`命令添加...
例如,将"hello"发送到pts/1终端只需在pts/0上执行echo hello > /dev/pts/1。 设备文件的管理还包括对硬件操作,如备份和恢复分区表。使用fdisk -l可以列出硬盘分区信息,而dd命令可用于备份MBR(主引导记录),如...
if grep hello TestFile > /dev/null then echo "Found" else echo "NOT Found" fi ``` 这里使用 `> /dev/null` 的目的是隐藏 `grep` 命令的输出结果。如果 `grep` 找到了匹配项,则输出会被丢弃,而不会显示在...
#include <linux/tty.h> // tty 头文件,定义了有关tty_io,串行通信方面的参数、常数。 #include <linux/sched.h> // 调度程序头文件,定义了任务结构task_struct、第1 个初始任务 // 的数据。还有一些以宏的形式...
//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...
[ 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 ...
[ 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 ...
#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> #...
dd if=/dev/tty bs=1 count=1 2>/dev/null # 恢复终端模式为规范模式,并开启回显 stty cbreak echo } ``` #### 知识点四:核心逻辑 接下来,我们来看整个脚本的核心部分,这部分负责循环读取用户输入,并将...
char=$(dd if=/dev/tty bs=1 count=1 2>/dev/null) stty cbreak echo # 检查是否按下了回车键 if [ "$char" = "" ]; then echo # 输出换行符以美化输出格式 break fi # 将输入的字符添加到密码变量中 ...
下载 3,425 kB,耗时 2秒 (1,303 kB/s) Selecting previously unselected package libencode-locale-perl. (正在读取数据库 ... 系统当前共安装有 213805 个文件和目录。) Preparing to unpack .../libencode-locale...
[ 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 ...
if (ptty == NULL) { printf("Failed to open serial port.\n"); return -1; } // 设置波特率为9600 setTTYSpeed(ptty, B9600); // 设置数据位、校验位和停止位 setTTYParity(ptty, CS8, NOPARITY, ...
if (ret > 0 && FD_ISSET(fd, &rfds)) { ssize_t len = read(fd, buf, 100); } ``` 以上就是在Linux下进行串口编程的主要内容。通过理解这些基础知识,开发者可以更加灵活地在实际项目中运用串口通信技术。
[ 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 ...
mesg n 2> /dev/null || true ``` 修改为: ``` tty -s&&mesg n || true ``` 6. **测试root登录**: 完成上述步骤后,重启或注销系统,然后在登录界面选择“未列出”,输入`root`作为用户名,再输入你在第一...
- **特殊文件**:在Linux系统中,有一些特殊的文件,如 `/dev/null`、`/dev/tty` 等。 - `/dev/null`:用于丢弃不需要的数据。 - `/dev/tty`:代表当前终端设备。 - **环境变量配置**:在用户的主目录下,通常有一...
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。这些信号通常用于控制终端的输入输出和会话管理,忽略它们可以确保...