I'm trying to assign the output of a grep command to a variable in a bash script and the variable comes up empty. When I run the following:
el_value=$(grep '<$2>.*<$2>' $1 | sed -e 's/^.*<$2/<$2/' | cut -f2 -d'>'| cut -f1 -d':')
echo "DEBUG: The value of el_value is '$el_value'"
The output is:
DEBUG: The value of el_value is ''
If I run the grep command outside of the script (and substitute the variables for real values) I get the output I'm expecting.
Here is a snippet of script. Use this script and add the values listed above.
#!/bin/bash
if [ $# -ne 3 ]; then
echo 1>&2 'This script replaces xml element’s value with the one provided as a command parameter \n\n\tUsage: $0 <xml filename> <element name> <new value>'
exit 127
fi
el_value=`grep "<$2>.*<$2>" $1 | sed -e "s/^.*<$2/<$2/" | cut -f2 -d'>'| cut -f1 -d':'`
echo "DEBUG: The value of el_value is '$el_value'"