`

linux shell (五)shell编程

阅读更多

一、输入输出

1、重定向

0   STDIN         <   <<

1   STDOUT     >   >>

2   STDERR

2、重定向数据和错误

command 2> errinfo

command 2> errinfo 1> data

经错误和数据信息重定向到一个文件: command &> all

 

二、脚本中重定向

1、临时重定向

echo "error test" >&2执行脚本时这个信息将会显示到 2> file中

2、永久重定向

exec 1>myfile

exec 2>error

#!/bin/bash
#descrip: test 
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/xiaolong.hou/bin:/usr/local/jdk1.7/bin:/usr/local/maven/bin:/usr/local/hadoop-1.2.1/bin


exec 1>data
exec 2>err

echo "hello world"
echo "hello shell"
echo "this is error test" >&2

exit 0

 cat data:

hello world
hello shell

 

cat err:

this is error test

 

脚本中的标准输入: exec 0< testfile

 

3、创建自己的重定向

除了0 1 2的其他描述符

关闭文件描述符:exec label>&-

 

阻止输出: command > /dev/null

 

4、临时文件

mktemp text.XXXXXXX

 

5、记录消息

tee -a(append)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics