代码之家  ›  专栏  ›  技术社区  ›  David Gidony

sh shell正在将子shell重定向到文件,找不到正确的语法

  •  0
  • David Gidony  · 技术社区  · 7 年前

    我想以编程方式运行sed命令,并更改参数。 问题是我找不到这样做的正确语法。 我想用这个和配置一个conf文件 将目录路径更改为其他路径。

    我当前正在使用:

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

    我得到错误消息:

    ./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
    

    “>”由于某种原因不起作用。

    我做错了什么?或者最好的方法是什么?

    使现代化

    我降低了结果变量,现在运行这个:

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

    正在>中创建新文件${SOURCE\u DIR}/${tool\u name}。形态, 但搜索/替换是按字面意思进行的,而不是作为变量进行的。。。

    谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   KamilCuk    7 年前

    放置 " 括号内会导致bash想要执行一个名为:

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

    您的系统上不存在此类命令。 也许你打算 “” 外部 $(...) :

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

    更好的方法是,如果您不需要结果变量,如果您想正确地转义 root_inst_dir 变量:

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

    或者,如果需要结果变量:

    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)