温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

shell如何输出重定向

发布时间:2022-03-23 14:31:49 来源:亿速云 阅读:155 作者:小新 栏目:web开发

这篇文章主要为大家展示了“shell如何输出重定向”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“shell如何输出重定向”这篇文章吧。

输出重定向

如果你愿你,可以将STDERR 和 STDOUT 的输出重定向到一个输出文件,为此,bash 提供了特殊的重定向符号 &>

ls file nofile &> /dev/null


我们如何在脚本里面重定向呢?没有什么特别之处,和普通重定向一样。

#!/bin/bash
#redirecting output to different locations
echo "now redirecting all output to another location" &>/dev/null


问题就来了,如果我们要将所有的输出都重定向到某个文件呢?我们都不希望每次输出的时候都重定向一下吧,正所谓,山人自有妙计。我们可以用exec 来永久重定向,如下所示:

#!/bin/bash
#redirecting output to different locations
exec 2>testerror
echo "This is the start of the script"
echo "now redirecting all output to another location"

exec 1>testout
echo "This output should go to testout file"
echo "but this should go the the testerror file" >& 2


输出结果如下所示:

This is the start of the script
now redirecting all output to another location
lalor@lalor:~/temp$ cat testout
This output should go to testout file
lalor@lalor:~/temp$ cat testerror
but this should go the the testerror file
lalor@lalor:~/temp$


以追加的方式重定向:

exec 3 >> testout


取消重定向:

exec 3> -

shell是什么

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。Shell还是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

以上是“shell如何输出重定向”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI