首页 > 系统相关 >Linux中&&、&、|、||等特殊符号

Linux中&&、&、|、||等特殊符号

时间:2024-01-16 09:33:58浏览次数:26  
标签:重定向 特殊符号 command && Linux test txt root localhost

&& 和 &

& 表示任务后台执行,与nohup命令功能差不多。

# 运行jar包,并且置于后台执行,执行的日志重定向到当前默认的log.txt文件中
[root@localhost local]$ java -jar test.jar > log.txt &

&& 表示前一条命令执行成功时,才执行后一条命令。

# 执行ls -l成功后,执行cd ..
[root@localhost tmp]$ ls -l && cd .. 
总用量 4 
-rw-r–r–. 1 root root 2252 1月   4 22:25 log.txt 
-rw——-. 1 root root    0 1月   3 23:23 yum.log 
[root@localhost /]$

| 和 ||

| 表示管道,上一条命令的输出,作为下一条命令参数(输入)。

# 查询全部进程后输出结果在进行过滤跟 进行中包含aux的进程。
[root@localhost ~]$ ps -aux | grep aux 
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ 
root        53  0.0  0.0      0     0 ?        S    16:33   0:00 [ata_aux] 
root      2379  4.0  0.1 110224  1172 pts/2    R+   22:55   0:00 ps -aux 
root      2380  0.0  0.0 103316   868 pts/2    D+   22:55   0:00 grep aux 

|| 表示上一条命令执行失败后,才执行下一条命令。

[root@localhost tmp]$ als -l || cd ..
-bash: als: command not found
[root@localhost /]$ 

> 和 >>

表示stdout标准输出信息重定向输出,覆盖写。

[root@localhost ~]$ cat test.txt
Hello
[root@localhost ~]$ echo 'World' > test.txt
[root@localhost ~]$ cat test.txt
World

表示内容追加写。

[root@localhost ~]$ cat test.txt
Hello
[root@localhost ~]$ echo 'World' >> test.txt
[root@localhost ~]$ cat test.txt
HelloWorld

&> 、2>&1 和 2>1

&> 表示stdout标准输出和stderr错误输出信息,重定向输出,覆盖写。

[root@localhost ~]$ cat test.txt
World
[root@localhost ~]$ lll
-bash: lll: command not found 
[root@localhost ~]$ lll > test.txt
[root@localhost ~]$ cat test.txt
# 由于这个是错误信息,所以不能使用标准输出>将信息重定向到test文件中,但覆盖写为空
# 所以错误信息直接在控制台打印出来了
[root@localhost ~]$ lll &> test.txt
[root@localhost ~]$ cat test.txt
-bash: lll: command not found
# 使用&>重定向错误信息没有输出到控制台了,表示错误信息正确重定向到了test文件,也同样是覆盖写

2>&1 表示把标准错误的输出重定向到标准输出1,&指示不要把1当做普通文件,而是fd=1即标准输出处理。
2>1 表示把标准错误的输出重定向到1,但这个1不是标准输出,而是一个名为1的文件。
linux重定向的设备代码

空设备文件/dev/null

  • 标准输入(stdin) 代码为0,实际映射关系:/dev/stdin -> /proc/self/fd/0
  • 标准输出(stdout)代码为1,实际映射关系:/dev/stdout -> /proc/self/fd/1
  • 标准错误输出(stderr)代码为2,实际映射关系:/dev/stderr ->/pro/self/fd/2

command>a 2>1 、command>a 2>a 与 command>a 2>&1的区别

1. command>a 2>&1 等价于 command 1>a 2>&1
   意思为执行command产生的标准输入重定向到文件a中,标准错误也重定向到文件a中。

2. command>a 2>a 不等价于 command 1>a 2>&1,其区别如下:
   i. command>a 2>a打开文件两次,而command 1>a 2>&1只打开文件一次;
  ii. command>a 2>a由于打开文件两次,导致stdout被stderr覆盖;
 iii. 从IO效率上来讲,command 1>a 2>&1比command 1>a 2>a的效率更高。

3. command>a 2>1 等价于 command 1>a 2>1
   意思为执行command产生的标准输入重定向到文件a中,标准错误重定向到文件1中。

