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


 在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 current dir
                 2:Use the vi editor
                 3:See who is on the system
                 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
 
 
 
 
#!/bin/bash
TARGET_DIR=$PWD
cd /
list=`cat << EOF
usr/local/Trolltech/QtEmbedded-4.6.3-arm/examples/widgets/wiggly/wiggly
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/screenshots/raycasting.png

usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/slides/demo_6.png
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/slides/demo_3.png
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/slides/demo_5.png
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/fluidlauncher
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/config.xml
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/styledemo/styledemo
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/pathstroke/pathstroke.html
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/pathstroke/pathstroke
EOF
`
tar cfvz $TARGET_DIR/target-qte-4.6.3.tgz $list
EOF本意是 End Of File,表明到了文件末尾。

使用格式基本是這樣的:
命令 << EOF
內容段
EOF
將“內容段”整個作為命令的輸入。
你的代碼里就是用cat命令讀入整段字符串並賦值給list變量。
其實,不一定要用EOF,只要是“內容段”中沒有出現的字符串,都可以用來替代EOF,只是一個起始和結束的標志罷了。

有個特殊用法不得不說:
: << COMMENTBLOCK
shell腳本代碼段
COMMENTBLOCK
這個用來注釋整段腳本代碼。 : 是shell中的空語句。

搜索一下 Here document 你會明白更多。

如果就一行,那么直接賦值即可:
list="usr/local/Trolltech/QtEmbedded-4.6.3-arm/examples/widgets/wiggly/wiggly"

EOF相當於讀文件的方式,適用於多行內容的操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM