shell學習筆記--here document用法

 1.先看下這個腳本,注意最後的"EOF"位置sql

  
  
  
  
  1. #!/bin/sh 
  2.   
  3. ftpip=192.168.10.14 
  4. ftpuser=reed 
  5. ftppasswd=reed 
  6.   
  7. ftp -n <<EOF 
  8.         open $ftpip 
  9.         user $ftpuser $ftppasswd 
  10.         binary 
  11.         ls 
  12.         bye 
  13. EOF 

執行看下結果:ide

  
  
  
  
  1. [root@yunwei14 scripts]# ./test_here_document.sh  
  2. Please login with USER and PASS. 
  3. Please login with USER and PASS. 
  4. KERBEROS_V4 rejected as an authentication type 
  5. -rw-rw-r--    1 510      510             9 Jun 05  2012 123.txt 
  6. -rw-r--r--    1 510      510         35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z 
  7. -rw-rw-r--    1 510      510           662 May 08  2012 grep.txt 
  8. drwxr-xr-x    4 0        0            4096 Sep 12 09:26 log 
  9. -rw-r--r--    1 0        0            1811 Dec 16  2011 log.log 
  10. drwxrwxr-x    7 510      510          4096 Dec 07 08:26 scripts 
  11. -rw-r--r--    1 510      510          1415 Dec 03 08:53 t.sh 
  12. -rw-r--r--    1 510      510             8 Dec 04 02:27 test.log 
  13. -rw-r--r--    1 510      510             0 Apr 16  2012 test.txt 
  14. -rw-rw-r--    1 510      510           118 May 08  2012 total.txt 
  15. -rw-rw-r--    1 510      510           115 May 08  2012 total1.txt 
  16. [root@yunwei14 scripts]#  

2.修改一下腳本,注意最後那個"EOF"不在行首了函數

  
  
  
  
  1. #!/bin/sh 
  2.   
  3. ftpip=192.168.10.14 
  4. ftpuser=reed 
  5. ftppasswd=reed 
  6.   
  7. #ifconfig |grep '192.168.10.14' &>/dev/null 
  8. #if [ $? -eq 0 ];then 
  9. ftp -n <<EOF 
  10.         open $ftpip 
  11.         user $ftpuser $ftppasswd 
  12.         binary 
  13.         ls 
  14.         bye 
  15.         EOF 

執行看下結果:spa

  
  
  
  
  1. [root@yunwei14 scripts]# ./test_here_document.sh  
  2. Please login with USER and PASS. 
  3. Please login with USER and PASS. 
  4. KERBEROS_V4 rejected as an authentication type 
  5. -rw-rw-r--    1 510      510             9 Jun 05  2012 123.txt 
  6. -rw-r--r--    1 510      510         35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z 
  7. -rw-rw-r--    1 510      510           662 May 08  2012 grep.txt 
  8. drwxr-xr-x    4 0        0            4096 Sep 12 09:26 log 
  9. -rw-r--r--    1 0        0            1811 Dec 16  2011 log.log 
  10. drwxrwxr-x    7 510      510          4096 Dec 07 08:28 scripts 
  11. -rw-r--r--    1 510      510          1415 Dec 03 08:53 t.sh 
  12. -rw-r--r--    1 510      510             8 Dec 04 02:27 test.log 
  13. -rw-r--r--    1 510      510             0 Apr 16  2012 test.txt 
  14. -rw-rw-r--    1 510      510           118 May 08  2012 total.txt 
  15. -rw-rw-r--    1 510      510           115 May 08  2012 total1.txt 

從上面能夠看出,最後的EOF位置不管在行首仍是其它位置,都可以正常執行ip

3.再來修改下腳本,加入if條件嵌套,"EOF"放在行首string

  
  
  
  
  1. #!/bin/sh 
  2.   
  3. ftpip=192.168.10.14 
  4. ftpuser=reed 
  5. ftppasswd=reed 
  6.   
  7. ifconfig |grep '192.168.10.14' &>/dev/null  
  8. if [ $? -eq 0 ];then  
  9. ftp -n <<EOF 
  10.         open $ftpip 
  11.         user $ftpuser $ftppasswd 
  12.         binary 
  13.         ls 
  14.         bye 
  15. EOF  
  16. else  
  17.         echo "error" 
  18. fi 

