【发布时间】:2021-12-25 00:01:52
【问题描述】:
我正在尝试通过将 .js 块替换为我的 main.config.php 文件中的特定行来自动化构建过程。当我运行以下代码时:
declare -a js_strings=("footer." "footerJQuery." "headerCSS." "headerJQuery.")
build_path="./build/build"
config_path="./system/Config/main.config.php"
while read -r line;
do
for js_string in ${js_strings[@]}
do
if [[ $line == *$js_string* ]]
then
for js_file in "$build_path"/*
do
result="${js_file//[^.]}"
if [[ $js_file == *$js_string* ]] && [[ ${#result} -eq 3 ]]
then
sed -i "s/$line/$line$(basename $js_file)\";/g" $config_path
fi
done
fi
done
done < "$config_path"
我收到此消息,但文件尚未更新/编辑:
sed: 1: "./system/Config/main.co ...": invalid command code .
我无法在搜索中找到与此特定消息相关的任何内容。有谁知道我需要更改/尝试替换我的 .php 文件中的特定行吗?
更新的脚本包含相同的消息:
declare -a js_strings=("footer." "footerJQuery." "headerCSS." "headerJQuery.")
build_path="./build/build"
config_path="./system/Config/main.config.php"
while read -r line;
do
for js_string in ${js_strings[@]}
do
if [[ $line == *$js_string* ]]
then
for js_file in "$build_path"/*
do
result="${js_file//[^.]}"
if [[ $js_file == *$js_string* ]] && [[ ${#result} -eq 3 ]]
then
filename=$(basename $js_file)
newline="${line//$js_string*/$filename\";}"
echo $line
echo $newline
sed -i "s\\$line\\$newline\\g" $config_path
echo ""
fi
done
fi
done
done < "$config_path"
例如$line:
$config['public_build_header_css_url'] = "http://localhost:8080/build/headerCSS.js";
例如$newline:
$config['public_build_header_css_url'] = "http://localhost:8080/build/headerCSS.7529a73071877d127676.js";
更新的脚本包含@Vercingatorix 建议的更改:
declare -a js_strings=("footer." "footerJQuery." "headerCSS." "headerJQuery.")
build_path="./build/build"
config_path="./system/Config/main.config.php"
while read -r line;
do
for js_string in ${js_strings[@]}
do
if [[ $line == *$js_string* ]]
then
for js_file in "$build_path"/*
do
result="${js_file//[^.]}"
if [[ $js_file == *$js_string* ]] && [[ ${#result} -eq 3 ]]
then
filename=$(basename $js_file)
newline="${line//$js_string*/$filename\";}"
echo $line
echo $newline
linenum=$(grep -n "^${line}\$" ${config_path} | cut -d':' -f 1 )
echo $linenum
[[ -n "${linenum}" ]] && sed -i "${linenum}a\\
${newline}
;${linenum}d" ${config_path}
echo ""
fi
done
fi
done
done < "$config_path"
【问题讨论】:
-
在
while之前添加set -xv(启用调试模式),运行您的脚本,查看(调试)输出以查看sed命令在变量展开后的样子;我猜一个变量包含一个文件路径(包括正斜杠),这将与您使用正斜杠作为sed脚本分隔符发生冲突 -
请阅读您应用的标签的描述。另外,创建一个minimal reproducible example。特别是,我想知道这是否与 PHP 有任何关系,除了它应用于 PHP 文件。此外,
sed是一个程序,而不是 Bash 内置的或特定于 Bash 的命令。 -
添加到 Ulrich 的评论...看看
sed是如何生成错误消息的...也许添加sed作为标签? -
这是在什么平台上运行的(因此这是
sed的哪个版本)?不同版本的sed有confusingly different syntax for the-ioption。此外,有几个变量引用应该用双引号引起来(shellcheck.net 将指出其中的大部分),在sed命令need to be escaped 中执行这些字符串,你确定./指的是你的目录吗?期待? -
您使用
/作为 sed 的s命令的分隔符,但变量内容包含斜杠。为s使用不同的分隔符。