Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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!


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  








Reader Comments To Date: 16

Pooja said, on August 18, 2005 9:24 PM:

Hi,

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

Pl help

ramana said, on February 26, 2007 7:53 AM:

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

ftp -n << EOF

Ankit said, on February 26, 2007 8:28 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"

Anantha Ramakrishnan said, on May 22, 2007 8:13 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

Mani said, on July 26, 2007 12:58 PM:

Hi,

How do I execute a shell script inside FTP commands.

Thanks,
Mani

Aditya Khaund said, on January 19, 2008 4:07 AM:

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

Abhijeet said, on February 10, 2009 8:20 PM:

HI,
I want to save a spool file generated during exceution of shell script in UTF 16 format. Please suggest some way to implement this functionality. By default file is in ANSI format.

Thanks
Abhijeet

Felix Mwango Mutale said, on February 20, 2009 3:13 AM:

We are using SCO UNIX 5.05 and Informix-4gl 7.20; I would like to write a KORN shell script that will FTP file from Informix Server to Window environment and vise vesa. I have the following script but is is not working:
#!/bin/ksh
echo "OK starting now ..."
echo "lcd c:/payruns" >> $TMP_CMDS # where u want the files to get ftped
cd /u/fmutale/phaseI/bin
ftp -n -i 192.168.100.10 << !
quote user fmutale
quote pass xxxxxxxx
lcd c:\payruns
binary
put microfin433.unl
quit
!
echo "Right ending now ..."

lcd does not seem to be working.

How do you write SFTP from one server to another ?

Please help


Felix Mwango Mutale said, on March 18, 2009 2:19 AM:

I still can't have PUT my document from SCO UNIX to windows. We are using EWAN terminal emulator. Can any one please help ?

NM said, on May 28, 2009 12:33 AM:

For what << EOF is used in unix scripting for sql statement.

Also, we have bdump area for oracle database alert log....for unix,linux,solaris,aix etc., what is the location.

kns said, on July 13, 2010 8:41 AM:

i want to echco the text on screen and same time want to log the text in log file.
without any dublication of work.

Dave Taylor said, on July 13, 2010 5:48 PM:

kns, you're looking for the "tee" command. Type in "man tee" and you'll be a happy scripter!

Robert Holland said, on September 9, 2010 3:06 PM:

I am trying to create a function to run in a users .profile in AIX UNIX that will FTP files that the user specifies. From the command line I want the user to only type:
sendtoftp username dateinfilename.

The filenames that will be sent via FTP are the same length and all have a common format (usernamexxxxdatexxxx.txt). The problem that I am having is that I want to make the second argument "$2" a requirement and evaluate the second argument to make sure it is a number or all digits. I have looked into using the POSIX [:digit:] character class but I am not sure how to go about using it.

Filename format example:
ban1194so09012010_1_1.txt
ban1194st09012010_1_2.txt
ban1194su09012010_1_2.txt
ban1283so09062010_1_1.txt
ban1283so09072010_1_1.txt
ban1283so09082010_1_1.txt
ban1283so09092010_1_1.txt

My function that I want to modify:
sendtoftp() {
case $1 in
ban11)
HOST=10.65.48.199
USER=ban11
PASSWD=ban11password

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p prompt
print -p ascii
print -p mput ban11*$2*
print -p bye

wait
exit 0
;;
@ban02 | ban03 | ban04 | ban05 | ban06 | ban07 | ban08 | ban09 | ban10 | ban12 | ban13 | ban14 | ban15 | ban16 | ban17 | ban18 | ban19 | ban20 | ban20 | ban21)

HOST=10.65.48.199
USER=$1
PASSWD=allhavesamepassword

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p prompt
print -p ascii
print -p mput $1*$2*
print -p bye

wait
exit 0
;;
*)
Message="Usage: sendtoftp username date."
Message1="Example: sendtoftp ban12 09012010."
echo " "
echo " "
echo $Message " "$Message1
echo " "
echo " "
;;
esac

Robert Holland said, on September 9, 2010 3:11 PM:

I was too busy trying to make sure the above post made sense that I forgot to ask for help. If you can, please post examples of how I can make the second argument a requirement and evaluate if it is a number. Thank you in advance!

Robert Holland said, on September 30, 2010 12:11 PM:

The fucntion works now! I couldn't figure out how to force the user to input a number for the second argument but I figured out a work-around to force the user to input two arguments. If they don't it will present a message telling them what they need to do. I also had to add a bogus option (xyzpdq) in front of the user names in the "case" section. For some reason the script ignores whatever is in the first position.

sendtoftp() {
if [ $# -ne 2 ]
then
echo
echo "--------------------------------------"
echo "| Example: sendtoftp ban11 09122010 |"
echo "--------------------------------------"
echo
else

case $1 in
ban11)
HOST=10.65.48.199
USER=ban11
PASSWD=xxxxxx

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
#print -p cd directory
#print -p binary
print -p prompt
print -p ascii
print -p mput ban11*$2*
print -p bye

wait
exit 0
;;
@xyzpdq | ban02 | ban03 | ban04 | ban05 | ban06 | ban07 | ban08 | ban09 | ban10 | ban12 | ban13 | ban14 | ban15 | ban16 | ban17 | ban18 | ban19 | ban20 | ban20 | ban21)

HOST=10.65.48.199
USER=$1
PASSWD=xxxxxxx

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
#print -p cd directory
##print -p binary
print -p prompt
print -p ascii
print -p mput $1*$2*
print -p bye

wait
exit 0
;;
*)
Message="Usage: sendtoftp username date."
Message1="Example: sendtoftp ban12 09012010."
echo " "
echo " "
echo $Message " "$Message1
echo " "
echo " "
;;
esac
fi
}

sugandhi said, on December 1, 2010 7:08 AM:

write a unix shell script to display time in am/pm format using "if/then","case","for loop" statements?

hi am sugandhi
i dono how to write this program please help me out.
please give me the answer soon

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.