2

So I have a Bash script which looks like this:

#!/bin/bash

extentemplate="
exten => `$prevchannel`,n,Queue(`$prevchannelname`)\
exten => `$prevchannel`,n,Hangup()\
\
;`$channelname`\
exten => `$channel`,1,Answer()\
exten => `$channel`,n,ResetCDR()\
exten => `$channel`,n,Set(CDR(accountcode)=`$accountcode`)\
exten => `$channel`,n,Set(CDR(userfield)=inbound)\
exten => `$channel`,n,Set(MONITOR_FILENAME=`$channelname`_\${UNIQUEID})\
exten => `$channel`,n,Set(CALLERID(name)=`$channelname`)\
exten => `$channel`,n,Queue(`$channelname`)\
exten => `$channel`,n,Hangup()\
"

FILE="/etc/asterisk/extensions.pap.confTEST"

args=("$@")

operation=("${args[0]}")
channel=("${args[1]}")
accountcode=("${args[2]}")
channelname=("${args[3]}")
music=("${args[6]}")
prevchannel=("${args[4]}")
prevchannelname=("${args[5]}")

if [ $operation = "add" ] ; then
        sed "s/exten => `$prevchannel`,n,Queue(`$prevchannelname`)/`$extentemplate`/" $FILE
fi

According to the answers I've seen so far this should be able to get substituted correctly however apparently I haven't escaped something or other correctly.

My args don't contain any special characters.

Does anyone know why this is failing with the "unterminated `s' command" error?

3
  • 7
    There are a lot of substitutions. If I were debugging it myself I'd put set -x in the script and look at what is actually being sent to sed (you can use set +x to stop printing commands).
    – user4443
    Sep 1, 2015 at 15:29
  • extentemplate= starts with a newline. Are all these backticks really single quotes?
    – meuh
    Sep 1, 2015 at 16:49
  • 2
    Please cut your code down to the minimal example. Sep 1, 2015 at 17:52

1 Answer 1

1

You'll need to extract the variables from the arguments first to get the values substituted into the template. You do this in a really odd way:

args=("$@")

operation=("${args[0]}")
channel=("${args[1]}")
accountcode=("${args[2]}")
channelname=("${args[3]}")
music=("${args[6]}")
prevchannel=("${args[4]}")
prevchannelname=("${args[5]}")

operation is an array holding one element. Bash happens to handle $operation like ${operation[0]}. Why not this?

operation=$1
channel=$2
accountcode=$3
channelname=$4
prevchannel=$5
prevchannelname=$6
music=$7

When declaring your template, you have to be careful to escape all backticks, otherwise you get command substitution.

$ extentemplate="\
exten => \`$prevchannel\`,n,Queue(\`$prevchannelname\`)\
exten => \`$prevchannel\`,n,Hangup()\
\
;\`$channelname\`\
exten => \`$channel\`,1,Answer()\
exten => \`$channel\`,n,ResetCDR()\
exten => \`$channel\`,n,Set(CDR(accountcode)=\`$accountcode\`)\
exten => \`$channel\`,n,Set(CDR(userfield)=inbound)\
exten => \`$channel\`,n,Set(MONITOR_FILENAME=\`$channelname\`_\${UNIQUEID})\
exten => \`$channel\`,n,Set(CALLERID(name)=\`$channelname\`)\
exten => \`$channel\`,n,Queue(\`$channelname\`)\
exten => \`$channel\`,n,Hangup()\
"

All those line continuations mean you don't have any actual newlines in your template string.

Then

sed "s/exten => `$prevchannel`,n,Queue(`$prevchannelname`)/`$extentemplate`/" $FILE

Again, due to the double quotes, you're getting variable and command substitution. You'll need single quotes, then double quotes to substitute variables

sed 's/exten => `'"$prevchannel"'`,n,Queue(`'"$prevchannelname"'`)/`'"$extentemplate"'`/' "$FILE"
# ..^............^^............^^...........^^................^^....^^..............^^..^.^.....^

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .