本文将带您了解关于linux下/dev/null被误删的新内容,同时我们还将为您解释linuxdev/null的相关知识,另外,我们还将为您提供关于2>/dev/null和>/dev/null2>&1
本文将带您了解关于linux下/dev/null被误删的新内容,同时我们还将为您解释linux dev/null的相关知识,另外,我们还将为您提供关于2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null、Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...、failed to parse: {"row":[8,6,12,7,13,8,14],"meta":[null,null,null,null,null,null,null]}]} 11:46:29.8、linux /dev 常见特殊设备介绍与应用 [loop,null,zero,full,random]的实用信息。
本文目录一览:- linux下/dev/null被误删(linux dev/null)
- 2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null
- Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...
- failed to parse: {"row":[8,6,12,7,13,8,14],"meta":[null,null,null,null,null,null,null]}]} 11:46:29.8
- linux /dev 常见特殊设备介绍与应用 [loop,null,zero,full,random]
linux下/dev/null被误删(linux dev/null)
2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null
Linux中的标准输入输出
标准输入0 从键盘获得输入 /proc/self/fd/0
标准输出1 输出到屏幕(即控制台) /proc/self/fd/1
错误输出2 输出到屏幕(即控制台) /proc/self/fd/2
/dev/null代表linux的空设备文件,所有往这个文件里面写入的内容都会丢失,俗称“黑洞”
1、2>/dev/null意思就是把错误输出到“黑洞”
2、>/dev/null 2>&1默认情况是1,也就是等同于1>/dev/null 2>&1。意思就是把标准输出重定向到“黑洞”,还把错误输出2重定向到标准输出1,也就是标准输出和错误输出都进了“黑洞”
3、2>&1 >/dev/null意思就是把错误输出2重定向到标准出书1,也就是屏幕,标准输出进了“黑洞”,也就是标准输出进了黑洞,错误输出打印到屏幕 关于这里”&”的作用,我们可以这么理解2>/dev/null重定向到文件,那么2>&1,这里如果去掉了&就是把错误输出给了文件1了,用了&是表明1是标准输出。--------------------- 作者:中琦2513 来源:CSDN 原文:https://blog.csdn.net/zhongqi2513/article/details/78613768 版权声明:本文为博主原创文章,转载请附上博文链接!
Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...
http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21
Just looking for the difference between
2>&-
2>/dev/null
|&
&>/dev/null
>/dev/null 2>&1
and their portability with non-Bourne shells
like tcsh
, mksh
, etc.
For background:
- a number 1 = standard out (i.e. STDOUT)
- a number 2 = standard error (i.e. STDERR)
- if a number isn''t explicitly given, then number 1 is assumed by the shell (bash)
First let''s tackle the function of these. For reference see the Advanced Bash-Scripting Guide.
Functions
2>&-
The general form of this one is M>&-
, where "M" is a file descriptor number. This will close output for whichever file descriptor is referenced, i.e. "M".
2>/dev/null
The general form of this one is M>/dev/null
, where "M" is a file descriptor number. This will redirect the file descriptor, "M", to /dev/null
.
2>&1
The general form of this one is M>&N
, where "M" & "N" are file descriptor numbers. It combines the output of file descriptors "M" and "N" into a single stream.
|&
This is just an abbreviation for 2>&1 |
. It was added in Bash 4.
&>/dev/null
This is just an abbreviation for >/dev/null 2>&1
. It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null
.
>/dev/null
This is just an abbreviation for 1>/dev/null
. It redirects file descriptor 1 (STDOUT) to /dev/null
.
Portability to non-bash, tcsh, mksh, etc.
I''ve not dealt much with other shells outside of csh
and tcsh
. My experience with those 2 compared to bash''s redirection operators, is that bash is superior in that regard. See the tcsh man page for more details.
Of the commands you asked about none are directly supported by csh/tcsh. You''d have to use different syntaxes to construct similar functions.
failed to parse: {"row":[8,6,12,7,13,8,14],"meta":[null,null,null,null,null,null,null]}]} 11:46:29.8
IDEA整合springboot与neo4j时出现这个问题
linux /dev 常见特殊设备介绍与应用 [loop,null,zero,full,random]
linux 是文件型系统,所有硬件如软件都会在对于的目录下面有相应的文件表示。对于 dev 这个目录,我们知道它下面的文件,表示的是 linux 的设备。在 windows 系统中,设备大家很好理解,象硬盘,磁盘指的是实实在在硬件。而在文件系统的 linux 下面,都有对于文件与这些设备关联的。访问它们就可以放到实际硬件,想想还是 linux 灵活了。变成文件,操作该多简单了。不用调用以前 com,prt 等接口了。直接读文件,写文件就可以向设备发送读或者写操作了。 按照读写存储数据方式,我们可以把设备分为以下几种:字符型设备,块设备,伪设备。
一、设备分类
字符型设备
字符设备是指每次与系统传输 1 个字符的设备。这些设备节点通常为传真、虚拟终端和串口调制解调器、键盘之类设备提供流通信服务,它通常不支持随机存取数据。 字符设备在实现时,大多不使用缓存器。系统直接从设备读取/写入每一个字符。例如,键盘这种设备提供的就是一个数据流,当你敲入 “cnblogs” 这个字符串时,键盘驱动程序会按照和输入完全相同的顺序返回这个由七个字符组成的数据流。它们是顺序的,先返回 c,最后是 s。
块设备
块设备是指与系统间用块的方式移动数据的设备。这些设备节点通常代表可寻址设备,如硬盘、CD-ROM 和内存区域。
块设备通常支持随机存取和寻址,并使用缓存器。操作系统为输入输出分配了缓存以存储一块数据。当程序向设备发送了读取或者写入数据的请求时,系统把数据中的每一个字符存储在适当的缓存中。当缓存被填满时,会采取适当的操作(把数据传走),而后系统清空缓存。它与字符设备不同之处就是,是否支持随机存储。字符型是流形式,逐一存储。
伪设备
在类 Unix 操作系统中,设备节点并不一定要对应物理设备。没有这种对应关系的设备是伪设备。操作系统运用了它们提供的多种功能。部份经常使用到的伪设备包括: null,zero,full,loop,random,urandom
二、特殊设备及使用
这里说到特殊设备除了硬盘主板等,只是它在 linux shell 命令里面,有特殊的作用,因此把它们单独拿出来。这些设备分别是:
/dev/stdin
/dev/stdout
/dev/stderr
/dev/null
/dev/zero
/dev/full
/dev/random,urandom
/dev/fd
/dev/tcp|upd
/dev/loop
1、标准输出输入设备
还记得上次说的,linux 重定向吗?可以看下:linux shell 数据重定向(输入重定向与输出重定向)详细分析 。它们就是与几个特殊文件描述符对应,fd0,fd1,fd2 (stdin,stdout,stderr)
如:
?
1234567891011[chengmo@centos5 shell]$
cat
>teststdin<
/dev/stdin
test
#ctrl+D
#cat从/dev/stdin获得数据,然后将标准输出,输入的到teststdin文件
[chengmo@centos5 shell]$
cat
teststdin
test
[chengmo@centos5 shell]$
cat
>teststdin
test
#ctrl+D
#不指定输入,默认输入设备就是/dev/stdinn
/dev/stdin 指的就是键盘设备
?
12345678910111213141516171819202122232425262728[chengmo@centos5 shell]$
cat
test
.sh >
/dev/stdout
|
grep
''echo''
echo
"very good!"
;
echo
"good!"
;
echo
"pass!"
;
echo
"no pass!"
#/dev/stdout指向就是标准输出,因此重定向给它的数据,最终发送到屏幕上(fd1)
[chengmo@centos5 shell]$
cat
test
.sh |
grep
''echo''
echo
"very good!"
;
echo
"good!"
;
echo
"pass!"
;
echo
"no pass!"
;
[chengmo@centos5 shell]$
cat
test
.sh >
/dev/stderr
|
grep
''echo''
#!/bin/sh
scores=40;
if
[[ $scores -gt 90 ]];
then
echo
"very good!"
;
elif
[[ $scores -gt 80 ]];
then
echo
"good!"
;
elif
[[ $scores -gt 60 ]];
then
echo
"pass!"
;
else
echo
"no pass!"
;
fi
;
#/dev/stderr 指是错误输出,默认也是输出到屏幕上面,但是它的内容不能通过管道传递给grep,管道只能传递标准输出
/dev/null 设备
是个黑洞设备,它丢弃一切写入其中数据,空设备通常被用于丢弃不需要的输出流。记得当年用 windows 时候,有个类似的设备:NUL ,跟这个功能一样。任何写入该设备数据都会被丢弃掉。从这个里面读取数据返回是空。将一些不用内容经常发送给这个设备,丢弃不需要的数据。
如:
?
12[chengmo@centos5 shell]$
cat
/dev/null
[chengmo@centos5 shell]$
cat
test
.sh >
/dev/null
?
1#读该设备为空,写入该设备数据都丢弃了
/dev/zero 设备
在类 UNIX 操作系统中,/dev/zero 是一个特殊的文件,当你读它的时候,它会提供无限的空字符 (NULL, ASCII NUL, 0x00)。其中的一个典型用法是用它提供的字符流来覆盖信息,另一个常见用法是产生一个特定大小的空白文件。
如:
?
123456789101112[chengmo@centos5 shell]$
dd
if
=
/dev/zero
of=testzero count=1024 bs=1024
1024+0 records
in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.0107194 seconds, 97.8 MB
/s
#创建一个大小为1M文件,该文件一个块是1024字节,一共是1024块(刚好1M),用/dev/zero文件内容填充它。输出创建到:testzero文件
[chengmo@centos5 shell]$
dd
if
=
/dev/zero
of=
/dev/
磁盘分区
#这个命令一定不要随便用,有点象windows里面的粉碎文件工具。不过它是用\0x00填充整个分区。这样做数据是不可以恢复的了。
[chengmo@centos5 shell]$
cat
/dev/zero
>testinputzero
#这个命令也不能随便使用咯,/dev/zero设备一个特效是,如果你读取的话,是一个死循环会输出无穷的\x00,这样你将创建一个用\x00填充的文件。如果你没有限制该用户磁盘配额。它将耗尽整个磁盘空间。
在 linux 资源配额限制里面,如果没有现在普通用户的磁盘空间利用,或内存使用。一个普通用户就可以通过上面方法一会就塞满整个磁盘。也可以通过 while (true) {fork……} 类程序,启动无限线程,耗尽整个系统内存。
/dev/full 设备
类 Unix 系统中,/dev/full(常满设备)是一个特殊设备文件,总是在向其写入时返回设备无剩余空间(错误码为 ENOSPC),读取时则与 /dev/zero 相似,返回无限的空字符 (NULL, ASCII NUL, 0x00)。这个设备通常被用来测试程序在遇到磁盘无剩余空间错误时的行为。
如:
?
12345[chengmo@centos5 shell]$
echo
''chengmo''
>
/dev/full
-
bash
:
echo
: write error: 设备上没有空间
[chengmo@centos5 shell]$
echo
$?
1
#命令执行返回错误
?
1/dev/random[urandom]
在类 UNIX 操作系统中,/dev/random 是一个特殊的设备文件,可以用作随机数发生器或伪随机数发生器。它允许程序访问来自设备驱动程序或其它来源的背景噪声。常用作随机数发生器。具体参考:linux shell 实现随机数多种方法(date,random,uuid)
/dev/fd
记录用户打开的文件描述符
[chengmo@centos5 shell]$ ls /dev/fd/
0 1 2 3详细参考:
linux shell 数据重定向(输入重定向与输出重定向)详细分析 文件描述符介绍。
/dev/tcp[udp]/host/port
读取该类形式设备,将会创建一个连接 host 主机 port 端口的 tcp [upd] 连接。打开一个 socket 通讯接口。
详细使用可以参考:
linux shell 脚本实现 tcp/upd 协议通讯(重定向应用)
/dev/loop
在类 UNIX 操作系统中,Loop 设备 可以把 loop 文件,作为块设备挂载使用。
如:
[chengmo@centos5 shell]$mount -o loop example.img /home/chengmo/img
#将 img 镜像文件挂载到 /home/chengmo/img 目录下面,有了这个设备,我们不需要通过虚拟光驱就可以读虚拟磁盘格式的文件。
说了很多 linux 特殊设备,其它象 cpu, 内存,磁盘,网络,键盘,终端设备。跟我们 windows 里面常见差不多。有什么问题,欢迎交流!
我们今天的关于linux下/dev/null被误删和linux dev/null的分享已经告一段落,感谢您的关注,如果您想了解更多关于2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null、Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>...、failed to parse: {"row":[8,6,12,7,13,8,14],"meta":[null,null,null,null,null,null,null]}]} 11:46:29.8、linux /dev 常见特殊设备介绍与应用 [loop,null,zero,full,random]的相关信息,请在本站查询。
本文标签: