shell脚本:shell的基本元素-7 特殊文件:/dev/null与/dev/tty

本文介绍了UNIX系统中两个特殊的文件:/dev/null 和 /dev/tty。/dev/null常用于丢弃不需要的数据,而/dev/tty则用于读取人工输入,如密码等敏感信息。文章还详细解释了如何使用这些文件进行shell编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    UNIX系统提供了两个对shell编程特别有用的特殊文件。第一个文件/dev/null,就是大家所熟知的位桶(bit bucket).传送到此文件的数据都会被系统丢掉。也就是说,当程序将数据写到此文件时,会认为它已完成了写入数据的操作,但实际上什么事都没做。如果需要的是命令的退出状态,而非它的输出,此功能会很有用。
    相对地,读取/dev/null则会立即返回文件结束符号(end-of-life).读取/dev/null的操作很少会出现在shell程序里,不过了解这个文件的行为模式还是非常重要的。
    另一个特殊文件为/dev/tty。当程序打开此文件时,UNIX会自动将它重定向到一个终端(一个实体的控制台或串行端口,也可能是一个通过网络与窗口登陆的伪终端)再与程序结合。这在程序必须读取人工输入时特别有用。此外,用它来产生错误信息也很方便,只是比较少人这么做:
    printf "enter new password: "   提示输入
    stty -echo                      关闭自动打印输入字符的功能
    read pass < /dev/tty            读取密码
    printf "enter again: "          提示在输入一次
    read padd2 < /dev/tty           在读取一次以确认
    stty echo                        别忘了打开自动打印输入字符功能
    stty(set tty)命令用来控制终端的各种设置。-echo选项用来关闭自动打印每个输入字符的功能,stty echo 用来恢复该功能
tcgetattr() failed 2025-06-11 15:27:31.025 5420-5420 SerialPort com.silan.chuankou E native open returns null 2025-06-11 15:27:31.024 5420-5420 .silan.chuankou com.silan.chuankou I type=1400 audit(0.0:1578): avc: denied { open } for path="/dev/bus/usb/004/005" dev="tmpfs" ino=60129 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:usb_device:s0 tclass=chr_file permissive=1 2025-06-11 15:27:31.026 5420-5420 System.err com.silan.chuankou W java.io.IOException: native openSerialPort(/dev/bus/usb/004/005) returns null 2025-06-11 15:27:31.026 5420-5420 System.err com.silan.chuankou W at android_serialport_api.SerialPort.<init>(SerialPort.java:122) 2025-06-11 15:27:31.026 5420-5420 System.err com.silan.chuankou W at android_serialport_api.SerialPort.<init>(SerialPort.java:154) 2025-06-11 15:27:31.026 5420-5420 System.err com.silan.chuankou W at com.silan.chuankou.MainActivity.openSerialPort(MainActivity.java:310) 2025-06-11 15:27:31.026 5420-5420 System.err com.silan.chuankou W at com.silan.chuankou.MainActivity.lambda$initUI$1$com-silan-chuankou-MainActivity(MainActivity.java:99) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at com.silan.chuankou.MainActivity$$ExternalSyntheticLambda7.onClick(D8$$SyntheticClass:0) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at android.view.View.performClick(View.java:5637) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1218) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at android.view.View$PerformClick.run(View.java:22445) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at android.os.Handler.handleCallback(Handler.java:755) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at android.os.Handler.dispatchMessage(Handler.java:95) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at android.os.Looper.loop(Looper.java:154) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at android.app.ActivityThread.main(ActivityThread.java:6141) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at java.lang.reflect.Method.invoke(Native Method) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 2025-06-11 15:27:31.027 5420-5420 System.err com.silan.chuankou W at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值