Download the specified file method via FTP in Linux

Source: Internet
Author: User


/> Cat > test25.sh
#!/bin/sh
#1. Test the validity of the number of script parameters.
If [$#-ne 2]; Then
echo "Usage: $ ftp://... username" >&2
Exit 1
Fi
#2. Gets the top six characters of the first parameter and, if not "ftp://", is considered an illegal FTP URL format. Here the cut's-c option indicates that the first to the sixth character is intercepted in the form of a character.
Header= ' echo $ | Cut-c1-6 '
If ["$header"!= "ftp://"]; Then
echo "$0:invalid ftp URL." >&2
Exit 1
Fi
#3. Example of a valid FTP URL: Ftp://ftp.myserver.com/download/test.tar
#4. For the above URL example, the Cut command is delimited by/character, so the third field field represents the server (ftp.myserver.com).
#5. When you intercept filename, the Cut command is also delimited by/character, but "-f4-" gets all the fields after the beginning of the fourth field (Download/test.tar).
#6. Gets the filename portion of filename through the basename command.
Server= ' echo $ | cut-d/-f3 '
Filename= ' echo $ | cut-d/-f4-'
basefile= ' basename $filename '
Ftpuser=$2
#7. Here you need to call Stty-echo so that the password input is not displayed, after you enter the password, you need to reopen the option to ensure that the subsequent input can be returned to display.
#8. echo "", is to simulate a swap.
Echo-n "Password for $ftpuser:"
Stty-echo
Read password
Stty Echo
echo ""
#9. Use the here document to bulk execute FTP commands.
Echo ${0}: Downloading $baseile from server $server.
Ftp-n << EOF
Open $server
User $ftpuser $password
Get $filename $basefile
Quit
Eof
#10. Shell built-in variable $? represents the exit value of the last shell process, 0 indicates a successful execution, and the remaining values represent a failure for different reasons.
If [$?-eq 0]; Then
Ls-l $basefile
Fi
Exit 0
Ctrl+d
/>/test25.sh Ftp://ftp.myserver.com/download/test.tar, Stephen
Password for Stephen:
./test25.sh:downloading from server ftp.myserver.com.
-rwxr-xr-x. 1 root 678 Dec 9 11:46 Test.tar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.