首页 > 解决方案 > 如何在不解析的情况下在bash中写入文件?

问题描述

尝试在 bash 中编写以下命令时,其中的文件具有可变路径/etc/languagetools/language_tool.sh

java -jar /path/to/languagetools/languagetool-commandline.jar "${@:1}"

我在阻止对"${@:1}". 执行解析的函数包含:

#!/bin/bash
some_function() {
  local create_target_file=$(sudo touch 
  $LANGUAGE_TOOL_TARGET_DIR/$LANGUAGE_TOOL_CONTROL_SCRIPTNAME)
  local make_readable=$(chmod 777 $LANGUAGE_TOOL_TARGET_DIR/$LANGUAGE_TOOL_CONTROL_SCRIPTNAME)
  command_one="java -jar 
  $LANGUAGE_TOOL_TARGET_DIR/$LANGUAGE_TOOL_SNAPSHOT_DIRNAME/$LANGUAGE_TOOL_TARGET_FILENAME "
  command_two='${@:1}'
  local write_content_to_file=$(sudo sh -c "echo $command_one$command_two > $LANGUAGE_TOOL_TARGET_DIR/$LANGUAGE_TOOL_CONTROL_SCRIPTNAME")
}

返回:

sh: 1: 替换错误

因此,我很好奇,如何在不解析命令内容的情况下将命令字符串写入文件?

标签: bashecho

解决方案


您不需要命令替换或变量赋值。只是

echo "$command_one$command_two" > "$LANGUAGE_TOOL_TARGET_DIR/$LANGUAGE_TOOL_CONTROL_SCRIPTNAME"

推荐阅读