shell中的cat和文件分界符(<<EOF) (转)

原文地址: http://blog.csdn.net/mosesmo1989/article/details/51123257

在shell中,文件分界符(通常写成EOF,你也可以写成FOE或者其他任何字符串)紧跟在<<符号后,意思是分界符后的内容将被当做标准输入传给<<前面的命令,直到再次在独立的一行遇到这个文件分界符(EOF或者其他任何字符,注意是独立一行,EOF前面不能有空格)。通常这个命令是cat,用来实现一些多行的屏幕输入或者创建一些临时文件。
1、最简单的用法
root@ribbonchen-laptop:~#cat<<EOF
> ha
> haha
> hahaha
> EOF
输出:
ha
haha
hahaha
2、把输出追加到文件
root@ribbonchen-laptop:~#cat<<EOF>out.txt
> ha
> haha
> hahaha
> EOF
root@ribbonchen-laptop:~#cat out.txt
ha
haha
hahaha
3、换一种写法
root@ribbonchen-laptop:~#cat>out.txt<<EOF
> ha
> haha
> hahaha
> EOF
root@ribbonchen-laptop:~#cat out.txt
ha
haha
hahaha
4、cat>filename,创建文件,并把标准输入输出到filename文件中,以ctrl+d作为输入结束
root@ribbonchen-laptop:~#cat>filename
ha  
haha 
hahaha
root@ribbonchen-laptop:~#cat filename
ha
haha
hahaha
下面的脚本实现了一个简单的菜单功能:
#!/bin/bash
MYDATE=`date+%d/%m/%Y`
THIS_HOST=`hostname`
USER=`whoami`
while :
do
 clear
 cat<<EOF
 _______________________________________________________________
 User:$USER        Host:$THIS_HOST         DATE:$MYDATE
 _______________________________________________________________
                1:List files in currentdir
                2:Use the vi editor
                3:See who is on thesystem
                H:Help sreen
                Q:Exit Menu
 _______________________________________________________________
EOF
 echo -e -n "\tYour Choice [1,2,3,H,Q]>"
 read CHOICE
   case $CHOICE in
   1) ls
     ;;
   2) vi
     ;;
   3) who
     ;;
   H|h)
     cat<<EOF
     This is the help screen,nothing here yet to help you!
EOF
     ;;
   Q|q) exit 0
     ;;
   *) echo -e "\t\007unknown user response"
     ;;
   esac
 echo -e -n "\tHit the return key to continue"
 read DUMMY
done

转载于:https://www.cnblogs.com/nyist-xsk/p/7612618.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值