If you want to loop over the lines of a file, you can use the read
builtin in a loop like this:
while IFS= read -r line; do
echo "$line"
done <input_file.txt
The content of the line is stored in the variable line
(you can choose any other name). See Understanding IFS for more explanations. Without IFS=
, leading or trailing whitespace is stripped, which may be desirable in your case.
Beware that >stockquote.txt
truncates the output file each time. Either put it around the whole command, or use >>stockquote.txt
which appends to the file. If you use >>
, take care that the file may not be empty when your script starts.
This snippet runs the program stock
for each line, and stores the output in stockquote.txt
with a blank line (echo
with no argument) between each symbol.
while read -r line; do
stock "$line"
echo
done <input_file.txt >stockquote.txt
Make sure that stock
is executable. If it's a shell script, make sure that it begins with #!/bin/sh
. If it isn't in the command search path $PATH
, specify the path explicitly (e.g. ./stock
if it's in the current directory).
You can also use xargs
, but beware that its input syntax is peculiar: it doesn't use newlines as delimiters, but whitespace, and treats \'"
as quoting characters (with the default input syntax — just to make things worse, xargs has alternative input syntaxes depending on its command-line options). For your use case, this is probably suitable as well, and it's shorter, but you lose the opportunity to have whitespace or quote characters in symbols, to skip comment lines, etc.
xargs -n 1 stock <input_file.txt >stockquote.txt