-1

I am trying to run a variable sed command depending on user input:

#!/bin/bash

ACTION=$1

if [ "wp" == "$ACTION"  ]
then
   MODIFIEDCONF='sed -i -e "/#Begin wp redirect/,/#End wp redirect/d" /root/test-conf'
else
   MODIFIEDCONF='sed -i -e "/#Begin ghost redirect/,/#End ghost redirect/d" /root/test-conf'
fi

RESULT=$($MODIFIEDCONF)

But I am getting the error:

sed: -e expression #1, char 1: unknown command: `"'

How do I fix the error? Or is there a better way to do it?

Community
  • 1
  • 1
Srikanth Koneru
  • 264
  • 1
  • 3
  • 13
  • I don't think this is the root of your problem , but your comparison should only have a single equals sign. https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash – newbie Jul 20 '18 at 12:21
  • Why you are creating variables? If you want to so see new edited file then simply print it? – RavinderSingh13 Jul 20 '18 at 12:21

4 Answers4

0

Try to avoid doing variable expansion, ie running $var. And it's a custom to have UPPERCASE_VARIABLES beeing exported variables only, if you use variable only in your script use lowercase.
Why not like this:

#!/bin/bash

# get action
action=$1

# check action value
case "$action" 
wp) ;;
*) action=ghost; ;;
# or maybe: *) echo "ERROR: action has bad value! Exiting!" >&2; exit 1; ;;
esac

# run sed
result=$(sed -i -e "s/#Begin $action redirect/,/#End $action redirect/" /root/test-conf)

If you have to result=$($modifiedconf) use bash arrays:

if [ "wp" = "$action" ]; then
   modifiedconf=(sed -i -e "/#Begin wp redirect/,/#End wp redirect/d" /root/test-conf)
else
   modifiedconf=(sed -i -e "/#Begin ghost redirect/,/#End ghost redirect/d" /root/test-conf)
fi
result=$("${modifiedconf[@]}")
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

Use functions to store code, not strings.

#!/bin/bash

ACTION=$1

if [ "wp" == "$ACTION"  ]
then 
   modify_conf() { sed -i -e "/#Begin wp redirect/,/#End wp redirect/d" /root/test-conf; }
else
   modify_conf() { sed -i -e "/#Begin ghost redirect/,/#End ghost redirect/d" /root/test-conf; }
fi

modify_conf

(modify_conf doesn't output anything useful, so there's no need to capture its output.)

Or pass ACTION as an argument:

modify_conf () {
  if [ "$1" = wp ]; then
    sed -i -e "/#Begin wp redirect/,/#End wp redirect/d" /root/test-conf
  else
    sed -i -e "/#Begin ghost redirect/,/#End ghost redirect/d" /root/test-conf
  fi
}

modify_conf "$ACTION"
chepner
  • 497,756
  • 71
  • 530
  • 681
0

Keep it simple. Set your sed command pattern in a variable, then execute that.

case "$ACTION" in
wp) cmd="/#Begin wp redirect/,/#End wp redirect/d"       ;;
 *) cmd="/#Begin ghost redirect/,/#End ghost redirect/d" ;;
esac

sed -i "$cmd" /root/test-conf

or for a little more readability, just set the key and embed.

case "$ACTION" in
wp) key=wp    ;;
 *) key=ghost ;;
esac

sed -i "/#Begin $key redirect/,/#End $key redirect/d" /root/test-conf

Abstraction is power, but unnecessary abstraction is obfuscation.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
-1

To remove the error you reported, you should change the line:

 RESULT=$($MODIFIEDCONF)

to:

 eval "$MODIFIEDCONF"

For more details about this, and references, see for example:

One caveat. The command eval does have a bad reputation: see for instance Why should eval be avoided in Bash, and what should I use instead?. But in this case it is perfectly safe. The extra quotes in eval "$MODIFIEDCONF" (optional here) help protect from unexpected bugs.


Minor point: The sed command you wrote won't return any text (except maybe to stderr). If you really need a value for your variable RESULT, add the line:

  RESULT=$?

$RESULT will always be 0, unless the file /root/test-conf does not exist (or if your script happens to have an invalid sed command, which is not the case here).

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77