0

i want to run a sed command with programatically with changing parameters. the thing is that i cant find the correct syntax to do so. i want to configure a conf file with this and change a dir path to another.

i'm currently using:

RESULT=$("sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf")

and i get the error message:

./change_tst.sh: line 7: sed 's/--ROOT_DIR--//home/test_dir/g' /home/tst/conf.conf > /home/script_tst/conf.conf: No such file or directory

the ">" is not working for some reason.

what am i doing wrong? or what is the best way to do this ?

UPDATE

i drooped the result variable and now running this:

(sed 's/--ROOT_DIR--/$root_inst_dir/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf) > ${SOURCE_DIR}/${tool_name}.conf

the new file is being created in > ${SOURCE_DIR}/${tool_name}.conf, but the search/replace is happening literally and not as a variable...

thanks.

David Gidony
  • 1,243
  • 3
  • 16
  • 31
  • it's there :) double checked. and even if not, it should create it, but it's definitely there – David Gidony May 17 '18 at 11:13
  • I think directory script_tst not exist – ctac_ May 17 '18 at 11:14
  • What are you hoping to store in `RESULT`? The output of the sed command or its exit status or something else? [edit] your question to include concise, testable sample input and expected output so we can help you. – Ed Morton May 17 '18 at 12:16
  • the output of the sed command should be redirected to the file via">" , in the result i expect to get the exit status – David Gidony May 17 '18 at 12:26

1 Answers1

3

Putting " inside parenthesis will result in bash wanting to execute a command named exactly:

sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf"

Such command does not exist on your system. Probably you intended to put " outside $(...):

RESULT="$(sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf)"

Better way, if you don't need the RESULT variable and if you want to properly escape root_inst_dir variable:

sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"

Or if you need RESULT variable:

sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
RESULT=$(cat ${SOURCE_DIR}/${tool_name}.conf)
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Note that there is absolutely no reason to ever write `RESULT="$(cmd > redirected)"`, since it is guaranteed to assign the empty string to RESULT. You might as well write `RESULT=""; cmd > redirected;`! – William Pursell May 17 '18 at 11:32
  • You could do `RESULT=$(cmd | tee redirected)` - although I don't think that's a better than what's suggested in this answer – SpoonMeiser May 17 '18 at 11:44
  • hi, i can live without the result, but your examples dont work, i tried: (sed 's/--ROOT_DIR--/$root_inst_dir/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf) > ${SOURCE_DIR}/${tool_name}.conf , this works but the /search/replace value inside the ' ' is taken literally and not as a variable... how can i escape the var inside '' ? – David Gidony May 17 '18 at 12:41
  • Strings inside `'` are taken literally. If you want variables to expand, use double qoutation marks `"`. And why do you need braces? I would write: `sed 's/--ROOT_DIR--/'"$root_inst_dir"'/g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"` – KamilCuk May 17 '18 at 12:43
  • @KamilCuk then i get this: sed: -e expression #1, char 17: unknown option to `s'... and the sed command needs the ' – David Gidony May 17 '18 at 12:46
  • 1
    If the variable `root_inst_dir` contains `/` characters it gets parsed by sed utility as command separator. Try using `#` as separator, like `sed 's#--ROOT_DIR--#'"$root_inst_dir"'#g'` (and make sure the variable root_inst_dir does not contain any `#` characters. If it does,t hey need to be escaped for sed utility to work) – KamilCuk May 17 '18 at 12:48
  • 1
    @KamilCuk, you are my hero :) thank you very much... update this comment on your answer, and ill mark it as the one. thanks again ! – David Gidony May 17 '18 at 13:06