3

I have the following list of stocks that is generated and it is placed in file called awk_1

dfs
fsd
dsf
sdf

I then run the following one liner which generates the correct ULR links

while read i ; do 
  echo $(http://uk.finance.yahoo.com/echartss=$i#symbol=$i\;range=my\;compare=\;indicator=volume\;charttype\=area\;crosshair\=on\;ohlcvalues\=0\;logscale\=off\;source\=undefined\;) tee stock_urls; 
done < awk_1 

However is does not put the output in the file called stock_urls?

Also it generate and strange output on the screen, below is a small section of the output that I get to standard output. It puts ./large_cap_stocks.sh: 51: ./large_cap_stocks.sh: at the front and not found at the end; why might that be happening?

I have searching high and low for why this is not working - any help would be really appreciated.

tripleee
  • 175,061
  • 34
  • 275
  • 318
user1380599
  • 103
  • 2
  • 6
  • 1
    Are you trying to use $( ) as quotes? You appear to be trying to run a URL as a command. And if you want the output of echo to be the input for tee you would need a pipe. – tabstop Jan 13 '14 at 20:25
  • @user1380599 Keep the block of code and input file 4 spaces from the start of the line (select all of the code and press `{}`), so it appears as code, otherwise formatting gets broken and the code is virtually unreadable (hard to read as is) – Reinstate Monica Please Jan 13 '14 at 20:39

2 Answers2

2

You probably meant to write like this:

while read i; do
    echo "http://uk.finance.yahoo.com/echarts?s=$i#symbol=$i\;range=my\;compare=\;indicator=volume\;charttype\=area\;crosshair\=on\;ohlcvalues\=0\;logscale\=off\;source\=undefined\;"
done < awk_1 | tee stock_urls

That is:

  • In the echo command, use "..." to quote your text instead of $(...) which is something else
  • Use the pipe operator | to pass the output to tee, and you can pipe the entire loop this way, no need to do for individual echo lines.
janos
  • 120,954
  • 29
  • 226
  • 236
0

As for what's wrong, you have several basic syntax errors here.

  • echo $(http://something) attempts to run http://something as a command, and then pass its output as a string to echo. Of course, in your case, http://something is not a valid command, so that's why you get the not found error message.
  • echo $(http://something) tee file is passing three arguments to echo; two of them are tee and file, so it will not actually run tee at all.
  • If you fixed that, running tee again and again inside the loop would overwrite the output file in each iteration, ending up with only the output from the last loop iteration in the file.
  • Tangentially, the superstitious backslashing in the echo argument is superfluous. The equals sign is not a shell metacharacter at all, and a semicolon inside a double-quoted string only represents the literal semicolon character itself.

A minimal fix would look like

while read i ; do 
    echo "http://uk.finance.yahoo.com/echartss=$i#symbol=$i;range=my;compare=;indicator=volume;charttype=area;crosshair=on;ohlcvalues=0;logscale=off;source=undefined;" |
     tee -a stock_urls
done < awk_1

but a better fix would avoid redirecting from inside the loop;

while read i ; do 
    echo "http://uk.finance.yahoo.com/echartss=$i#symbol=$i;range=my;compare=;indicator=volume;charttype=area;crosshair=on;ohlcvalues=0;logscale=off;source=undefined;"
done < awk_1 | tee stock_urls

and a better solution still would be to replace this gnarly and slow loop with a one-liner (or, well, two-liner);

sed 's|.*|http://uk.finance.yahoo.com/echartss=&#symbol=&;range=my;compare=;indicator=volume;charttype=area;crosshair=on;ohlcvalues=0;logscale=off;source=undefined;|' awk_1 |
tee stock_urls

This uses the sed command s/regex/replacement/ where the regex matches the entire input line, and & in the replacement recalls the text which the regex matched.

Perhaps also visit https://shellcheck.net/ which should be able to point out at least some of these errors.

tripleee
  • 175,061
  • 34
  • 275
  • 318