lulix删除文件_使用grep恢复被删文件内容

相信每一个人对于操作系统的重定向不会陌生了。就是>, >>,

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

1

2

3

4function mytest()

{

...

} < mytest.in > mytest.out 2> mytest.err

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

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

1

2

3

4

5

6

7

8

9

10

11#!/bin/bash

function mytest()

{

echo Hello World CoolShell.cn

} >$out

out=mytest1.out

mytest

out=mytest2.out

mytest

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

1

2

3

4

5

6

7

8

9$bash mytest.sh;more mytest?.out

::::::::::::::

mytest1.out

::::::::::::::

Hello World CoolShell.cn

::::::::::::::

mytest2.out

::::::::::::::

Hello World CoolShell.cn

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

1

2

3

4

5

6

7

8#!/bin/bash

function mytest()

{

echo Hello World CoolShell.cn

}

mytest >mytest1.out

mytest >mytest2.out

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24#!/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

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

1

2

3$bash mytest.sh ;cat err.log

mytest1 hello1 world1 coolshell.cn

mytest2 hello2 world2 coolshell.cn

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21#!/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是在另一个进程中。

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

1

2

3

4

5

6

7#!/bin/bash

function mytest()

{

echo hello world coolshell.cn

} >out1.txt

mytest >out2.txt

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13#!/bin/bash

function mytest()

{

while read line

do

echo $line

done

} <

hello

coolshell.cn

EOF

mytest

下面是其运行结果: 1

2

3$bash mytest.sh

hello

coolshell.cn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值