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.
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:
|-n||Restrains 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:
user ftp $anonpass
get $filename $basefile
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:
read var1 var2
echo var1 = $var1 and var2 = $var2
Watch what happens when we run it. Each time here, my typed in input is on bold:
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!