Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a path say ==========================

/nfs/old_home/dexter/work/Deamon/Test2/IN/

Inside this path i have script files say: Files1.sh Files2.sh Flles3.sh ....

Filesn.sh

Each of these script files have a line say

export DATABASE_XML=/mnt/nfs/lin_work_live/linear_work/dexter/Deamon/Test2/RDB.xml

===================================================== So say the first path i.e. /nfs/old_home/dexter/work/Deamon/Test2/ has this RDB.xml file and the path Given in that Script file i.e. export DATABASE_XML=**/mnt/nfs/lin_work_live/linear_work/dexter/Deamon/Test2/**RDB.xml is the linked path.

So If i move the script files from say /nfs/old_home/dexter/work/Deamon/Test2/IN/ to /nfs/old_home/dexter/work/Deamon/Test3/IN/ so it should read the RDB.xml file of the /nfs/old_home/dexter/work/Deamon/Test3/ directory and not the Test2..in 1 simple word the path inside the scripts export DATABASE_XML=/mnt/nfs/lin_work_live/linear_work/dexter/Deamon/Test2/RDB.xml Should change to Test3 also this change should reflect in all the File1.sh...FileN.sh files i move. Hope this is much clear now??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...