Shell 语法之输入输出
Linux 使用文件描述符标识每个文件对象。文件描述符是一个非负整数,可以唯一地标识会话中打开的文件。每个进程中最多可以有9个打开文件的描述符。
Linux 标准文件描述符
文件描述符 缩写 描述
0 STDIN 标准输入
1 STDOUT 标准输出
2 STDERR 标准错误
STDIN 文件描述符引用 shell 的标准输入。
STDOUT 文件描述符引用 shell 的标准输出。
STDERR 文件描述符处理错误消息。
可以将 STDEER 和 STDOUT 输出重定向到同一个输出文件,使用 &> 符号
- ls -al test test2 test3 badtet &> test7
或者
command>&file:将命令的标准错误输出一起重定向到一个文件。
- find /etc -name passwd >alloutlput >&
将标准输出、错误输出都存入到alloutout文件中
在脚本中重定向输出
在脚本中使用文件描述符在多个位置生成输出,只要重定向相应的文件描述符即可。
在文件描述符编号前面添加 & 号,表示在脚本的文件描述符指向的地方而不是普通的 STDOUT 上显示文本。
示例
#!/bin/bash
# testing STDEERR messages
echo "This is an error" >&2
echo "This is normal output"
使用 exec 命令通知 shell 在脚本执行期间重定向特定的文件描述符
示例
#!/bin/bash
# redirecting output to different locations
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 testout file"
echo "but this should go to the testerror file" >&2
[root@tang sh]# ./test12-11
This is the start of the script
now redirecting all output to another location
在脚本中重定向输入
示例
#!/bin/bash
# redirecting file input
exec 0< states
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
[root@tang sh]# ./test12-12
Line #1: Alabama
Line #2: Alaska
Line #3: Arizona
Line #4: Arkansas
Line #5: Colorado
Line #6: Connecticut
Line #7: Florida
Line #8: Georgia
自定义输出文件描述符
shell 中最多有9个打开的文件描述符,可以将其余的文件描述符应用到任何文件,然后在脚本中作用它们。
示例
#!/bin/bash
# using an alternative file descriptor
exec 3>test3out
echo "this should display on the monitor"
echo "and this should be stored in teh file" >&3
echo "then this should be back on the monitor"
[root@tang sh]# ./test12-13
this should display on the monitor
then this should be back on the monitor
重定向文件描述符
可以将 STDOUT 的原位置重定向到备选文件描述符,然后将该文件描述符重定向加 STDOUT。
示例
#!/bin/bash
# storing STDOUT, then coming back to it
exec 3>&1
exec 1>test14out
echo "this should store in the output file"
echo "along with this line."
exec 1>&3
echo "now things should be back to normal"
[root@tang sh]# ./test12-14
now things should be back to normal
自定义输入文件描述符
示例
#!/bin/bash
# redirecting input file descriptors
exec 6<&0
exec 0<states
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 "Goodby" ;;
N|n) echo "Sorry, this is the end." ;;
esac
关闭文件描述符
如果创建新的输入或输出文件描述符, shell 将在脚本退出时自动关闭它们。有时需要在脚本结束前手动关闭文件描述符。
要关闭文件描述符,将它重定向到特殊符号 &- 。
示例
#!/bin/bash
# testing closing file descriptors
exec 3>test17file
echo "This is a test line of data" >&3
exec 3>&-
cat test17file
exec 3>test17file
echo "This'll be bad" >&3
列出开放文件描述符
lsof 命令列出整个 Linux 系统上所有的开放文件描述符。
-p 指定进程 ID
-d 指定要显示的文件描述符编号
-a 连接其他两个选项的结果
示例
#!/bin/bash
# testing lsof with file descriptors
exec 3>test18file1
exec 6>test18file2
exec 7<states
/usr/sbin/lsof -a -p $$ -d0,1,2,3,6,7
禁止命令输出
如果不希望显示任何脚本输出,可以将 STDERR 重定向到称为空文件的特殊文件。
示例
- ls -al > /dev/null
可以使用 /dev/null 文件作为输入文件用于输入重定向,由于 /dev/null 文件不包含任何内容,可以使用它快速将数据从现有文件移除。
- cat /dev/null > testfile
创建临时文件
mktemp 命令可以轻松在 /tmp 文件夹中创建一个唯一的临时文件。
默认情况下,mktemp 在本地目录创建文件,需要指定一个文件名模板。模板包括文件名以及附加到文件名后的6个X。mktemp 命令的输出是它所创建的文件的名称。
示例
#!/bin/bash
# creating and using a temp file
tempfile=`mktemp test19.XXXXXX`
exec 3>$tempfile
echo "This scrit writes to temp file $tempfile"
echo "This is the first line" >&3
echo "This is the second line." >&3
exec 3>&-
echo "Done creating temp file. The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null
-t 选项强迫 mktemp 在系统的临时文件夹中创建文件。
示例
#!/bin/bash
# creating a temp file in /tmp
tempfile=`mktemp -t tmp.XXXXXX`
echo "This is a test file" > $tempfile
echo "This is the second line of the test." >> $tempfile
echo "The temp file is located at: $tempfile"
cat $tempfile
rm -f $tempfile
-d 选项让 mktemp 创建一个临时目录而不是一个文件。
示例
#!/bin/bash
# using a temporary directory
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 $tempfile2" >&8
记录消息
使用 tee 命令将输出同时发送到监视器和日志文件。
tee 命令重定向来自 STDIN 的数据,可以与管道命令配置使用重定向任何命令的输出。
示例
#!/bin/bash
# using the tee command for logging
tempfile=test22file
echo "this is the start of the test" | tee $tempfile
echo "this is the second line of the test" | tee -a $tempfile
echo "this is the end of the test" | tee -a $tempfile
Shell 语法之输入输出的更多相关文章
- makefile中的shell语法
在Makefile中写shell代码有点诡异,和不同的shell语法不太一样,如果不了解,看Makefile会莫名其妙.下面总结了一些. 1:尽在Makefile文件的目标项冒号后的另起一行的代码才是 ...
- shell中的输入输出和编程中的变量(shell 03)
shell中的输入输出标准输入:键盘标准输出:终端显示器>> 追加是换行追加的echo -n 不尾随换行符 -e 启用解释反斜杠的转义功能 -E 禁用解释反斜杠的转义功能(默认) --he ...
- Hadoop学习笔记之HBase Shell语法练习
Hadoop学习笔记之HBase Shell语法练习 作者:hugengyong 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令 ...
- Shell语法规范
ver:1.0 博客:https://www.cnblogs.com/Rohn 本文介绍了Shell编程的一些语法规范,主要参考依据为谷歌的Shell语法风格. 目录 背景 使用哪一种Shell 什么 ...
- shell语法习题练习进阶版
第4章 shell语法深度习题练习 4.1 使用if,case,函数的方法将服务改成system(centos6) 4.1.1 if方法 4.1.1.1 system实现 4.1.1.1.1 编写代码 ...
- 读完学会shell语法,shell脚本80%已经学会
第3章 shell语法讲解 3.1 shell运算讲解 3.1.1 运算符的讲解 3.1.2 shell运算方式的讲解 3.1.2.1 $(())运算 [root@m01 test_init] # a ...
- shell语法(二)
Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...
- shell语法学习
[原文] 菜鸟笔记shell教程学习. 本篇博客只是记录shell的一些关键语法,主要是做一个记录,有些内容也是copy过来的,并不是一个完整的教程,想完整学习shell的同学可以前往 shell脚本 ...
- shell语法
基本语法列表 #linux组成:内核+工具 #linux启动: . getty:提示登录名和密码,输入之后调用login . login:login验证用户名和密码,然后调用shell . shell ...
随机推荐
- 【五子棋AI循序渐进】——整合完成
经过一年多的学习和探索,终于在今天得到了一些回报,在实现PVS多线程和加入了一个新的启发模式之后,搜索速度达到了120K左右,现在整合了VCF/VCT引擎.PVS混合引擎之后,棋力与连珠fiver6基 ...
- CSS之border-radius
1.圆角设置 CSS3圆角只需设置一个属性:border-radius(含义是"边框半径").你为这个属性提供一个值,就能同时设置四个圆角的半径.所有合法的CSS度量值都可以使用: ...
- sql cross join table
A 表数据如下图所示 B表数据如下图所示
- [转]C++11 多线程
转载自:http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html C++11开始支持多线程编程,之前多线程编程都需要系统的支持, ...
- Jmeter工作原理
- Python之路-python(面向对象进阶)
一.面向对象高级语法部分 1.静态方法.类方法.属性方法 2.类的特殊方法 3.反射 二.异常处理 三.Socket开发基础 一.面向对象高级语法部分 静态方法(@staticmethod) 定义:只 ...
- jsp:中文乱码解决
说明:request乱码指的是:浏览器向服务器发送的请求参数中包含中文字符,服务器获取到的请求参数的值是乱码: response乱码指的是:服务器向浏览器发送的数据包含中文字符,浏览器中显示的是乱码: ...
- java 读取excel poi 和cell 方法
http://poi.apache.org/spreadsheet/quick-guide.html http://www.aspose.com/docs/display/cellsjava/Eval ...
- [转]Git调用第三方对比工具beyondCompare
点击阅读原文 对于我这种 git 命令行小白来说, git 自带的对比工具各种水土不服,想念以前的 svn 小乌龟 + beyondCompare 的日子...纠结完 gitHub client 未果 ...
- JFrame中setDefaultCloseOperation的参数含义
实例1:一个空的java窗口 // JFrameDemo1.java import javax.swing.*; //使用Swing类,必须引入Swing包 public class JFra ...