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就能够按照优雅的嵌入方式进行对齐。