3

I need to import a text file like this below:

AMBI
CBNK
CCXI
CERE
CLACW
CNYD
DAEG
DLIA
DLLR
DNDN
DSGX
HAST
HIBB,
HPOL
IRDMZ
MARK
NEPT
NFEC
NINE
NRCIB
OMER
PLCC
PLPM
PSUN
UNTK

There are 25 entries, I need to pass them into a bash script.

I want to run the program once for each symbol. The symbols are Nasdaq stock symbols. I'm trying to pull a quote with the script.

I've been able to do this command manually:

sh stock (symbol) > /home/user/Desktop/stockquote.txt

by inputting the (symbol) manually, I need a a script to do it automatically.

  • Your question isn't clear. Do you want to run the script once for each symbol? Or once for all, with the symbols as separate arguments? Or something else? – Gilles Aug 25 '13 at 19:20
  • good question, I want to run it for each symbol, the symbols are nasdaq stock symbols I'm trying to pull a quote with the script. – user177073 Aug 26 '13 at 11:10
2

Are you trying to read each line as the symbol?

Have the 25 entries be in a file called stocks.txt. Then use the following script to iterate over each line in the stocks file and process the symbol using the stock function declared in the same script:

while read symbol ; do 
  stock $symbol >> /home/user/Desktop/stockquote.txt 
done < stocks.txt
  • 6
    you almost certainly want to either use >> or move the stdout redirection outside of the loop (I'd go for option 2). And ($symbol) will cause a bash error; I think you want to get rid of the parentheses. – rici Aug 25 '13 at 5:19
  • Thanks for the comment. I have reflected your suggestions in my answer. – neowulf33 Aug 26 '13 at 2:43
1

Apparently, the title and the body of the question did not seem quite the same to me. Here are two solutions:

Importing data from an external file into a Bash script:

Read the file and store its contents as entries in an array. (Note: this is feasible given the contents of your text file. If, for e.g., any line in the file had multiple words without quotations, the array so formed would not be as desired.)

declare -a symbols=($(cat input_file.txt | tr '\n' ' '))

Now, iterate over the items of the array symbols as follows.

LOG_FILE=/home/user/Desktop/stockquote.txt    

for symbol in ${symbols[@]}  
do  
    # echo "Processing $symbol ..."  
    sh stock "$symbol" >> "$LOG_FILE"    
done

In fact, you can embed this in the script named stock itself. Just change the sh stock "$symbol" above to something like this:

f_process "$symbol" >> "$LOG_FILE"    

where f_process is possibly a function processing the symbol. If you don't want to write a function, just put the relevant lines of code there.

Passing data from an external file as arguments to a shell script:

A related solution has already been posted by neowulf33. Am just reproducing that with corrections:

LOG_FILE=/home/user/Desktop/stockquote.txt 
while read symbol    
do  
    stock "$symbol" >> "$LOG_FILE"
done < input_file.txt
  • here is what I changed it to, I know its not right as its not working; #!/bin/bash declare -a symbols=($(cat /home/user/Desktop/file5.txt | tr '\n' ' ')) for symbol in ${symbols[@]} do # echo "Processing $symbol ..." sh stock "$symbol" >> "$LOG_FILE" { lynx -dump "google.com/finance?client=ob&q=${1}" | sed 's/.*']'//' | perl -00ne "print if /Watch this stock/i" | sed 's/Watch this stock//' | sed 's/Disclaimer//' | sed '/^$/d' | sed 's/Currency in USD//' | LOG_FILE=/home/user/Desktop/stockquote.txt } /bin/bash /home/user/Desktop/quoteparse h – user177073 Aug 25 '13 at 15:45
  • also when I run the script I'm getting this error: sh stock2 stock2: 2: stock2: Syntax error: "(" unexpected as it doesn't do any of the file manipulation I'm guessing its stopping at the first brace "(" in the script. – user177073 Aug 25 '13 at 15:52
  • The code pasted in the comment earlier doesn't seem to be meaningful. Additionally, the arrays wouldn't work with sh -- use bash instead. – Barun Aug 25 '13 at 17:17
  • Here is what I've come up with so far: > /bin/bash #!/bin/bash # SCRIPT: method1.sh # PURPOSE: Process a file line by line with PIPED while-read loop. FILENAME=$ /home/user/Desktop/file5.txt count=0 cat $FILENAME | while read LINE do bin/bash stock $LINE | > /home/user/Desktop/$Line.txt let count++ echo “$count $LINE” done echo -e “\nTotal $count Lines read” while read -r line;do touch "$line" || echo "Couldn't create \"$Line\"" && exit 27 done < /home/user/Desktop/file5.txt As you can see this is a mashup of suggestions posted here, but it's not working, any ideas? – user177073 Aug 30 '13 at 20:45
  • Unless there was some typing mistake while posting the code in the above comment, this contains a few errors. Corrected lines -- 1) FILENAME=/home/barun/Desktop/file5.txt 2) /bin/bash stock $LINE > /home/barun/Desktop/$Line.txt – Barun Aug 31 '13 at 6:16
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.