通过shell脚本连接一个ftp服务器,并下载指定的文件。
本例下载文件通过参数传递给脚本。参数是包含目录和文件名。
脚本名:ftp.sh
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 ftpIP filename"
exit 1
fi
echo "begin..."
dir=$(dirname $2)
file=$(basename $2)
ftpIP=$1
ftp -n -v << EOF
open ${ftpIP}
user allenle 123456
binary
cd ${dir}
get "${file}"
EOF
运行脚本: /bin/bash ftp.sh 192.168.1.245 sources/readme.txt
ftp命令参数:
-n是自动登录
-v 显示过程详细信息