Bash札记(二)

一、I/O File
1、read指定分割
[code]
while IFS=: read user pass uid gid fullname homedir shell
do
//process each line
done < /etc/passwd
[/code]
2、使用here document,作为读入内容:
[code]
#!/bin/bash
cat <<EOF
hello
this is a here
document
EOF
[/code]
我们可以使用here document存放一些ed命令然后执行,比如
原来文档:
[quote]
That is line 1
That is line 2
That is line 3
That is line 4
[/quote]
我们使用一下脚本:
[code]
#!/bin/sh
ed a_text_file <<EOF
3
d
.,\$s/is/was/
w
q
EOF
[/code]
结果变成:
[quote]
That is line 1
That is line 2
That was line 4
[/quote]
3、标准输入、输出、错误输出重定向:
[code]
make 1> result 2> error
[/code]
将make执行的标准输出重定向到result,错误重定向到error文件
如果我们想抛弃掉错误:
[code]
make 1> result 2> /dev/null
[/code]
1>显示的指定1是没有必要的,这是>重定向默认将标准输出进行重定向。
如果我们想把标准输出和标准错误信息都重定向到相同的文件我们可以使用:
[code]
make > results 2>&1
[/code]
这个命令的含义是:>results将标准输入重定向到results,接下来的重定向2>&1
分成两部分 2>将标准错误重定向,&1代表1的描述符被重定向的位置。
4、使用exec改变shell的I/O设置:
exec 2> /temp/err.log 重定向shell的标准错误到err.log文件中
exec 3> /file 打开新的文件描述符3
read name rank serno <&3 读/file文件
保存原来和恢复:
[code]
exec 5> &2 #保存标准错误到fd 5
exec 2> /tmp/err.log #重定向标准错误到err.log
#....做一些操作
exec 2>&5 #利用保存的标准错误恢复
exec 5>&- #关闭fd 5
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值