how to set timeout?

When I run a script where the 1st parameter is ip address
ftp -n -i -v $1

I hang here if the ip is wrong
how to set a timeout something like

if (20s not complete "ftp -n -i -v $1") then
echo "error"
fi

Thanks a lot.

Why not check if it's a valid IP address with, say, ping before you attempt to connect?

Jerry

Thanks for your advice.

The ip address could be pinged but the computer do not have open ftp / firewall any reason that can't connect through ftp and it hang for a very long while.

Any other solution? thanks.

ftp is intended for interactive use, not scripts.

For scripts, use the ncftp suite of utilities.

Thank you for your suggestion.

I replaced the "ftp -n -i -v ip" code to "ncftp -u user -p pass ip"
I want to list out the files of the folder from remote ftp
Before I use "ls . folders" to save the list to a local text file and then manipulate it.

Any other method to list out the files in folders and pass to a parameter?
thanks!

To list the files, use ncftpls. You can redirect the output to a file or use command substitution to save them in a variable.

Thanks a lot.
I changed to use
list=$(ncftpls -u user -p pass -t 2 -x -l ftp://ip/\)
echo "$list"

It works great.

Further question is how to split the lines into an array?

Assuming bash or ksh93:

IFS='
'
list=( $(ncftpls -u user -p pass -t 2 -x -l ftp://ip/) )

list=$(ncftpls -u user -p pass -t 2 -x -l ftp://ip/\)
echo $list output is
drwxr-xr-x 2 0 0 4096 Dec 19 10:27 FolderName1 drwxr-xr-x 2 0 0 4096 Dec 19 10:27 FolderName2 drwxr-xr-x 2 0 0 4096 Dec 19 10:27 FolderName3

IFS='
'
list=( $(ncftpls -u user -p pass -t 2 -x -l ftp://ip/\) )
echo $IFS
nothing
echo $list
only drwxr-xr-x 2 0 0 4096 Dec 19 10:27 FolderName1

any problem for me?

Not "nothing"; it would print a blank line since $IFS contains a newline.

It's an array; treat it as such:

printd "%s\n" "${list[@]}"

Sorry for so many questions. Thanks first.
I want to add a "/" next to each item in array, is the following method the fastest?

for a in ${list[@]}
do
array=("${array[@]}" $a"/")
done

IFS='
'
array=( printf "%s/\n" "${list[@]}" )

I used

 
IFS='
'
list=( $(ncftpls .....) )
array=( printf "%s/\n" "${list[@]}" )

The array become
"printf"
"%s/\n"
"drwxr-xr-x ...."
"drwxr-xr-x ...."
...

Sorry. What I meant was:

array=( $( printf "%s/\n" "${list[@]}" ) )

No need to "Sorry" la, you really really help me a lot.
Really appriciate your help. :b: