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: line"count=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}
运行结果:
linux shell命令行与脚本编程---处理输入输出相关推荐
- linux 脚本编写基本命令,Linux Shell命令行及脚本编程实例详解
<Linux典藏大系:Linux Shell命令行及脚本编程实例详解>共15章,分为两篇.主要内容包括:Linux 及Linux Shell简介.初识Linux Shell.常用Shell ...
- linux shell命令行及脚本编程实例详解_Linux高手必看的10本经典书籍
Linux高手必看的10本经典书籍 Linux 是一个开放.灵活.跨平台的操作系统,上至庞大的数据中心,下至可放于掌心中的嵌入式设备,Linux 的身影无处不在. 如果你想成为一名精通 Linux 程 ...
- linux shell命令行及脚本编程实例详解_超全整理!这些Shell编程必备知识你都掌握了吗?...
正文最近很多粉丝咨询我,被问到了一些Shell编程的问题,看看大家能否答出来: 1.shell脚本千千万,不知道从哪入手 2.没经验缺方法,面试通不过.做事没头绪 3.野路子.没人教自动化,做了几年基 ...
- Linux shell 命令行环境下使用阿里云盘
阿里云盘在内测的时候我就在使用,整体体验相当的好,最起码不会限速,比起下载速度只有十几 KB 的某垃圾云盘要强太多了. 当然除了使用各系统的客户端进行下载之外,我还想要在命令行进行操作,主要原因也是我 ...
- linux shell 宏定义_Linux系统和Shell命令行简介,走上数据分析之路
122Linux系统和Shell命令行简介,走上数据分析之路 本节作者:刘永鑫 中国科学院遗传与发育生物学研究所 版本1.0.2,更新日期:2020年8月31日 本项目永久地址:https://git ...
- Linux下命令行压缩照片或图片的脚本 (ImageMagick使用心得,convert,import,display实例)
from: Linux下命令行压缩照片或图片的脚本 (ImageMagick使用心得,convert,import,display实例) 在认识ImageMagick之前,我使用的图像浏览软件是Kui ...
- 122.Linux系统和Shell命令行简介,走上数据分析之路
122Linux系统和Shell命令行简介,走上数据分析之路 本节作者:刘永鑫 中国科学院遗传与发育生物学研究所 版本1.0.2,更新日期:2020年8月31日 本项目永久地址:https://git ...
- bash shell sleep_如何使用Linux Sleep命令暂停Bash脚本
原标题:如何使用Linux Sleep命令暂停Bash脚本 Sleep是一个命令行实用程序,允许您将调用进程挂起一段指定的时间.也就是说,sleep命令在给定的时间内暂停下一个命令的执行. 当在bas ...
- linux下logcat命令,Android shell命令行中过滤adb logcat输出的几种方法
我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adb lo ...
- 最牛B 的 Linux Shell 命令
最牛B 的 Linux Shell 命令(一) 引言 Shell作为Unix系操作系统当中最有魅力且不可或缺的组件,经过数十载的洗礼不仅没有被淘汰,而且愈加变得成熟稳健,究其原因,大概因为它是个非常稳 ...
最新文章
- 修改 framework 代码的经验和踩过的坑
- pemicro识别不了驱动_usb驱动无法识别如何解决,手把手教你如何解决usb驱动问题...
- mysql字段简索引_Mysql索引优化攻略(全)
- 大型网站架构之JAVA中间件
- 研究相机和IMU坐标系变换
- modbus-crc16——c语言
- Objcet_类的方法
- np.prod() 函数计算数组元素乘积等
- 项目运行报‘vue-cli-service‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件“
- 个人CTF入门训练过程WriteUp
- 2021-2004中国环境统计年鉴面板数据、环境面板数据
- 数据库身份证号用什么类型_数据库设计规范
- 宝塔安装phalcon扩展及nginx配置
- Shell脚本常见问题
- 【区块链】Go 实现简单区块链
- 视频编辑SDK,AE模版SDK,绿幕抠图SDK,AI人像分割SDK,VLOG模版SDK
- 2022数维杯问题 C:如何利用大脑结构特征和认知行为特征诊断阿尔茨海默病-多思路+代码分享
- Armadillo与OpenCV矩阵数据mat、vec与Mat的相互转换
- 工商管理专业知识与实务(初级)【4】
- [BZOJ4627][BeiJing2016]回转寿司 cdq分治
热门文章
- 采集金山词霸每日一句一言Api
- 在Linux下使用金山词霸2003(转)
- oracle 回收站清空,Oracle 清空回收站
- linux清除回收站权限错误,在Ubuntu 14.04 中修复无法清空回收站的问题
- 自己动手写iPhone wap浏览器之界面架构篇
- 黎曼可积和若尔当可测
- php微信问卷调查,We_Questionnaire: !!停止维护!!基于Thinkphp3.2.3 + jqueryMobile1.4.4的微信公众号应用 -- 移动端问卷调查...
- Spring核心技术
- 最完美开启三星note9USB调试模式的方法
- 科技论文写作课程笔记