Linux---shell编程总结之数据的重定向(五)

1、标准文件描述符:bash保存了三个文件描述符,分别为:STDIN标准输入,STDOUT标准输出和STDERR标准错误,用数字表示分别是0,1,2
标准输入就比如<符号,键盘等,标准输出和标准错误用到的就是显示屏
如果只想重定向标准错误比如:ls -al test 2> test,会将错误信息输出到test文件中,对于服务器报错消息很管用。
2、临时重定向:在重定向到文件描述符时,你必须要在文件描述符数字前加一个&
例如:echo "This is an error message" >&2
这里子是将这句标准输出当成标准错误
例子:

#!/bin/bash
#
echo "This is an error" >&2
echo "This is normal output"

运行脚本:

This is an error
This is normal output
输出结果没有异样,因为Linux会将STDERR导向STDOUT,但是你只要在运行脚本的时候重定向了STDERR,那么标准错误就会被重定向

$ bash test > test1
This is normal output

cat test1
This is an error

3、永久重定向:需要用到exec来指定
例子:

#!/bin/bash
#
exec 1> testout

echo "This is a test of redirecting all output"
echo "from a script to another file."
echo "without having to redirect every individual line"

例子2:

#!/bin/bash
#
exec 2>testerror

echo "This is the start of the script"
echo "now redirecting all output to another location"

exec 1>testout

echo "This output should go to the testour file"
echo "but this should go to the testerror file" >&2

4、重定向输入:和上面原理一样
例子:

#!/bin/bash
#
exec 0< testfile
count=1

while read line
do
	echo "Line #$count: $line"
	count=$[ $count + 1 ]
done

5、创建自己的重定向:因为文件描述符是0~9,所有还有其他数字可以用,用得最多应该是3,6
例子:

#!/bin/bash
#
exec 3> testout

echo "This should dispaly"
echo "and this should be" >&3
echo "Then this should be"

6、重定向文件描述符:直接看例子,用得比较多,类似于变量的替换
例子:

#!/bin/bash
#
exec 3>&1
exec 1>testout

echo "This should store in"
echo "along with this line."

exec 1>&3

echo "Now things should be back"
(重点在与exec后面的三个文件描述符的重定向)

例子2:

#!/bin/bash
#
exec 6<&0

exec 0< testfile

count=1
while read line
do
	echo "Line #$count: $line"
	count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now?" answer
case $answer in
Y | y) echo "Goodbay" ;;
N | n) echo "Sorry";;
esac

7、关闭文件描述符:exec 3>&-,关闭之后脚本就再也不能用此文件描述符,用了就会报错
例子:

#!/bin/bash
#
exec 3> test17file

echo "This is a test line of data" >&3

exec 3>&-

echo "The won'k work" >&3

错误为:badtest: line 9: 3: Bad file descriptor

8、创建临时文件和创建临时目录:mktemp test.XXXXXX,创建目录要用到-d选项,mktemp -d test.XXXXXX
例子:

#!/bin/bash
#
tempdir=$(mktemp -d dir.XXXXXX)
cd $tempdir
tempfile1=$(mktemp temp.XXXXXX)
tempfile2=$(mktemp temp.XXXXXX)
exec 7> $tempfile1
exec 8> $tempfile2

echo "Sending data to directory $tempdir"
echo "This is a test line of data for $tempfile1" >&7
echo "This is a test line of data for $tempfile1" >&8

9、实例:

#!/bin/bash
#
outfile='members.sql'
IFS=','
while read lname fname address city state zip
do
	cat >> $outfile << EOF
	INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('$lname','$fname','$address','$city','$state','$zip');
EOF
done < ${1}

输入以下数据文件:
cat members.csv
Blum,Richard,123 Main St.,Chicago,IL,60601
Blum,Barbara,123 Main St.,Chicago,IL,60601
Bresnahan,Christine,456 Oak Ave.,Columbus,OH,43201
Bresnahan,Timothy,456 Oak Ave.,Columbus,OH,43201

cat members.sql
	INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Blum','Richard','123 Main St.','Chicago','IL','60601');
	INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Blum','Barbara','123 Main St.','Chicago','IL','60601');
	INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Bresnahan','Christine','456 Oak Ave.','Columbus','OH','43201');
	INSERT INTO members (lname,fname,address,city,state,zip) VALUES
('Bresnahan','Timothy','456 Oak Ave.','Columbus','OH','43201');
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值