0

I have string which looks like this:

/mnt/blumeta0/db2/head/home/db2inst1/sqllib/ctrlha/ /mnt/blumeta0/db2/head/home/db2inst1/sqllib/ctrl/ /mnt/blumeta0/db2/head/home/db2inst1/sqllib/log/ /mnt/blumeta0/db2/head/home/db2inst1/sqllib/cfg/

This is stored in variable sqllib_files_folders in the script below : I am trying to remove all occurences of prefix TMP_SOTRAGE which is /mnt/blumeta0/db2 for which I wrote the following:

#!/bin/sh
TMP_STORAGE="/mnt/blumeta0/db2"
METADATA_PATH="/head"
sqllib_files_folders=$(find $TMP_STORAGE$METADATA_PATH/home/db2inst1/sqllib/ -maxdepth 1 -mindepth 1 '(' '(' -type d -printf '%p/ ' ')' -o -printf '%p ' ')')
sqllib_files_folders=echo ${sqllib_files_folders//$TMP_STORAGE/}
echo $sqllib_files_folders

This works for string which does not have /. I have tried escaping with \. But that does not work either. Any help please?

codec
  • 7,978
  • 26
  • 71
  • 127
  • You should store the paths in an array instead of relying on them not containing spaces, see https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash – Benjamin W. Mar 25 '19 at 14:50

2 Answers2

1

For example:

sqllib_files_folders=$(echo $sqllib_files_folders|sed "s%$TMP_STORAGE%%g")
Alexey Godin
  • 307
  • 1
  • 6
1

2 ideas come to mind:

  1. cd to the directory first so the prefix is not in the find output in the first place:

    sqllib_files_folders=$(cd "$TMP_STORAGE" && find ./"$METADATA_PATH"/home/db2inst1 ...)
    
  2. backslash escape the slashes for the parameter expansion replacement (this one's a mouthful):

    sqllib_files_folders=${sqllib_files_folders//${TMP_STORAGE//\//\\/}/}
    # ...........................................^^...........^^^^^^^^^
    
glenn jackman
  • 238,783
  • 38
  • 220
  • 352