From: pk on
Ed Morton wrote:

> You could alternatively do:
>
> youtube-dl -g -e -b "$URL" |
> awk '{out = (NR==1 ? $0 ".flv" : out ORS $0)} END{print out ORS $0}'
>
> if you really want the output as from your posted script, which I think
> might actually contain some debugging lines. If you want something
> different, tell us what you're really trying to do.

Well, I think he wanted to get those values into shell variables, and the
echo commands he was using were to check what was contained in the
variables, rather than to just print those values.
From: Ed Morton on
On 4/9/2010 9:09 AM, pk wrote:
> Ed Morton wrote:
>
>> You could alternatively do:
>>
>> youtube-dl -g -e -b "$URL" |
>> awk '{out = (NR==1 ? $0 ".flv" : out ORS $0)} END{print out ORS $0}'
>>
>> if you really want the output as from your posted script, which I think
>> might actually contain some debugging lines. If you want something
>> different, tell us what you're really trying to do.
>
> Well, I think he wanted to get those values into shell variables, and the
> echo commands he was using were to check what was contained in the
> variables, rather than to just print those values.

Beats me. Whatever it is the OP is trying to do, I expect it could be done
simpler, briefer, and clearer without that shell loop.

Ed.
From: Ed Morton on
On 4/9/2010 11:38 AM, houghi wrote:
> Ed Morton wrote:
>> Beats me. Whatever it is the OP is trying to do, I expect it could be done
>> simpler, briefer, and clearer without that shell loop.
>
> Clearified that in another post. I will clearify again more precise.
> Here the command and the standard output. youtube-dl can be found here:
> http://bitbucket.org/rg3/youtube-dl/wiki/Home
>
> houghi(a)penne : youtube-dl -g -e http://www.youtube.com/watch?v=sbQHvUObMbA
> Lady Gaga ft. Beyonc� Vs. Metallica - Telephone Vs. Enter Sandman (Djs From Mars Bootleg Remix)
> http://www.youtube.com/get_video?video_id=sbQHvUObMbA&t=vjVQa1PpcFPg6lz7u5V2uIq8jlbZNDxLjQwESkxxV00=&eurl=&el=detailpage&ps=default&gl=US&hl=en
>
> The first is the title. The second is the URL for download. Here
> specifically I want to have both the title and url in values. At this
> moment there are only two values, but in the future more could be added,
> like the image and the description.
>
> I was first thinking of using a simple awk '{printf $NF}' and a awk
> -F'http://' '{print $1}' but then realized that the title could hold the
> letters http:// for whatever reason.
>
> On a more generic level, what I want to be able to do and learn is to
> have several lines of outcome from a command and give a parameter a
> value.
>
> e.g. `lynx -source lynx -source http://houghi.org/x/file.txt` gives:
> The first line of whatever
> The second line
>
> So what I want to achieve is the following result:
> FIRST="The first line of whatever"
> SECOND="The second line"
>
> I do not want to run the command `lynx -source lynx -source
> http://houghi.org/x/file.txt` more then once. The easy way out is to
> write it to a temporary file. Makes everything a lot easier. But it is
> more fun to be able to do it without writing anything to anywhere.
>
> Hope this is more clear of what want to achieve in general.
>
> houghi

I think so. It seems like it'd be simpler for you to just populate an array of
output lines and reference that. Look:

$ cat tst1.sh
foo() {
echo "a b"
echo "c d e"
echo "f"
}

oIFS="$IFS"
IFS='
'
arr=( $(foo) )
IFS="$oIFS"

for ((i=0;i<${#arr[@]};i++)); do

echo "arr[$i] = ${arr[$i]}"

done

$ ./tst1.sh
arr[0] = a b
arr[1] = c d e
arr[2] = f

Just replace $(foo) with $(lynx...) or whatever command you want to get the
output from line by line into array "arr" so you'd do something like:

oIFS="$IFS"; IFS='
'
arr=( $(lynx -source lynx -source http://houghi.org/x/file.txt) )
IFS="$oIFS"

TITLE=arr[0]
LINK=arr[1]

If that doesn't work for you, there are other alternatives that also don't
require you to write a loop. In particular, depending on what you're doing with
TITLE and LINK maybe having the rest of your script in shell rather than awk or
similar is the wrong approach all together.

Regards,

Ed.
From: Ed Morton on
On 4/10/2010 4:38 AM, houghi wrote:
> Ed Morton wrote:
>> I think so. It seems like it'd be simpler for you to just populate an array of
>> output lines and reference that. Look:
>
> It might be simpler for you, but although I get the idea of what you
> want to do, I have no real understanding what it means exactly. I am
> just not that smart. :-(
>
> Thanks for the feedback though. I will study it more and will try to
> understand it. (Hey, I thought people came here to solve their homework,
> not to get homework. ;-) )

Luckily it's only half a dozen lines so it shouldn't keep you up all night!

> However one line:
>
>> for ((i=0;i<${#arr[@]};i++)); do
>
> And then the comment:
>
>> If that doesn't work for you, there are other alternatives that also don't
>> require you to write a loop.
>
> Isn't the line above (part of) a loop?

That loop is part of the example and is just to show you how the array is
populated. You wouldn't write that in your code. There's no loop in the part
about populating the array which is what I'm suggesting you use.

Ed.
From: Geoff Clare on
houghi wrote:

>> You should also get into the habit of using printf instead of echo
>> (except maybe for simple fixed strings), and quoting variables.
>> E.g.
>> printf '%s\n' "$LINK"
>>
>
> What is the reason for that? What I see (and what is probably wrong,
> otherwise you would not sugest something else) is that the result is the
> same. I never had an issue with the usage of echo.

See http://cfajohnson.com/shell/cus-faq.html#0b

--
Geoff Clare <netnews(a)gclare.org.uk>