【问题标题】:Using sed in a Makefile to replace something with a variable在 Makefile 中使用 sed 用变量替换某些内容
【发布时间】:2017-06-06 21:16:04
【问题描述】:

我进行了广泛的搜索,发现了许多类似的问题,但据我所知,没有找到真正符合我要求的问题。

这是最接近的问题:Using sed in a makefile; how to escape variables?

我想在 Makefile 中做的一个非常简化的版本是

sed s~foo~${BAR}~

$BAR 是我的 makefile 中的一个变量。显然,我想将foo 替换为$BAR,而不是当前正在发生的文本${BAR}

既然有人要求,这里是完整版:
请注意,目标是使 Makefile 为 POSIX。

Makefile(仅相关部分):

...
diff:
    scripts/build/diff-all.sh
...

scripts/build/diff-all.sh

#!/bin/bash -e

for DIFFBRANCH in $(cat diff-branches); do
    scripts/build/diff.sh $DIFFBRANCH
done

差异分支

2016_reorg_diff
# more branches here

scripts/build/diff.sh

#/bin/bash -e

DIFFBRANCH=$1
BRANCH=$(git rev-parse --abbrev-ref HEAD)
CHAPTERDIR=src/chapters
SHORTCHAPTERDIR=${CHAPTERDIR##src/} # remove src/ from the start of CHAPTERDIR
CHAPTERS=$(ls $CHAPTERDIR | grep "^\\d\\d_.*\\.tex$")

LATEXARGS="-file-line-error -halt-on-error"

if [[ ! $DIFFBRANCH ]]; then
    echo "usage: $0 DIFFBRANCH"
    exit 1
fi

mkdir -p tmp
mkdir -p pdf

for CHAPTER in $CHAPTERS; do # make tex diff files for each chapter
    SLUG=$(echo $CHAPTER | cut -f 1 -d '.')
    latexdiff-vc --git --so --flatten  --force -r $DIFFBRANCH -r $BRANCH $CHAPTERDIR/$CHAPTER
    mkdir -p tmp/$CHAPTERDIR
    mv -v $CHAPTERDIR/$SLUG-diff$DIFFBRANCH-$BRANCH.tex tmp/src_diff_$DIFFBRANCH/$SHORTCHAPTERDIR/$SLUG-$BRANCH-diff-$DIFFBRANCH.tex
done

rsync -avz --exclude $SHORTCHAPTERDIR src/ tmp/src_diff_$DIFFBRANCH/ # copy all source files to tmp except chapters (because they're already there)

sed -i.bak -E 's~^([[:blank:]]*\subimport{chapters/}{[[:alnum:]_]+)}~\1-$BRANCH-diff-$DIFFBRANCH}~' tmp/src_diff_$DIFFBRANCH/iuf-rulebook.tex 
# this replaces \subimport{chapters/}{01_general} with \subimport{chapters/}{01_general-$BRANCH-diff-$DIFFBRANCH} with the variables expanded
# so that the iuf-rulebook.tex uses the diffed chapters
# the -i.bak is required so SED works on OSX and Linux
rm -f tmp/src_diff_$DIFFBRANCH/*.bak # because of this, we have to delete the .bak files after


mkdir -p tmp/out_diff_$DIFFBRANCH
TEXINPUTS=tmp/src_diff_$DIFFBRANCH: latexmk -pdf $LATEXARGS -quiet -output-directory=./tmp/out_diff_$DIFFBRANCH tmp/src_diff_$DIFFBRANCH/iuf-rulebook.tex
mv tmp/out_diff_$DIFFBRANCH/iuf-rulebook.pdf pdf/iuf-rulebook-$BRANCH-diff-$DIFFBRANCH.pdf
rm -rf tmp/*_$DIFFBRANCH

【问题讨论】:

  • 不清楚你的意思是“不是文本 ${BAR}”。您链接的问题几乎显示了我认为您要问的问题。 sed 's~foo~'${BAR}'~g' 应该可以工作...
  • @l'L'l 不应该,${BAR} 可能包含波浪号、空格或制表符,所有这些都会使您的替换失败。
  • 向我们展示您的 Makefile,尤其是对 BAR 的分配以及您使用 sed 的目标/规则。描述您期望的结果文本以及实际结果。
  • 我已经添加了脚本。请注意,${BAR} 永远不会包含空格或制表符,并且可以预见也不会包含波浪号。
  • sed 中的正则表达式来自另一个 stackexchange 问题:stackoverflow.com/questions/41787537/…

标签: bash variables sed makefile


【解决方案1】:

命令:

sed -e s/number/$(BAR)/g 

验证:

VAR = random number
VAR1 = file
FOO = $(VAR)
$(info before substitution: $(FOO))
FOO = $(shell echo $(VAR) | sed -e s/number/$(VAR1)/g )
$(info after substitution: $(FOO))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2017-12-17
    • 2017-03-21
    • 2011-12-02
    • 2021-11-20
    • 2011-11-03
    • 2011-04-04
    相关资源
    最近更新 更多