shell 输入输出

1.文件描述符

文件描述符是一个非负整数,可以唯一的标示一个回话中打开的文件,每个过程最多打开9个文件描述符。shell会保存了3个文件描述符0,1,2

0  SEDIN 标准输入
1 STDOUT 标准输出
2 STDERR 标准错误

默认情况下STDOUT 和 STDERR 输出到同一个位置-控制台。
我们可以手动改变如:

cat < testfile
ls -al > testfile
who >> testfile #追加

demo1 重定向错误

master@master:~/shell$ ls -al ddd 2> test
master@master:~/shell$ ls
data  data1  script1  test  testfile
master@master:~/shell$ cat test
ls: 无法访问ddd: 没有那个文件或目录

demo2 重定向错误和标准输出

ls -al test1 badfile 1> out1 2>out2
ls -al test1 badfile &> out #重定向到同一个位置

2.在脚本中重定向输出

(1)临时重定向
在重定向到文件描述符时,需要在文件描述符前面加&

echo "this is an erro Message"  >&2
#>&没有空格

demo1

master@master:~/shell/output$ cat test1
#!/bin/bash

echo "this is an error message" >&2
echo "this is normal output"
master@master:~/shell/output$ ./test1 2> out
this is normal output
master@master:~/shell/output$ cat out
this is an error message

(2)永久重定向
用exec

master@master:~/shell/output$ cat test2
#!/bin/bash
#redirecting all output file to a file

exec 1>testout

echo "This is a test of reding all output"
echo "whiout having "

echo 2>testerr

echo "this is the normal message"
echo "this is the error message" >&2
master@master:~/shell/output$ ls
out  test1  test2  testerr  testout
master@master:~/shell/output$ cat testerr
master@master:~/shell/output$ cat testout
This is a test of reding all output
whiout having 

this is the normal message

3.在脚本中重定向输入 exec 0< testfile

master@master:~/shell/output$ cat testfile
this is an error message
master@master:~/shell/output$ cat test3
#!/bin/bash
#redirecting file input

exec 0< testfile
count=1
while read line
do 
    echo "line #$count: $line"
    count=$[ $count + 1 ]
done

master@master:~/shell/output$ ./test3
line #1: this is an error message

4.创建自己的重定向

exec 3> test3out
exec 3>> test3out #追加

master@master:~/shell/output$ ./test4
this is on monitor
master@master:~/shell/output$ cat test3out 
add this to stored fiile
master@master:~/shell/output$ cat test4
#!/bin/bash
#using an alternative3 file descriptor

exec 3> test3out
echo "this is on monitor"
echo "add this to stored fiile" >&3

(2)重定向文件描述符

master@master:~/shell/output$ ./test5
now this is on the monitor
master@master:~/shell/output$ cat test4file 
this is store in fiole
master@master:~/shell/output$ cat test5
#!/bin/bash

exec 3>&1
exec 1>test4file 
echo "this is store in fiole"

exec 1>&3

echo "now this is on the monitor"

(3)创建输入文件描述符
exec 6<&0
exec 0<testfile

master@master:~/shell/output$ ./test6
line #1 :this is an error message
are you done now?y
GoogBye
master@master:~/shell/output$ cat test6
#!/bin/bash
# redircting input file descriptor

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 "GoogBye";;
N|n) echo "sorry is the end" ;;
esac

(4)创建读写文件描述符exec 3<> testfile

master@master:~/shell/output$ ./test7
read:this is an error message
master@master:~/shell/output$ cat testfile
this is an error message
this is a test lien
master@master:~/shell/output$ cat test7
#!/bin/bash
# create a read/write descriptor

exec 3<> testfile
read line <&3
echo "read:$line"
echo "this is a test lien" >&3

(4)关闭文件描述符
exec 3>&-

5.列出打开的文件描述符lsof
master@master:~/shell/output$ lsof -a -p $$ -d0,1,2
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    2388 master    0u   CHR 136,13      0t0   16 /dev/pts/13
bash    2388 master    1u   CHR 136,13      0t0   16 /dev/pts/13
bash    2388 master    2u   CHR 136,13      0t0   16 /dev/pts/13

master@master:~/shell/output$ ./test8
./test8: 行 6: textfile: 没有那个文件或目录
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
test8   2993 master    0u   CHR 136,13      0t0     16 /dev/pts/13
test8   2993 master    1u   CHR 136,13      0t0     16 /dev/pts/13
test8   2993 master    2u   CHR 136,13      0t0     16 /dev/pts/13
test8   2993 master    3w   REG    8,1        0 285618 /home/master/shell/output/test8file1
test8   2993 master    6w   REG    8,1        0 285622 /home/master/shell/output/test8file

#!/bin/bash

exec 3> test8file1
exec 6> test8file

exec 7< textfile

lsof -a -p $$ -d 0,1,2,3,6,7

6.阻止命令的输出

ls -al > /dev/null
清空文件
cat /dev/null > filename

7.创建临时文件

系统会回在启动时,自动删除/temp下面的 文件

#创建临时文件
mktemp test.XXXXX
#-t 会在tmp 目录下创建文件
mktemp -t test.XXXX
#-d 创建临时目录
mktemp -d testDir.XXXXXX

8.T 型管道

tee filename

master@master:~/shell/output$ date | tee filename
2016年 11月 26日 星期六 14:56:21 CST
master@master:~/shell/output$ cat filename
2016年 11月 26日 星期六 14:56:21 CST

``
-a 追加

master@master:~/shell/output$ date | tee -a filename
2016年 11月 26日 星期六 14:56:46 CST
master@master:~/shell/output$ cat filename
2016年 11月 26日 星期六 14:56:21 CST
2016年 11月 26日 星期六 14:56:46 CST

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容