linux shell命令行与脚本编程---处理输入输出

作者:zz

1. 在脚本中重定向输出

1.1 临时
使用重定向符将输出信息重定向。需在文件描述符数字之前加一个&
什么是文件描述符?
在这里插入图片描述

例子:echo “This is an error message” >&2
在stderror 所指向的位置显示文本。

例子:
$ cat test8
#!/bin/bash
# testing STDERR messages
echo “This is an error” >&2
echo “This is normal output”
$

运行结果1:默认情况下STDERR导向STDOUT
$ ./test8
This is an error
This is normal output
$

运行结果2:重定向了stderr。那么错误信息就会导向test9。
$ ./test8 2> test9
This is normal output
$ cat test9
This is an error
$

怎么用?
可以在日常工作、调试中把错误信息重定向到特定文本中,方便查阅。

1.2 永久重定向
应用场景:需重定向大量数据,不可能写多条echo。可以用exec。
例子:
$ cat test10
#!/bin/bash
# redirecting all output to a file
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”
$ ./test10
$ cat testout
This is a test of redirecting all output
from a script to another file.
without having to redirect every individual line
$
很明显,已将STDOUT文件描述符重定向到testout。

例子2:
$ cat test11
#!/bin/bash
# redirecting output to different locations
exec 2>testerror #将错误重定向到testerror
echo “This is the start of the script”
echo “now redirecting all output to another location”
exec 1>testout #将输出重定向到testout
echo “This output should go to the testout file”
echo “but this should go to the testerror file” >&2
$
$ ./test11
This is the start of the script
now redirecting all output to another location
$ cat testout
This output should go to the testout file
$ cat testerror
but this should go to the testerror file
$

用途:可以将需要的信息重定向到特定的文件。
缺陷:重定向了STDOUT和STDERROR、就很难再换回原来的位置了。
怎么解决?
看第4节

2. 重定向输入

例子:exec 0< testfile

Shell 从testfile中获得输入。
例子:
$ cat test12
#!/bin/bash
#redirecting file input
exec 0< testfile
count=1
while read line
do
echo "Line #$count: l i n e " c o u n t = line" count= line"count=[ $count + 1 ]
done
$ ./test12
Line #1: This is the first line.
Line #2: This is the second line.
Line #3: This is the third line.
$

3. 创建自己的重定向

1、shell最多有9个可打开的文件描述符,3~8的文件描述符按需分配给自己。
例子:
$ cat test13
#!/bin/bash
# using an alternative file descriptor
exec 3>test13out
echo “This should display on the monitor”
echo “and this should be stored in the file” >&3
echo “Then this should be back on the monitor”
$ ./test13
This should display on the monitor
Then this should be back on the monitor
$ cat test13out
and this should be stored in the file
$
例子: 换成 exec 3>>test13out #追加到test13out

2、重定向文件描述符
如何恢复已重定向的文件描述符:分配另外一个文件描述符给标准文件描述符。

例子:
$ cat test14
#!/bin/bash
# storing STDOUT, then coming back to it
exec 3>&1 #先把3重定向到1
exec 1>test14out #1重定向到文件,下面的输出就会到文件里
echo “This should store in the output file”
echo “along with this line.”
exec 1>&3 #把1重定向到3,而上面又令 3到1,说白了就是1到1。
echo “Now things should be back to normal”
$
$ ./test14
Now things should be back to normal
$ cat test14out
This should store in the output file
along with this line.

重定向输入的例子:一样的道理。
$ cat test15
#!/bin/bash
#redirecting input file descriptors
exec 6<&0
exec 0< testfile
count=1
while read line
do
count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now? " answer
case $answer in
Y|y) echo “Goodbye”;;
N|n) echo “Sorry, this is the end.”;;
esac
$ ./test15

4. 创建读写文件描述符

可以创建单个文件描述符来作为输入输出,但我们不推荐这种方式,因为对同一个文件进行读写时,shell会维护一个指针,指向操作文件的位置。那么任何的读写都会从上次指针的位置开始。会导致结果出错。

