If I quit using variables, and just write the regexes directly into to the last sed command, everything works. But as it is here, no substitutions are done?

#!/bin/bash
#html substitutions
ampP="\&"
ampR="&"

ltP="\<"
ltR="<"

gtP="\&gt;"
gtR=">"

quotP="\&quot;"
quotP2='\&#8220;'
quotP3="\&#8221;"
quotR="\""

tripDotP="\&#8230"
tripDotR="..."

tickP="\&#8217;"
tickR="\´"

#get a random page, and filter out the quotes
#pick a random quote
#translate wierd html symbols
curl "www.yodaquotes.net/page/$((RANDOM % 9 +1))/" -Ls | sed -nr 's/.*data-text=\"([^\"]+)\".*/\1/p' \
| sort -R | head -n1 \
| sed 's/"$ampP"/"$ampR"/g; s/$ltP/$ltR/g; s/$gtP/$gtR/g; s/$quotP/$quotR/g; s/"$quotP2"/"$quotR"/g; s/$quotP3/$quotR/g; s/$tripDotP/$tripDotR/g; s/$stickP/$stickR/g'
有帮助吗?

解决方案

This sed isn't going to work:

sed 's/"$ampP"/"$ampR"/g'

because of wrong shell quoting. Your shell variables won't be expanded at all in single quotes. Try using this form:

sed "s~$ampP~$ampR~g"

其他提示

Debugging 101: Let's just echo what sed receives:

echo 's/"$ampP"/"$ampR"/g; s/$ltP/$ltR/g; s/$gtP/$gtR/g; s/$quotP/$quotR/g; s/"$quotP2"/"$quotR"/g; s/$quotP3/$quotR/g; s/$tripDotP/$tripDotR/g; s/$stickP/$stickR/g'

s/"$ampP"/"$ampR"/g; s/$ltP/$ltR/g; s/$gtP/$gtR/g; s/$quotP/$quotR/g; s/"$quotP2"/"$quotR"/g; s/$quotP3/$quotR/g; s/$tripDotP/$tripDotR/g; s/$stickP/$stickR/g

That doesn't look right now, does it?

There's no variable substitution within single quotes in bash. That's why we have two different quotes, so you can decide which one is more appropriate for the task.

For readability I would suggest putting each sed command within double quotes.

Like this: "s/$ampP/$ampR/g"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top