/> 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