例子:
$ cat test16
#!/bin/bash
# testing input/output file descriptor
exec 3<> testfile #先用3这个描述符来标识testfile的读和写。
read line <&3 #用testfile中读入一行,现在指针的位置在文件的第二行。
echo “Read: $line” #输出哪一行
echo “This is a test line” >&3 往文件里覆盖写入一行,很明显,会从第二行开始,就会覆盖原来的内容。
$ cat testfile
This is the first line.
This is the second line.
This is the third line.
$ ./test16
Read: This is the first line.
$ cat testfile
This is the first line.
This is a test line
ine.
This is the third line.
$

5. 关闭文件描述符

例子:exec 3>&- #要用到&-
1、文件描述符无法再写入数据了,否则报错
2、再次在脚本中打开同一个文件时,会创建一个新的文件覆盖掉老文件,所有数据消失。

6. 列出打开的文件描述符

Lsof
-p :指定的进程
-d:指定要显示的文件描述符编号
$$:当前进程的PID
-a:对两个选项的结果进行布尔运算AND
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

7. 阻止命令输出

应用场景:不想显示脚本的输出,比如脚本在后台运行时会出现错误消息,但不想显示错误的消息。

方法:将STDERR重定向到 /dev/null。顾名思义,这是个空文件,什么都没有。

例子1:
$ ls -al > /dev/null
$ cat /dev/null
$

例子2:
$ ls -al badfile test16 2> /dev/null #把错误重定向到空文件
-rwxr–r-- 1 rich rich 135 Oct 29 19:57 test16*
$

例子3:用来清除文件内容

$ cat testfile
This is the first line.
This is the second line.
This is the third line.
$ cat /dev/null > testfile
$ cat testfile
$

8. 创建临时文件

/tmp
mktemp命令可以在 /tmp创建唯一的临时文件。

默认情况下,mktemp会在本地目录创建一个临时文件。需要指定文件模板:文件名.XXXXXX 。mktemp 会自动用随机的6个字符替代6个X,确保唯一性。
例子1:$ mktemp testing.XXXXXX
$ ls -al testing*
-rw------- 1 rich rich 0 Oct 17 21:30 testing.UfIi13
$

例子2:
$ mktemp testing.XXXXXX
testing.1DRLuV
$ mktemp testing.XXXXXX
testing.lVBtkW
$ mktemp testing.XXXXXX
testing.PgqNKG

在这里插入图片描述

使用mktemp命令,将文件名保存到变量中,以供后续的引用。
在这里插入图片描述

Mktemp -t :-t 会强制在系统的临时目录中创建该文件,而且会返回临时文件的额全路径,而不是只是文件名。可以引用。
$ mktemp -t test.XXXXXX
/tmp/test.xG3374
$ ls -al /tmp/test*
-rw------- 1 rich rich 0 2014-10-29 18:41 /tmp/test.xG3374
$

-d:创建一个临时的目录。

9. 记录消息

同时显示在显示器和日志文件上:tee
追加:-a

学以致用:从.csv中读取数据文件,利用SQL lnsert 语句将数据插入数据库中

例子:我已经在Ubuntu上做了实验 /home/book/practise

#!/bin/bash

#read file and create INSERT statements for MySQL

outfile=‘members.sql’
IFS=’,’ #read 语句的分隔符,表示读取的数据行以逗号进行分隔
while read lname fname address city state zip
#read 的语法是从 $1 名称的文件中以逗号为分隔符,读取数据,分别赋给lname fname ……这些变量
do

    cat >> $outfile << EOF  #从${1}中读取数据,写入文件,碰到EOF就结束

    INSERT INTO members (lname,fname,address,city,state,zip) VALUES('$lname', '$fname', '$address', '$city', '$state', '$zip'); #循环读入数据

EOF #结束符
done < ${1}
运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值