首页 > 系统相关 >What is /Dev/Null in Linux?

What is /Dev/Null in Linux?

时间:2023-06-15 20:45:47浏览次数:62  
标签:What null dev standard Dev file output Null data

What is /Dev/Null in Linux?

https://www.geeksforgeeks.org/what-is-dev-null-in-linux/

 

所有物理设备对应的文件,放在dev目录下。

What is /dev?

In the Linux file system, everything is a file or a directory. Even devices are accessed as files. Your hard drive partitions, Pendrive, speakers, for all of these, there exists a file from which these are accessed. Now to understand how devices are accessed as files, think of it in this way: what do we do with a file? We read data from it and write data to it. A device like a speaker can input data to produce sound, a hard disk can be used to read and write data, a printer takes the input to print files, etc. Files and devices are similar in this way

 

/dev is a directory that stores all the physical and virtual devices of a Linux system. Physical devices are easy to understand, they are tangible devices like pen-drive, speakers, printers, etc. A Linux system also has virtual devices which act as a device but represent no physical device.

 

黑洞设备, 所有写入此设备的内容,都将消失。

What is /dev/null?

It is a virtual device, which has a special property: Any data written to /dev/null vanishes or disappears. Because of this characteristic, it is also called bitbucket or blackhole. Let us see a demonstration.

 

Redirect output to /dev/null

 

In this demo, you can see that we added some data, but that data was not stored. This particular characteristic of /dev/null has some use cases. Let’s see them.

 

主要用于丢弃输出, 删除文件并不常用。

Usage of /dev/null

Since it discards anything written to it, you can move files to /dev/null to delete them. But, this is not a widely used use case. It is mainly used to discard standard output and standard error from an output.

 

标准输出和标准错误。

What are standard output and standard error?

The output of a Linux command contains two streams of data: standard output (stdout) and standard error (stderr0), while the standard output is the normal output if the command has no errors, the standard error is the error generated by the command. Output may contain both standard output and standard input. Example of stdout and stderr:

 

Let’s run the following shell script

# /bin/sh
ls
apt update

 

The first command ran successfully, but the second one generated errors. Since stdout and stderr are two separate data streams, they can be handled separately.

 

丢弃标准输出

丢弃标准错误

丢弃两者

How to access stderr and stdout?

Stdout and stderr are streams of data, both of which are treated as a file in Linux. If you wish to perform any action on these, you use a file descriptor that uniquely identifies the file stream. The file descriptor for stdout is 1 and for stderr is 2.

To access them separately, let’s see the following command which is performed on the same script file which was used in the previous example.

./script.sh 1>stdout.txt

 

In this, you can see that the output contains standard errors but the standard output was not displayed. This is because the stdout stream was sent to the stdout.txt file, and you can see that the stdout.txt file contains the standard output.

While writing a shell script, we may want to discard the standard error from the output. Whatever stream we may want to suppress from the output, that stream of data can be written into /dev/null. You could write that data into another file, just like the above example, but if you have no use for that data, why would you want to waste memory on it, it’s better to discard it completely. Let’s see the following example

./script.sh 2>/dev/null

 

And just like that, we have removed the stderr from the output. If you wish to discard the complete output, you can use the following command

command >/dev/null 2>&1

The &> command redirects the output of the file descriptor which is mentioned on the left (in the above command, the output of 2) to the stream of file descriptor mentioned on the right-hand side. So, the output of stderr (2) gets redirected to stdout(1), which in turn gets written in /dev/null and thus gets destroyed.

 

 

 

调试日志控制

https://www.cnblogs.com/wanng/p/shell-dev-null.html

在脚本中,为了方便调试,经常会加一些日志打印的逻辑,有时这种调试日志还比较多,脚本测试通过之后,这些调试日志可能就删除或者注释掉了

这里提供一个小技巧,既不用删除也不用注释掉日志,同时执行脚本的时候还不会输出这些调试日志

比如: 当前目录有一个日志文件 log.txt,脚本的调试日志会以 echo " this is debug log" >> log.txt 的形式写入这个文件中

现在脚本功能测试通过了,调试日志不需要写入log.txt

可以这么做:原来的脚本原样不动,本地先删除 log.txt,然后执行 ln -s /dev/null ./log.txt 命令,该命令建立了一个 log.txt/dev/nulll的软连接,以后都有写入 log.txt 的内容实际都会写入 /dev/null ,而写入 /dev/null 的所有内容都会被丢弃掉

如果后面需要再次调试脚本,删除链接即可

 

判断命令是否存在

https://blog.csdn.net/inthat/article/details/124699933

#!/bin/bash

# fast fail.
set -eo pipefail

SHELLCHECK_VERSION=0.7.1
INSTALL_DIR="${HOME}/bin/"
mkdir -p "$INSTALL_DIR" || true

function install_shellcheck {
  if ! command -v shellcheck &> /dev/null; then
      MACHINE=$(uname -m);
      TMPFILE=$(mktemp)
      rm "$TMPFILE"
      mkdir -p "$TMPFILE"/
      curl -L -o "$TMPFILE"/out.xz "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.$(uname -s | tr '[:upper:]' '[:lower:]').${MACHINE}.tar.xz"
      tar -xf "$TMPFILE"/out.xz -C "$TMPFILE"/
      cp "${TMPFILE}/shellcheck-v${SHELLCHECK_VERSION}/shellcheck" "${INSTALL_DIR}/shellcheck"
      rm -rf "$TMPFILE"
      chmod +x "${INSTALL_DIR}"/shellcheck
  fi
}