来源:https://blog.csdn.net/bocai8058/article/details/82932397

标签:重定向,特殊符号,command,&&,Linux,test,txt,root,localhost
From: https://www.cnblogs.com/liuyanhang/p/17966894

相关文章

  • 在Linux下, 不使用包管理器安装Golang sdk
    尝试从targz安装go下面这个是go官网的,注意使用代理下载;wgethttps://go.dev/dl/go1.21.6.linux-amd64.tar.gztar-zxvfgo1.13.1.linux-amd64.tar.gzmvgo//usr/local/将go添加到环境变量:确实fishshell是这样设置环境变量的吗?vim/etc/profile加入以下内容:e......
  • Linux常用命令汇总
    tags:-linux持续更新author:zhangleidate:2024-01-1522:53dateupdated:'2024-01-15'前言本篇文章是为了巩固和记录一些linux下的常用指令,相当于是自己的一本工具书。本文参考以下网站Linux命令大全(手册)–真正好用的Linux命令在线查询网站(linuxcool......
  • linux下NTFS磁盘报错输入输出错误
    title:linux下NTFS磁盘报错输入输出错误banner_img:https://cdn.studyinglover.com/pic/2023/12/334c0c129076533308cbc7e03f8c55be.pngdate:2024-1-1522:50:00tags:-踩坑linux下NTFS磁盘报错输入输出错误简单来说就是我在linux下挂载了一个NTFS格式的移动硬盘,然后在......
  • linux安装花生壳,内网穿透
    1,官网下载linux版本的花生壳,命令:wget"https://dl.oray.com/hsk/linux/phddns_5.3.0_amd64.deb"-Ophddns_5.3.0_amd64.deb或者去官网下载deb包。2,解压,安装:dpkg-iphddns_5.3.0_amd64.deb3,运行:sudophddnsstart4,查看phddns的状态:sudophddnsstatus打开花生壳web链......
  • Linux操作系统不同文件类型区别?
    蓝色代表目录,绿色代表可执行文件,红色代表压缩文件、浅蓝色表示连接文件、白色表示其他文件相关目录及作用:      Bin:存放普通用户可执行的指令      Boot:开机引导目录      Dev:设备目录      Etc:各种配置文件目录      Lib:库文件存放......
  • Linux find命令删除N天或N小时前的文件或目录
    前言全局说明Linuxfind命令删除N天或N小时前的文件或目录一、模拟创建N天前的文件(夹)1.1生成不带0开头的文件名foriin{1..15};dot=$i;touch-d"2024-01-0$i00:00:00"$i.txt;done1.2生成0开头的文件名foriin{01..15};dot=$i;touch-d"2024-01-0$......
  • linux安装docker 社区版(免费版)
    Docker分为CE和EE两大版本。CE即社区版(免费,支持周期7个月)EE即企业版,强调安全,付费使用,支持周期24个月。DockerCE分为stabletest和nightly三个更新频道。官方网站上有各种环境下的安装指南,这里主要介绍DockerCE在CentOS上的安装。1.1.卸载(可选)如果之前安装......
  • linux 部署 jdk1.8
    将文件(jdk-8u391-linux-x64.tar.gz)上传到服务器的文件中。我是放到了/usr/local/jdk文件夹下面。然后输入指令压文件tar-zvxfjdk-8u391-linux-x64.tar.gz找到 /etc/profile文件,在最后一行添加exportJAVA_HOME=/usr/local/jdk/jdk1.8.0_391exportCLASSPATH=$:......
  • SecureCRT & SecureFX 9.5 for macOS, Linux, Windows
    SecureCRT&SecureFX9.5formacOS,Linux,Windows-跨平台的多协议终端仿真和文件传输请访问原文链接:SecureCRT&SecureFX9.5formacOS,Linux,Windows,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgSecureCRT客户端运行于Windows、Mac和Linux,将坚如磐......
  • Acunetix v24.1 (Linux, Windows) - 漏洞扫描 (Web 应用程序安全测试)
    Acunetixv24.1(Linux,Windows)-漏洞扫描(Web应用程序安全测试)Acunetix|WebApplicationSecurityScanner请访问原文链接:https://sysin.org/blog/acunetix/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org重要提示AcunetixPremium现在使用日历化版本命......