0

I am working on a bash script that should find files such as

/var/www/templates/testdoctype/test_file.html.head

and return something like

cp -f '/var/www/sites/t/test/test_file.html' '/home/user/tmp/test_file.html' 

my script so far looks like this:

#!/bin/bash
DOCPATH='/var/www/templates/testdoctype'
INSTALL_PATH='/var/www/sites/t/test'
PKGBACKPATH='/home/user/tmp'
function find_suffix_head {
 find "$FROM/$DOCTYPE" -type f -iname "*.head" -print \
 | awk -v docpath="$DOCPATH" -v installpath="$INSTALL_PATH" -v pkgbackpath="$PKGBACKPATH" \
 '{ sub( docpath, installpath ) sub(/.head$/, "") } { printf "cp -f ""'\''"0ドル"'\''"" " ; sub( installpath, pkgbackpath ) ; print "'\''"0ドル"'\''" }'
}
find_suffix_head

This returns

cp -f '/var/www/templates/testdoctype/test_file.html' '/var/www/templates/testdoctype/test_file.html'

So, sub(/.head$/, "") works as it should, but sub( docpath, installpath ) and sub( installpath, pkgbackpath ) does not.

Aziz Shaikh
16.6k11 gold badges65 silver badges81 bronze badges
asked Jan 17, 2014 at 11:04

1 Answer 1

2

No need for awk, you can do it with bash:

function find_suffix_head {
 find "$FROM/$DOCTYPE" -type f -name "*.head" | while read filename; do
 filename=${filename%.head} # strip suffix
 filename=${filename#/var/www/templates/testdoctype} # strip prefix
 echo cp -f "$INSTALL_PATH/$filename" "$PKGBACKPATH/$filename"
 done
}

From there you can just run the cp, too, rather than echoing it.

answered Jan 17, 2014 at 13:23
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! That was indeed a much cleaner solution, thank you very much!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.