RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
shell如何输出重定向

这篇文章主要为大家展示了“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如何输出重定向”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章题目:shell如何输出重定向
本文路径:http://sczitong.cn/article/pcepeh.html