I'm writing a simple package management bash script. Basically it just takes a package installed into /pkg/<PACKAGENAME> and then symlinks the files to the correct locations. It works fine, the only problem is that I have to put the path to the specific package inside the script (i.e. "/pkg/binutils-2.17"). So I thought instead of doing that, I'd use variables and have the user input the variables.
The problem I'm running into now is that I need to edit these variables. I'm using sed to do this and when I pass a variable equal to something like "
/pkg/binutils-2.17/usr", sed thinks the "/" chars are delimters and that the "." chars means "any character". So I need to escape all of sed's "special" characters with a "\" like it says in the title. So I would need to change the above variable to "
\/pkg\/binutils-2\.17\/usr"
Here's the setup I've got now for escaping these "special" characters:
Code:
### THESE ARE THE ORIGINAL VARIABLES WITHOUT ESCAPING
# $system = location of system's root directory.
system="/home/Desktop/sources"
# $pkgsdir = location of directory that stores the packages.
pkgsdir="$system/pkg"
# $pkg = directory of the package being installed.
pkg="$pkgsdir/lynx-2.8.6"
### THESE ARE THE NEW VARIABLES WITH ESCAPING
sed_system=`echo $system | sed 's@\/@\\\/@g' | sed 's@\.@\\\.@g' | sed 's@\\$@\\\$@g' | sed 's@\\^@\\\\^@g' | sed 's@\*@\\\\*@g'`
sed_pkgsdir=`echo $pkgsdir | sed 's@\/@\\\\/@g' | sed 's@\\.@\\\\.@g | sed 's@\\$@\\\\$@g' | sed 's@\\^@\\\\^@g' | sed 's@\*@\\\\*@g'`
sed_pkg=`echo $pkg | sed 's@\/@\\\\/@g' | sed 's@\\.@\\\\.@g' | sed 's@\\$@\\\\$@g | sed 's@\^@\\\\^@g' | sed 's@\*@\\\\*@g'`
It doesn't work very well (making changes to the sed syntax seems to break it), it's complex, and I need to do a lot of editing to make a simple change. What would be a better way to escape the special characters?
Any help is, as always, greatly appreciated.