restatement of the problem
- there are some files in folder
/orig
- there is a configuration file
/orig/conf
- there are some scripts
/orig/IN/script1
.. /orig/IN/scriptN
- each script contains a line:
export VAR="/orig-sym/conf"
/orig-sym
is a symbolic link to /orig
- the files are copied to a new folder
/new
- the config file moves from
/orig/conf
to /new/conf
- scripts move from
/orig/IN/scriptX
to /new/IN/scriptX
- change relevant lines to
export VAR="/new-sym/conf"
/new-sym
is a symbolic link to /new
solution 1
Do a search & replace on the files scriptX
after they have been moved.
Something along the lines of sed -i s/find/replace/ file*
can work but care must be taken to sure replace
does not contain characters that are special to sed (eg. /
, [
, etc). Quoting these safely can be complicated.
However, we can sidestep the issue. For example, using the shell and perl:
$ mkdir "/new"
$ ln -s "/new" "/new-sym"
$ cp -R "/orig/." "/new"
$ perl -i -pe '
BEGIN { $replacement = shift }
s/^(export VAR)=.*/$1="$replacment"/
' "/new-sym" "/new/IN/"script*
- create the new folder and the symlink to it
- copy the files into the new folder
- run a perl script to do search and replace
- first argument is replacement symlink path (
/new-sym
)
- remaining arguments are the files to change
-i
overwrites files
BEGIN
copies the new path avoiding the escaping complications
s///
performs the replacement
- Note: check that only desired lines match
solution 2
Don't use absolute paths.
As the configuration file is always one level up from the scripts, relative paths won't need to be changed.
If the script is executed directly, $0
should contain its path.
So in many situations, something like this will work:
#!/bin/sh
# scriptX
# ...
# commands that must not include "cd", "pushd", etc
# ...
top=$(dirname -- "$0")
absolute_top=$(CDPATH= cd -- "$top" && pwd -P)
# ...
# commands that may include "cd", "pushd", etc
# ...
export VAR="$(absolute_top)/../conf
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…