install_shellcheck

 

/dev/null不应该用于清空文件

https://askubuntu.com/questions/435866/why-is-moving-directories-to-dev-null-dangerous

$ echo "this is my file" > test
$ cat test
this is my file

$ sudo mv test /dev/null
$ cat /dev/null
this is my file

# Fix this!
$ sudo rm /dev/null
$ sudo mknod -m 0666 /dev/null c 1 3

 

How do I completely silence a cronjob to /dev/null/?

* * * * *       root    /usr/local/sbin/mycommand.sh > /dev/null 2>&1

 

标签:What,null,dev,standard,Dev,file,output,Null,data
From: https://www.cnblogs.com/lightsong/p/17484060.html

相关文章

  • getResourceAsStream()返回值为null
    getResourceAsStream()返回值为nulljavaWeb项目,写文件下载的时候遇到getServletContext().getResourceAsStream("/store/"+filename)获取的值为null//获得读取本地文件的输入流in=getServletContext().getResourceAsStream("/store/"+filename);目录结构如下查......
  • Vue项目打包部署上线时devServer.proxy代理失效如何解决?使用nginx的proxy_pass 代理跨
    Vue项目打包部署上线时devServer.proxy代理失效如何解决?使用proxy_pass代理跨域转发前言本篇文章用于记录项目前端部署上线遇到的问题,包含对问题的思考、解决思路,以及从中获得的收获。正确的部署流程我也写了一篇文章,供大家参考使用宝塔将Vue2+Nodejs全栈项目打包部署到腾讯云服......
  • 数据库与Redgate SQL Toolbelt和Azure DevOps的持续集成
    理论与实践中的数据库CICI背后的理论是,如果我们每天多次将代码集成到共享存储库中,然后通过运行自动构建和后续测试来验证每个提交,那么我们会及早发现并根除问题,并提高软件的质量。本文的内容是关于设置管道,使您能够将CI理论付诸实践,用于数据库。当我在2013年发布关于这个主题的原始......
  • 海康SDK注册报错 Structure.getFieldOrder() on class com.xxx.sdk.HCNetSDK$NET_DVR_
    Structure.getFieldOrder()onclasscom.xxx.sdk.HCNetSDK$NET_DVR_DEVICEINFO_V30doesnotprovideenoughnames[0]海康依赖的版本较低,项目引用的较高,导致海康注册报错,所以降低jna版本 <dependency><groupId>net.java.dev.jna</groupId><artifactId>jna<......
  • 界面控件DevExtreme UI组件——增强的自定义功能
    在本文中,我们将回顾DevExtremeUI组件在v22.2版本主要更新中一系列与自定义相关的增强。DevExtreme拥有高性能的HTML5/JavaScript小部件集合,使您可以利用现代Web开发堆栈(包括React,Angular,ASP.NETCore,jQuery,Knockout等)构建交互式的Web应用程序。从Angular和Reac,到ASP.NETCore......
  • 关于xfs文件系统-在操作系统中遇到两个uuid一样的-挂载报错-wrong fs type, bad optio
    当操作系统中,出现了两个uuid一样的文件系统(笔者这里是xfs),那么默认就只能挂载成功一个[root@qq-5201351~]#blkid|grepxfs|grep1ea9e784-0692-403c-bed1-bf34a5a86a57/dev/nvme1n1:UUID="1ea9e784-0692-403c-bed1-bf34a5a86a57"BLOCK_SIZE="512"TYPE="xfs"/dev/nvme2......
  • Oracle反连接HASH JOIN ANTI NA会处理驱动表连接列null值
     Oracle反连接HASHJOINANTINA会处理驱动表连接列null值 这个现象和Oracle内连接HASHJOIN/半连接HASHJOINSEMI不处理驱动表连接列null值相反。反连接中无论一下哪个结论都一样:HASHJOINANTINAHASHJOINANTISNAHASHJOINRIGHTANTINAHASHJOINRIGHTANTISN......
  • An analysis of what are the drug targets for the treatment of systemic lupus ery
    Systemiclupuserythematosus(SLE)isanautoimmunediseasethatproduceslargeamountsofimmunecomplexesandautoantibodiesandcausesdamagetothekidneys,skin,joints,andcentralnervoussystem.CurrentSLEtreatmentismostlyglucocorticoid-ledd......
  • MariaDB/MySQL的null值条件和索引
    对于应用程序来说,像这样使用WHERE条件并不罕见:WHEREstatus='DELETED'ORstatusISNULL如果运行EXPLAIN,这样的条件通常只会导致type列显示为ref_or_null。然而,如果没有NULL检查,它将显示为ref。但是,这是否意味着执行过程中只会发生一个细小的变化,而查询仍然会非常快呢?答......
  • ios开发 :CUICatalog: Invalid asset name supplied: '(null)'
    _iconImage.image=[UIImageimageNamed:sourceDic[@“image”]];明明有图片,但还是提示这个图片名称不存在报错CUICatalog:Invalidassetnamesupplied:'(null)'打断点进去,显示_iconImage的值是nil添加断点定位到错误在109行-(NSArray*)messageTableSource{if(_mes......