From: Janis Papanagnou on
houghi schrieb:
> Geoff Clare wrote:
>> See http://cfajohnson.com/shell/cus-faq.html#0b
>
> Thanks. I will for now stick with echo. Writing scripts just for myself
> and in Bash is already a big enough chalange. Im am just not as smart as
> everybody else.

If that's your conclusion from reading the above referenced page you're
probably right in your self-assessment. A switch from echo to printf is
quite trivial.

And since you're repeatedly asking questions here, please consider also
that if you're using echo in any of your code examples you post you may
provoke a lot of responses to first rule out any echo based error (e.g.
if variables are used with echo).

You started this thread, for example, with your code which effectively
(non-problem-related lines thoroughly snipped) was

unknown_command | while read LINE
do
TITLE=$LINE
LINK=$LINE
echo $LINK
done
echo ${TITLE}.flv
echo $LINK

Code like that has a lot inherent problems - as you should meanwhile
know - and a lot of questions have to be asked and circumstances be
clarified, if only WRT echo.

At some point people will just stop replying to your questions if you
refuse to learn by claiming that you're not smart enough.

>
> If I would try to write so any shell could understand it, I would not be
> able to write anything at all. :-(

It's neither required nor possible to write code that runs in any shell.
But you can certainly omit the echo related ambiguity problems without
loss by just using printf.

>
> One could even say that I re-read the last line in that FAQ chapter and
> followed up on it. ;-)

I am not sure you understood the article. But, anyway, again; consider
that you're not only writing code for yourself but also come here often
to let the audience fix your problems.

Think about it.

Janis

> But let's just keep it that I won;t be able to do anything if I would
> try to write for any other shell except bash.
>
> houghi
From: pk on
houghi wrote:

> Janis Papanagnou wrote:
>> I am not sure you understood the article. But, anyway, again; consider
>> that you're not only writing code for yourself but also come here often
>> to let the audience fix your problems.
>
> I probably didn't understand it, which is logical as I am not as smart
> as most others. ;-)
>
> Just for your information, I tried using print (well, printf as print
> does not exist on my machine apparently) and I had after an hour still
> no luck to get anything close to what I would have gotten with 'echo'.
> (and no, I will nore bnore people here with it.)
>
> So it is not an unwillingness to learn, nor is it being stubborn. It is
> accepting the limitations of what I can do. Again, if this results in
> people killfiling me or just not responding, I understand and respect
> that.
>
> If people are willing to help me, great. If not, also great. I will try
> for several more hours to try to get the hang of printf and just hope
> that is not time wasted.

At the very simplest, to emulate

echo "$variable"

with printf, you do

printf "%s\n" "$variable"

This is just 1% of what printf can do. The general format of printf is

printf <format string> <arguments>

"Format string" is a normal string, but it contains some special sequences
that are special to printf, called "format specifications". "%s" above is
one of those. When printf sees one of those sequences, it replaces it with
the next argument in its list, and formats it accordingly. It follows that
(usually) if the format string contains n format specifications, you have to
supply n arguments to printf after the format string. For example, the
format specification %d is to print integers, so you do this:

printf "The sum of %d and %d is %d.\n" "$num1" "$num2" "$((num1+num2))"

If $num1 is 4, and $num2 is 5, the above prints

The sum of 4 and 5 is 9.

The format specifications are many, %s and %d are just two of them.
Simplifying, %s is for strings, %d for integers, %f and %g for floating
point numbers, %c for characters, %% for a literal %. There are many others,
and also each specification can have optional modifiers that change the
formatting. For example, %5d is like %d, but uses five characters to print
the integer, even if it's shorter (pads with spaces); likewise, %05d pads
with zeros.

Printf has another advantage over echo: it recognizes many special escape
sequences that echo does not recognize (or recognizes only with -e in
certain implementations), like \n or \t.

