Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

Redirecting input in a unix shell script?

I have several SCO Unix script that read a parameter from user input or otherwise I put the param within my script, such as ftp scripts and so on. How can I read those parameters from a file instead?

Example: I write an ftp script that will connect to a remote location and bring files, right now I write the same script as many times as I have a remote location, but I'd rather substitute the open xxxx from a file. Also if it is an interactive script and the user need to send to more than one location then I need to be able to collect all the location numbers then read then into my script.


Dave's Answer:

Look at the "-n" flag to the FTP program if that's what you're working with. On my system, here's what it says:

-nRestrains ftp from attempting ``auto-login'' upon initial connection. If auto-login is enabled, ftp will check the .netrc (see below) file in the user's home directory for an entry describing an account on the remote machine. If no entry exists, ftp will prompt for the remote machine login name (default is the user identity on the local machine), and, if necessary, prompt for a password and an account with which to login.

Why is this useful? Because you can then feed the program a script that has every single command you want, and you can therefore script everything "just so" without any direct interaction.

In general, if you are reading from standard input, you can redirect it within some sort of loop or subshell. Here's an example taken from my popular book Wicked Cool Shell Scripts:

ftp -n << EOF
open $server
user ftp $anonpass
get $filename $basefile
quit
EOF

You can see where within a shell script you can gain lots of flexibility by using this technique.

The more general question of redirection is one where Unix really shines as an architecture. Consider this simple shell script that I'll call test.sh:

#!/bin/sh
read var1 var2
echo var1 = $var1 and var2 = $var2
exit 0

Watch what happens when we run it. Each time here, my typed in input is on bold:

$ sh test.sh
hi mom
var1 = hi and var2 = mom
$ echo "test two" | sh test.sh
var1 = test and var2 = two

I hope that all helps you see how to solve your problem!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Hi,

I want to connet to remote host only when I find a file in a given directory and than ftp code.

Pl help

Posted by: Pooja at August 18, 2005 9:24 PM

what is the use of this line below its a scipting code
pls explain me briefly sir

ftp -n << EOF

Posted by: ramana at February 26, 2007 7:53 AM

the -n option tells FTP not to ask for user login information immediately.
as the man page says
'Restrains FTP from attempting auto login upon initial connection"

Posted by: Ankit at February 26, 2007 8:28 AM

I am working in different host. I want to connect those two host through my Script. When I trying to do them in the command prompt. It is going through fine. But through the script, I am not able to do it. Please let me know the script

Posted by: Anantha Ramakrishnan at May 22, 2007 8:13 AM

Hi,

How do I execute a shell script inside FTP commands.

Thanks,
Mani

Posted by: Mani at July 26, 2007 12:58 PM

when calling ftp -n <<EOF inside if loop it is giving me syntax error "<<" Unmatched

Posted by: Aditya Khaund at January 19, 2008 4:07 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]