关于cat用法的一点疑问?

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
yang_hui1986527
帖子: 911
注册时间: 2006-03-04 23:10
来自: 江西高安
联系:

关于cat用法的一点疑问?

#1

帖子 yang_hui1986527 » 2009-07-11 18:40

详见下面的代码,能否解释下cat的这种用法什么意思?

代码: 全选

cat <<MAYDAY
 ----------------------------------------------------------
User: $USER          Host:$THIS_HOST         DATE:$MYDATE
----------------------------------------------------------
	1:List files in current directory
	2:Use the vi editor
	3:See who is on the system
	H:Help screen
	Q:Exit Menu	
----------------------------------------------------------
MAYDAY

代码: 全选

#!/bin/bash
#menu
#set the date,user and hostname up
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s`
USER=`whoami`
#loop forever !
while :
do
 #clear the screen
 clear
 #here documents starts here 
 cat <<MAYDAY
 ----------------------------------------------------------
User: $USER          Host:$THIS_HOST         DATE:$MYDATE
----------------------------------------------------------
	1:List files in current directory
	2:Use the vi editor
	3:See who is on the system
	H:Help screen
	Q:Exit Menu	
----------------------------------------------------------
MAYDAY

 #here document finished
echo -e -n "\tEnter Your Choice [1,2,3,H,Q] >"
read CHOICE
 case $CHOICE in
 1) ls
  ;;
 2) vi
 ;;
 3) who
 ;;
 H|h) 
#use a here document for the help screen
cat <<MAYDAY
This is the help screen,nothing here yet to help you!
MAYDAY
 ;;
Q|q) exit 0
 ;;
 *) echo -e "\t\007unknown user response"
 ;;
esac
echo -e -n "\tHit the return key to continue"
read DUMMY
done
微信:sn0wdr1am86
QQ: 3217680847
QQ 群:82695646
网站:https://www.itcoder.tech/
网站:http://www.snowdream.tech/
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 关于cat用法的一点疑问?

#2

帖子 BigSnake.NET » 2009-07-11 18:49

不关 cat 的事
这里的意思是把两个 MAYDAY 之间的内容作为 cat 的标准输入
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 关于cat用法的一点疑问?

#3

帖子 hellojinjie » 2009-07-11 22:21

那 <<< 三个连在一起呢?
Say hello to everyday!
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 关于cat用法的一点疑问?

#4

帖子 BigSnake.NET » 2009-07-11 22:29

代码: 全选

   Here Documents
       This  type  of  redirection  instructs the shell to read input from the
       current source until a line containing  only  word  (with  no  trailing
       blanks)  is seen.  All of the lines read up to that point are then used
       as the standard input for a command.

       The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

       No parameter expansion, command substitution, arithmetic expansion,  or
       pathname expansion is performed on word.  If any characters in word are
       quoted, the delimiter is the result of quote removal on word,  and  the
       lines  in the here-document are not expanded.  If word is unquoted, all
       lines of the here-document are subjected to parameter  expansion,  com-
       mand  substitution,  and arithmetic expansion.  In the latter case, the
       character sequence \<newline> is ignored, and \ must be used  to  quote
       the characters \, $, and `.

       If the redirection operator is <<-, then all leading tab characters are
       stripped from input lines and  the  line  containing  delimiter.   This
       allows  here-documents within shell scripts to be indented in a natural
       fashion.

   Here Strings
       A variant of here documents, the format is:

              <<<word

       The word is expanded and supplied to the command on its standard input.
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
回复