Again this just scratched the surface, but hopefully now you should get the
idea and the rest is just reading the man page to learn all about the format
specifications, the optional modifiers and the escape sequences. In case you
shell's man page for printf if scant, I suggest you do "man 3 printf" to see
the C printf man page with the full specs, to which the shell's printf is
mostly compliant (with the obvious differences/limitations).
From: Janis Papanagnou on
houghi schrieb:
> Janis Papanagnou wrote:
>> I am not sure you understood the article. But, anyway, again; consider
>> that you're not only writing code for yourself but also come here often
>> to let the audience fix your problems.
>
> I probably didn't understand it, which is logical as I am not as smart
> as most others. ;-)
>
> Just for your information, I tried using print (well, printf as print
> does not exist on my machine apparently) and I had after an hour still
> no luck to get anything close to what I would have gotten with 'echo'.
> (and no, I will nore bnore people here with it.)

Have you inspected the man page, or did you just try out some arbitrary
random guesses how it might work? (I am asking because there are two or
three guys here who seem to be reluctant looking into the docs, rather
prefer to mobilize the whole newsgroup with often bad specified problem
descriptions. Don't recall whether you've been one of those guys.)

I see that pk already gave some examples. To smooth the transition I'd
like to add one simple printf replacement for

echo "Hello, $user; my friend."

where a primitive, probably more obvious, straightforward substitute is,
e.g.

printf "%s\n" "Hello, $user; my friend."

As pk showed, you may then want to put the constant string parts into
the format string and separate the arguments, as in

printf "Hello, %s; my friend.\n" "$user"

>
> So it is not an unwillingness to learn, nor is it being stubborn. It is
> accepting the limitations of what I can do. Again, if this results in
> people killfiling me or just not responding, I understand and respect
> that.
>
> If people are willing to help me, great. If not, also great. I will try
> for several more hours to try to get the hang of printf and just hope
> that is not time wasted.

Start simple and evolve the solutions as necessary. It's no wasted time.

Janis

>
> houghi
From: thdyoung on
< there are two or
three guys here who seem to be reluctant looking into the docs />

i don't want to interfere but folk shy away from man pages coz they're
so darned bad to read

groups like this one are fab for learners

face it - a miraculous habit has been widely established on the net
via groups and BBs of helping each other: rolling it back w sharp
reproofs about reading the man pages is kind of counter-counter
cultural.

all us lurkers glean vast amounts of learning from these groups - of
course, we do: we are here to learn coz it is far better than the man
pages

ptarmigan
From: Janis Papanagnou on
thdyoung(a)googlemail.com schrieb:
> < there are two or
> three guys here who seem to be reluctant looking into the docs />
>
> i don't want to interfere but folk shy away from man pages coz they're
> so darned bad to read

Quality differs, certainly. And especially with Linux/GNU some man pages
are practically non-existing - or rather; they're containing just a hint
to the info pages. This is bad, I agree, though information is available;
that can't be denied.

But this is not the problem with above mentioned handful of posters here;
those are apparently just lazy, and they impose the burden to spot what's
wrong with *their* code onto the helpful souls. This is not the desireable
"mode of operation" to foster.

>
> groups like this one are fab for learners

I hope so. But many primitive questions are asked again and again, here.
Some folks just don't look into the man pages, don't look into the FAQ,
don't browse the archives, don't Ask Questions Smart; or a mixture of
those points.

>
> face it - a miraculous habit has been widely established on the net
> via groups and BBs of helping each other:

No one is questioning that. (I support people here since years, BTW.)

> rolling it back w sharp
> reproofs about reading the man pages is kind of counter-counter
> cultural.

(You seem to have missed the point.)

>
> all us lurkers glean vast amounts of learning from these groups - of
> course, we do: we are here to learn coz it is far better than the man
> pages

Most man pages give precise answers to many of the simple questions
that are repeatedly asked here.

The OP seems to have problems learning. (He says so.)

>
> ptarmigan

The posters behaviour should not repel the supporting souls. Don't you
think?

Janis