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.
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:
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
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:
$ sh test.sh
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!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+