Pfad per script korrigieren

stefkey

Member
Hallo,

kann mir jemand ad hoc sagen wie ich einen Pfad der duch find ausgegeben wird anpasse und in eine Variable schreibe?

zB soll aus:

/Volumes/squeeze/var/www/test.html
das werden
/Volumes/Backup/vs/20130402/var/www/test.html

Es soll quasi am Anfang etwas ersetzt werden. Das müsste doch mit shellbefehlen zu machen sein, oder?
 
Hi,

Code:
 ~ % PFAD="/Volumes/squeeze/var/www/test.html"
 ~ % echo ${PFAD/\/Volumes\/squeeze/\/Volumes\/Backup\/vs\/20130402}
/Volumes/Backup/vs/20130402/var/www/test.html

fiele mir jetzt spontan ein.
 
Code:
find /pfad/ -name test.html | sed 's/^\/Volumes\/squeeze\//\/Volumes\/Backup\/vs\/20130402\//g'
So zum Beispiel. Die Variable(n) könntest du dann z.B. mit einer "for"-Schleife setzen oder ggf. durch eine weitere Pipe mit xargs.
 
Als Shellscript-Baukasten:
Code:
#!/bin/sh

...otherfunctions...

function replace_inline_in_file() {
  local p1="$1"
  local p2="$2"
  local p3="$3"
  local p4="$4"
  find $p1 -findparams -print0 | xargs -0 sed -e "s#$p2#$p3#g" -i $p4
  return
}

...foobar...

# replace_inline_in_file params:
# p1 = path where find will operate in
# p2 = old path to be replaced
# p3 = with this new path
# p4 = file in which replace will be done inplace
p1="/"
p2="/old/path"
p3="/new/path"
p4="file.ext"
replace_inline_in_file $p1 $p2 $p3 $p4

...barfoo...

exit 0
VORSICHT Hausaufgaben:
Es ist keinerlei Fehlerbehandlung enthalten und es wird kein Backup des File in dem der Replace stattfindet angelegt!
Wenn in mehren Files replaced werden soll, dann bitte p4 als Array definieren und find+xargs in eine Schleife packen.


HTH
 
Back
Top