OrigScript="/path/to/script.sh"
echo $OrigScript
/path/to/script.sh
#note: no quotes
OrigScript='"/path/to/script.sh"'
echo $OrigScript
"/path/to/script.sh"
#note: with quotes, but not good for filesnames, rather use "$filename" in the command argument
DIRnew='"$(cd "$(dirname "$0")" \&\& pwd -P)"'
echo $DIRnew
"$(cd "$(dirname "$0")" \&\& pwd -P)"
#note: with quotes, escaping & needed for suppressing interpretation by sed
DIRold='"$(dirname "$(readlink -f $0)")"'
echo $DIRold
"$(dirname "$(readlink -f $0)")"
#note: in your question the original DIR=.. did NOT have double quotes,
#I assume a typo
sed -e "s/$DIRold/$DIRnew/g" "$OrigScript"
#note: $OrigScript should NOT have the double quotes in it!
#note: I assume $OrigScript == $StartStopScript
#test:
echo $DIRold | sed "s/$DIRold/$DIRnew/"
"$(cd "$(dirname "$0")" && pwd -P)"
#result includes double quotes!
1) for declaring a variable with (double) quotes, you need to escape the quotes to have the quotes in the string, better use single quotes to make sure the string inside is exactly just the string and no variables are interpreted. This also simplifies the use of &
, and $()
as the are then not interpreted by the shell
2) for echo
ing (double) quotes, escape them
3) for allowing sed
to interpret a shell variable, you MUST use double quotes around the sed
command ( with single quotes, the command sed 's/$a/$b/'
will try to replace the actual string '$a' with the string '$b' , also be aware of the special meaning of $
in sed
), double quotes around the variable within the sed
command is not needed, as sed
is for treating strings with any character (with restrictions regarding the special command characters of sed
).
4) echo "$StartStopScript" | sed -e 's/"$DIRold"/"$DIRnew"/g'
will echo the variable $StartStopScript
( which I assume is simply the file name of the script ) and try to replace the variables in that string (i.e. the file name). For applying sed
to a file, use sed 's/a/b/g' file
(output written to stout
), for overwriting the file, use -i
(use with care, i.e. test for the right substitutions first)
5) &
has a special meaning in sed
, it reprints the initial (matching) pattern:
echo ABC | sed 's/A/&12/'
A12BC
You will have to escape it in the replacement pattern to suppress interpretation:
echo ABC | sed 's/A/\&12/'
&12BC
Thus the variable $DIRnew
needs to have the escape backslashes in its string.
6) Apart from that, I would suggest to rather use an if
loop identifying the OS and to adapt the commands accordingly. That way you only need one script for both OSes. I.e.
if [ OS = Mac ] ; then
dir=MacDir
else
dir=LinuxDir
fi
$OrigScript
==$StartStopScript
?if
loop defining the OS in the script as inif [ OS = Mac ] ; then cmd=MacCommand ; else cmd=LinuxCommand ; fi
. Replacing a string from a variable withsed
's special characters like&
in them is a mess, as you would have to escape them while defining the variable in the first place (e.g.DIRnew='"$(... \&\& pwd -P )"'
)sed -e "s,${DIRold},${DIRnew},g"