【Linux】第七章 shell脚本呈现数据(二)

nohup ./run/train_spark.py -c pnn200323 >./200323wlog.txt  2>&1 &

nohup 不挂断的运行;& : 指在后台运行;2:标准错误;1:标准输出,运行日志;>:重定向,将输入放入200323wlog.txt。


1.输入和输出

    标准文件描述符

                 0 STDIN 标准输入

                 1 STDOUT 标准输出

                 2 STDERR 标准错误

    重定向错误

                  1.只重定向错误

ls -al badfile 2>test1         ##2>表示只重定向错误

                  2.重定向错误和数据

ls -al test test2 test3 badtest 2>test4 1>test5
### 2>表示将错误重定向到test4文件,1>表示将标准输出重定向到test5文件
ls -al test test3 test4 badtest &> test7
### &>会将所有的输出发送到同一个文件,包括错误和数据

在脚本中重定向输出

1.临时重定向

使用输出重定向符来讲输出信息重定向到STDERR文件描述符。在重定向到文件描述符时,必须在文件描述符数字前加一个&

2. 永久重定向

使用exec命令告诉shell在脚本执行期间重定向某个特定文件描述符。

## 临时重定向,使用&
echo "this is an error" >&2
echo "this is normal output"
 
## 永久重定向,使用exec命令
exec 2>testerror #重定向标准错误
echo "this is the start of the script" ##正常在屏幕显示
echo "now redirecting all output to another location" ##同上
 
exec 1>testout ##重定向标准输出
echo "this is a test should go to testout file" ##在testout文件中
echo "this line should go to testerror file" >&2 ##在testerror文件中

在脚本中重定向输入

exec命令允许将STDIN重定向到Linux系统上的文件中。

exec 0<test14_2.sh ##重定向输入
count=1
while read line
do 
echo "line #$count : $line"
count=$[ $count + 1 ]
done
## 1 创建输出文件描述符
exec 3>testout
echo "this should be stored in the file " >&3
echo "this should be on the screen"
## 2 重定向文件描述符
exec 3>&1  ##描述符3重定向为描述符1,即STDOUT
exec 1>testout  ##STDOUT重定向到文件
echo "this should store in the output file"
echo "along with this line"
exec 1>&3 ##STDOUT重定向到显示器
echo "now things should be back to normal"
## 3 创建输入文件描述符
exec 6<&0
exec 0<test14_2.sh
count=1
while read line
do
	echo "line #$count : $line"
	count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now? [Y/N]: " answer
case $answer in
	Y|y) echo "Goodbye" ;;
	N|n) echo "sorry,this is the end." ;;
esac
 
## 4 文件读写描述符,需要特别小心使用,使用<>
exec 3<>testfile
read line <&3
echo "read : $line"
echo "this is a test line." >&3
## 5 关闭文件描述符,使用&-,一旦关闭,在脚本就不能在使用
exec 3>&-

列出打开文件描述符

## 列出打开的文件描述符 lsof命令
## lsof -a -p $$ -d 0,1,2,3,4,5,6,7,8,9
exec 3>testfile1
exec 5<testfile2
exec 8<testfile
/usr/bin/lsof -a -p $$ -d0,1,2,3,5,8

阻止命令输出

可以将STDERR重定向到一个叫做null文件的特殊文件,该文件位置为/dev/null

ls -al >/dev/null

创建临时文件

mktemp testing.XXXXXX

指定一个文件名模板,模板可以包含任意文本文件名,在文件名末尾加上6个X就可以了。mktemp命令会用6个字符替换这6个X,该文件在系统启动时不会被删除。-t选项会强制mktemp命令在系统的临时目录创建文件,使用这个选项,mktemp命令会返回临时文件的全路径,在/tmp目录中的文件会在系统启动时被删除。-d选项会创建一个临时目录
 

##创建临时文件
# 本地临时文件 mktemp testing.XXXXXX,mktemp会用6个字符替换这6个X,保证文件名在目录中是唯一的
# -t选项会强制mktemp命令在/tmp目录中创建文件,返回文件的全路径,该文件在系统启动时会被删除
# -d选项会创建一个临时目录
tempfile=$(mktemp test.XXXXXX)
exec 3>$tempfile
echo "This script writes to temp file $tempfile"
echo "This is the first line" >&3
echo "This is the second line" >&3
echo "This is the thrid line" >&3
exec 3>&-
echo "Done creating temp file.The contents are:"
cat $tempfile
rm -f $tempfile 2> /dev/null

记录消息

tee命令可以将输出同时发送到显示器和日志文件,命令格式: tee filename,-a选项会将数据追加到文件中

## 记录消息
# tee命令会将输出同时发送到显示器和日志文件,用法tee filename
#  date | tee -a testfile
# -a选项会将数据追加到文件中


参考:

1.https://blog.csdn.net/liouyi250/article/details/83108026

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值