shell学习笔记--here document用法

 1.先看下这个脚本,注意最后的"EOF"位置

  
  
  
  
  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 

执行看下结果:

  
  
  
  
  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 

执行看下结果:

  
  
  
  
  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位置无论在行首还是其它位置,均可以正常执行

3.再来修改下脚本,加入if条件嵌套,"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  
  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: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"不在行首了

  
  
  
  
  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. ./test_here_document.sh: line 19: syntax error: unexpected end of file 
  3. [root@yunwei14 scripts]#  

5.再修改一下脚本"ftp -n <<- EOF",注意这里,在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 
  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就可以按照优雅的嵌入方式进行对齐。

你可能感兴趣的:(shell,用法)