1.先看下這個腳本,注意最後的"EOF"位置sql
- #!/bin/sh
- ftpip=192.168.10.14
- ftpuser=reed
- ftppasswd=reed
- ftp -n <<EOF
- open $ftpip
- user $ftpuser $ftppasswd
- binary
- ls
- bye
- EOF
執行看下結果:ide
- [root@yunwei14 scripts]# ./test_here_document.sh
- Please login with USER and PASS.
- Please login with USER and PASS.
- KERBEROS_V4 rejected as an authentication type
- -rw-rw-r-- 1 510 510 9 Jun 05 2012 123.txt
- -rw-r--r-- 1 510 510 35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z
- -rw-rw-r-- 1 510 510 662 May 08 2012 grep.txt
- drwxr-xr-x 4 0 0 4096 Sep 12 09:26 log
- -rw-r--r-- 1 0 0 1811 Dec 16 2011 log.log
- drwxrwxr-x 7 510 510 4096 Dec 07 08:26 scripts
- -rw-r--r-- 1 510 510 1415 Dec 03 08:53 t.sh
- -rw-r--r-- 1 510 510 8 Dec 04 02:27 test.log
- -rw-r--r-- 1 510 510 0 Apr 16 2012 test.txt
- -rw-rw-r-- 1 510 510 118 May 08 2012 total.txt
- -rw-rw-r-- 1 510 510 115 May 08 2012 total1.txt
- [root@yunwei14 scripts]#
2.修改一下腳本,注意最後那個"EOF"不在行首了函數
- #!/bin/sh
- ftpip=192.168.10.14
- ftpuser=reed
- ftppasswd=reed
- #ifconfig |grep '192.168.10.14' &>/dev/null
- #if [ $? -eq 0 ];then
- ftp -n <<EOF
- open $ftpip
- user $ftpuser $ftppasswd
- binary
- ls
- bye
- EOF
執行看下結果:spa
- [root@yunwei14 scripts]# ./test_here_document.sh
- Please login with USER and PASS.
- Please login with USER and PASS.
- KERBEROS_V4 rejected as an authentication type
- -rw-rw-r-- 1 510 510 9 Jun 05 2012 123.txt
- -rw-r--r-- 1 510 510 35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z
- -rw-rw-r-- 1 510 510 662 May 08 2012 grep.txt
- drwxr-xr-x 4 0 0 4096 Sep 12 09:26 log
- -rw-r--r-- 1 0 0 1811 Dec 16 2011 log.log
- drwxrwxr-x 7 510 510 4096 Dec 07 08:28 scripts
- -rw-r--r-- 1 510 510 1415 Dec 03 08:53 t.sh
- -rw-r--r-- 1 510 510 8 Dec 04 02:27 test.log
- -rw-r--r-- 1 510 510 0 Apr 16 2012 test.txt
- -rw-rw-r-- 1 510 510 118 May 08 2012 total.txt
- -rw-rw-r-- 1 510 510 115 May 08 2012 total1.txt
從上面能夠看出,最後的EOF位置不管在行首仍是其它位置,都可以正常執行ip
3.再來修改下腳本,加入if條件嵌套,"EOF"放在行首string
- #!/bin/sh
- ftpip=192.168.10.14
- ftpuser=reed
- ftppasswd=reed
- ifconfig |grep '192.168.10.14' &>/dev/null
- if [ $? -eq 0 ];then
- ftp -n <<EOF
- open $ftpip
- user $ftpuser $ftppasswd
- binary
- ls
- bye
- EOF
- else
- echo "error"
- fi
執行下看下結果:it
- [root@yunwei14 scripts]# ./test_here_document.sh
- Please login with USER and PASS.
- Please login with USER and PASS.
- KERBEROS_V4 rejected as an authentication type
- -rw-rw-r-- 1 510 510 9 Jun 05 2012 123.txt
- -rw-r--r-- 1 510 510 35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z
- -rw-rw-r-- 1 510 510 662 May 08 2012 grep.txt
- drwxr-xr-x 4 0 0 4096 Sep 12 09:26 log
- -rw-r--r-- 1 0 0 1811 Dec 16 2011 log.log
- drwxrwxr-x 7 510 510 4096 Dec 07 08:30 scripts
- -rw-r--r-- 1 510 510 1415 Dec 03 08:53 t.sh
- -rw-r--r-- 1 510 510 8 Dec 04 02:27 test.log
- -rw-r--r-- 1 510 510 0 Apr 16 2012 test.txt
- -rw-rw-r-- 1 510 510 118 May 08 2012 total.txt
- -rw-rw-r-- 1 510 510 115 May 08 2012 total1.txt
4.再來修改下腳本,此次的"EOF"不在行首了pip
- #!/bin/sh
- ftpip=192.168.10.14
- ftpuser=reed
- ftppasswd=reed
- ifconfig |grep '192.168.10.14' &>/dev/null
- if [ $? -eq 0 ];then
- ftp -n <<EOF
- open $ftpip
- user $ftpuser $ftppasswd
- binary
- ls
- bye
- EOF
- else
- echo "error"
- fi
執行看下,報錯了.......io
- [root@yunwei14 scripts]# ./test_here_document.sh
- ./test_here_document.sh: line 19: syntax error: unexpected end of file
- [root@yunwei14 scripts]#
5.再修改一下腳本"ftp -n <<- EOF",注意這裏,在EOF前面加一個"-"class
- #!/bin/sh
- ftpip=192.168.10.14
- ftpuser=reed
- ftppasswd=reed
- ifconfig |grep '192.168.10.14' &>/dev/null
- if [ $? -eq 0 ];then
- ftp -n <<-EOF
- open $ftpip
- user $ftpuser $ftppasswd
- binary
- ls
- bye
- EOF
- else
- echo "error"
- fi
執行看下結果,喲,能夠了哦~
- [root@yunwei14 scripts]# ./test_here_document.sh
- Please login with USER and PASS.
- Please login with USER and PASS.
- KERBEROS_V4 rejected as an authentication type
- -rw-rw-r-- 1 510 510 9 Jun 05 2012 123.txt
- -rw-r--r-- 1 510 510 35718 Dec 04 08:11 dbbak.tar.2012-12-01.Z
- -rw-rw-r-- 1 510 510 662 May 08 2012 grep.txt
- drwxr-xr-x 4 0 0 4096 Sep 12 09:26 log
- -rw-r--r-- 1 0 0 1811 Dec 16 2011 log.log
- drwxrwxr-x 7 510 510 4096 Dec 07 08:34 scripts
- -rw-r--r-- 1 510 510 1415 Dec 03 08:53 t.sh
- -rw-r--r-- 1 510 510 8 Dec 04 02:27 test.log
- -rw-r--r-- 1 510 510 0 Apr 16 2012 test.txt
- -rw-rw-r-- 1 510 510 118 May 08 2012 total.txt
- -rw-rw-r-- 1 510 510 115 May 08 2012 total1.txt
結論來了哦~~
1.若是用<<而不是<<-,則後面的EOF必須位於行首,不然,若here_document用在函數內部,則會報語法錯誤;用在函數外面,其後面的全部內容均會被當作here_document的內容。
2.若是重定向操做符是<<-, 則ftp和EOF行中的全部前綴TAB字符都將被忽略(但空格不會被忽略)。這樣源代碼中的here-documents就能夠按照優雅的嵌入方式進行對齊。