執行下看下結果:it

  
  
  
  
  1. [root@yunwei14 scripts]# ./test_here_document.sh  
  2. Please login with USER and PASS. 
  3. Please login with USER and PASS. 
  4. KERBEROS_V4 rejected as an authentication type 
  5. -rw-rw-r--    1 510      510             9 Jun 05  2012 123.txt 
  6. -rw-r--r--    1 510      510         35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z 
  7. -rw-rw-r--    1 510      510           662 May 08  2012 grep.txt 
  8. drwxr-xr-x    4 0        0            4096 Sep 12 09:26 log 
  9. -rw-r--r--    1 0        0            1811 Dec 16  2011 log.log 
  10. drwxrwxr-x    7 510      510          4096 Dec 07 08:30 scripts 
  11. -rw-r--r--    1 510      510          1415 Dec 03 08:53 t.sh 
  12. -rw-r--r--    1 510      510             8 Dec 04 02:27 test.log 
  13. -rw-r--r--    1 510      510             0 Apr 16  2012 test.txt 
  14. -rw-rw-r--    1 510      510           118 May 08  2012 total.txt 
  15. -rw-rw-r--    1 510      510           115 May 08  2012 total1.txt 

4.再來修改下腳本,此次的"EOF"不在行首了pip

  
  
  
  
  1. #!/bin/sh 
  2.   
  3. ftpip=192.168.10.14 
  4. ftpuser=reed 
  5. ftppasswd=reed 
  6.   
  7. ifconfig |grep '192.168.10.14' &>/dev/null 
  8. if [ $? -eq 0 ];then 
  9. ftp -n <<EOF 
  10.         open $ftpip 
  11.         user $ftpuser $ftppasswd 
  12.         binary 
  13.         ls 
  14.         bye 
  15.         EOF 
  16. else 
  17.         echo "error" 
  18. fi 

執行看下,報錯了.......io

  
  
  
  
  1. [root@yunwei14 scripts]# ./test_here_document.sh                                                                                                                        
  2. ./test_here_document.sh: line 19: syntax error: unexpected end of file 
  3. [root@yunwei14 scripts]#  

5.再修改一下腳本"ftp -n <<- EOF",注意這裏,在EOF前面加一個"-"class

  
  
  
  
  1. #!/bin/sh 
  2.   
  3. ftpip=192.168.10.14 
  4. ftpuser=reed 
  5. ftppasswd=reed 
  6.   
  7. ifconfig |grep '192.168.10.14' &>/dev/null 
  8. if [ $? -eq 0 ];then 
  9. ftp -n <<-EOF  
  10.         open $ftpip 
  11.         user $ftpuser $ftppasswd 
  12.         binary 
  13.         ls 
  14.         bye 
  15.         EOF 
  16. else 
  17.         echo "error" 
  18. fi 

執行看下結果,喲,能夠了哦~

  
  
  
  
  1. [root@yunwei14 scripts]# ./test_here_document.sh                                                                                                                       
  2. Please login with USER and PASS. 
  3. Please login with USER and PASS. 
  4. KERBEROS_V4 rejected as an authentication type 
  5. -rw-rw-r--    1 510      510             9 Jun 05  2012 123.txt 
  6. -rw-r--r--    1 510      510         35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z 
  7. -rw-rw-r--    1 510      510           662 May 08  2012 grep.txt 
  8. drwxr-xr-x    4 0        0            4096 Sep 12 09:26 log 
  9. -rw-r--r--    1 0        0            1811 Dec 16  2011 log.log 
  10. drwxrwxr-x    7 510      510          4096 Dec 07 08:34 scripts 
  11. -rw-r--r--    1 510      510          1415 Dec 03 08:53 t.sh 
  12. -rw-r--r--    1 510      510             8 Dec 04 02:27 test.log 
  13. -rw-r--r--    1 510      510             0 Apr 16  2012 test.txt 
  14. -rw-rw-r--    1 510      510           118 May 08  2012 total.txt 
  15. -rw-rw-r--    1 510      510           115 May 08  2012 total1.txt 

結論來了哦~~

1.若是用<<而不是<<-,則後面的EOF必須位於行首,不然,若here_document用在函數內部,則會報語法錯誤;用在函數外面,其後面的全部內容均會被當作here_document的內容。

2.若是重定向操做符是<<-, 則ftp和EOF行中的全部前綴TAB字符都將被忽略(但空格不會被忽略)。這樣源代碼中的here-documents就能夠按照優雅的嵌入方式進行對齊。

相關文章
相關標籤/搜索