| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
> cat <<-'EOF' | sed -e "s|[$]path|$usb|g"
> echo and here it is:
> ftpdir=$path
> echo more stuff
> EOF
> usb_rhs=$(printf '%s\n' "$usb" | sed -e 's:[|\&]:\\&:g;$!s/$/\\/');
I'm sorry, but that last one-line code snippet gives me a headache!!!
It's hard to understand and fragile. I pity the maintainer. And the
first script is way more complicated than needed, and as you astutely
pointed out, subject to failure depending on the value of the usb
variable. There are *much* simpler ways to do what the OP seemed to
want, as pointed out in other replies.
The basic problem is that the OP was writing code in a very bizarre,
obscure way. :( They didn't know any better. But they need to. We are
(or should be) here to teach and learn. So here goes...
1) Do NOT use something like $path as a literal string! Only use $path
when accessing the 'path' variable. :( This kind of bizarre coding
practice is 1) totally not needed, 2) extremely confusing to others.
2) Do NOT write something "clever" that takes many minutes to decipher,
such as the usb_rhs line above. There is usually a much simpler way.
Reading one line of "tricky" code is much harder and much more
error-prone than reading many lines of well-written code. Good code
tells an easy-to-follow story. Bad code presents a puzzle.
Again, here is a very simple, fool-proof (AFAICT) way to do exactly what
the OP wanted (AFAICT) without the weird machinations:
$ cat /tmp/temp.sh
usb=/home/mac
cat << EOF
here it is:
ftpdir=$usb
more stuff
EOF
$ /tmp/temp.sh
here it is:
ftpdir=/home/mac
more stuff
Given that we have not heard back from the OP, I cannot guarantee this
is the desired behavior. So here is a final "rule":
3) Focus on test cases. Clearly define what you are trying to do. What
is the exact input (if any)? Present the input file. Does it take into
account all possibilities? What is the exact desired output / result? If
you get the test cases right, you (or someone on this group) will know
the code is right, because it will produce the correct output. If you
mess up the test cases, things will likely fail in production mode.
If the OP is still around, I would recommend UNIX Shell Programming, by
Kochan and Wood, if looking for a book to read.
Daniel
------------------------------------
------------------------------------
--
------------------------------------
Yahoo Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/sed-users/join
(Yahoo! ID required)
<*> To change settings via email:
[email protected]
[email protected]
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo Groups is subject to:
https://info.yahoo.com/legal/us/yahoo/utos/terms/