惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
爱范儿
爱范儿
罗磊的独立博客
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
U
Unit 42
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
H
Help Net Security
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理

酷 壳 – CoolShell

感染新冠的经历 | 酷 壳 - CoolShell 从一次经历谈 TIME_WAIT 的那些事 | 酷 壳 - CoolShell 网络数字身份认证术 | 酷 壳 - CoolShell 我做系统架构的一些原则 | 酷 壳 - CoolShell 源代码特洛伊木马攻击 | 酷 壳 - CoolShell Go编程模式 : 泛型编程 | 酷 壳 - CoolShell 如何做一个有质量的技术分享 | 酷 壳 - CoolShell Go 编程模式:k8s Visitor 模式 | 酷 壳 - CoolShell Go编程模式:Pipeline | 酷 壳 - CoolShell Go编程模式:委托和反转控制 | 酷 壳 - CoolShell Go 编程模式:Go Generation | 酷 壳 - CoolShell Go编程模式:Map-Reduce | 酷 壳 - CoolShell Go 编程模式:错误处理 | 酷 壳 - CoolShell Go编程模式:切片,接口,时间和性能 | 酷 壳 - CoolShell 百度为什么掉队了 | 酷 壳 - CoolShell 程序员如何把控自己的职业 | 酷 壳 - CoolShell 计时攻击 Timing Attacks | 酷 壳 - CoolShell Rust语言的编程范式 | 酷 壳 - CoolShell HTTP的前世今生 | 酷 壳 - CoolShell 记一次Kubernetes/Docker网络排障 | 酷 壳 - CoolShell 可视化编程 | 酷 壳 - CoolShell 程序的本质复杂性和元语言抽象 | 酷 壳 - CoolShell 伙伴分配器的一个极简实现 | 酷 壳 - CoolShell C++11的Lambda使用一例:华容道求解 | 酷 壳 - CoolShell C++面试中string类的一种正确写法 | 酷 壳 - CoolShell C++模板”>>”编译问题与词法消歧设计 | 酷 壳 - CoolShell 数据即代码:元驱动编程 | 酷 壳 - CoolShell 数据的游戏:冰与火 | 酷 壳 - CoolShell 7个示例科普CPU Cache | 酷 壳 - CoolShell 加班与效率 | 酷 壳 - CoolShell
bash 函数级重定向 | 酷 壳 - CoolShell
陈皓 · 2009-10-14 · via 酷 壳 – CoolShell

bash 函数级重定向相信每一个人对于操作系统的重定向不会陌生了。就是>, >>, <, <<,关于重定向的基本知识我就不说了。这里主要讨论bash的重定向中的一个鲜为人知的东西,那就是bash脚本的函数也可以定义相关的重定向操作。这可不是命令级的重定向,这是函数级的重点向。这并不是一个新的东西,我只是想告诉大家一个已经存在了多年但却可能不被人常用的功能。

关于bash的这个函数级的重定向的语法其实很简单,你只需要在函数结尾时加上一些重定向的定义或指示符就可以了。下面是一个示例:

function mytest()
{
        ...
} < mytest.in > mytest.out 2> mytest.err

现在,只要是test被调用,那么,这个函数就会从mytest.in读入数据,并把输出重定向到mytest.out文件中,然后标准错误则输出到mytest.err文件中。是不是很简单?

因为函数级的重定向仅当在被函数调用的时候才会起作用,而且其也是脚本的一部分,所以,你自然也可以使用变量来借文件名。下面是一个示例:

#!/bin/bash

function mytest()
{
    echo Hello World CoolShell.cn
} >$out

out=mytest1.out
mytest
out=mytest2.out
mytest

这样一来,标准输出的重定向就可以随$out变量的改变而改变了。在上面的例子中,第一个调是重定向到mytest1.out,第二个则是到mytest2.out。

$ bash mytest.sh; more mytest?.out
::::::::::::::
mytest1.out
::::::::::::::
Hello World CoolShell.cn
::::::::::::::
mytest2.out
::::::::::::::
Hello World CoolShell.cn

正如前面所说的一样,这里并没有什么新的东西。上面的这个示例,转成传统的写法是:

#!/bin/bash

function mytest()
{
    echo Hello World CoolShell.cn
}
mytest >mytest1.out
mytest >mytest2.out

到此为此,好像这个feature并没有什么特别的实用之处。有一个可能比较实用的用法可能是把把你所有代码的的标准错误重定向到一个文件中去。如下面所示:

#!/bin/bash

log=err.log
function error()
{
    echo "$*" >&2
}
function mytest1()
{
    error mytest1 hello1 world1 coolshell.cn
}

function mytest2()
{
    error mytest2 hello2 world2 coolshell.cn
}

function main()
{
    mytest1
    mytest2
} 2>$log

main

运行上面的脚本,你可以得到下面的结果:

$ bash mytest.sh ;cat err.log
mytest1 hello1 world1 coolshell.cn
mytest2 hello2 world2 coolshell.cn

当然,你也可以不用定义一个函数,只要是{…} 语句块,就可以使用函数级的重定向,就如下面的示例一样:

#!/bin/bash

log=err.log
function error()
{
    echo "$*" >&2
}
function mytest1()
{
    error mytest1 hello1 world1 coolshell.cn
}

function mytest2()
{
    error mytest2 hello2 world2 coolshell.cn
}

{
mytest1
mytest2
} 2>$log

你也可以重定向 (…) 语句块,但那会导致语句被执行于一个sub-shell中,这可能会导致一些你不期望的行为或问题,因为sub-shell是在另一个进程中。

如果你问,我们是否可以覆盖函数级的重定向。答案是否定的。如果你试图这样做,那么,函数调用点的重定向会首先执行,然后函数定义上的重定向会将其覆盖。下面是一个示例:

#!/bin/bash

function mytest()
{
    echo hello world coolshell.cn
} >out1.txt
mytest >out2.txt

运行结果是,out2.txt会被建立,但里面什么也没有。

下面是一个重定向标准输入的例子:

#!/bin/bash

function mytest()
{
    while read line
    do
        echo $line
    done
} <<EOF
hello
coolshell.cn
EOF
mytest

下面是其运行结果:

$ bash mytest.sh
hello
coolshell.cn

(全文完)

Loading...