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

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

Bash | 酷 壳 - CoolShell

bash代码注入的安全漏洞 | 酷 壳 - CoolShell 应该知道的Linux技巧 | 酷 壳 - CoolShell 你可能不知道的Shell | 酷 壳 - CoolShell 用脚本实现哄宝宝睡觉(Demo) | 酷 壳 - CoolShell C语言和sh脚本的杂交代码 | 酷 壳 - CoolShell 用脚本实现哄小孩睡觉 | 酷 壳 - CoolShell 8个实用而有趣Bash命令提示行 | 酷 壳 - CoolShell 如何调试bash脚本 | 酷 壳 - CoolShell
bash 函数级重定向 | 酷 壳 - CoolShell
陈皓 · 2009-10-14 · via Bash | 酷 壳 - 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...