如何使用sed用shell变量替换文件中的字符串 [英] how to use sed to replace a string in a file with a shell variable
问题描述
Geez,我尝试了MAN页面,并在此处发表了几篇文章.我想我只是愚蠢的,因为我没有得到它.
Geez, I tried the MAN pages, and several posts here. I guess I'm just stupid because I', not getting it.
我有一个包含{VERSION}和{DISTRO}的javascript文件,其中应像这样替换变量字符串值.
I have a javascript file that has {VERSION} and {DISTRO} where the variable string values should be replaced like this.
var MyObject = {
/**
* @property {string} VERSION Holds the current version of the framework
*/
VERSION: '{VERSION}',
/**
* @property {string} DISTRO Holds the distrobution tag of the framework
*/
DISTRO: '{DISTRO}'
};
此命令在我的shell中运行.无论我采用哪种方式都行不通,否则会出错.
And this command running for my shell. no matter which whay I do this it does not work or i get errors.
VERSION="0.9.0"
DISTRO="Developer"
OUT_CAT=$OUT_CAT_DEBUG
${OUT_CAT} | sed -i "s/{VERSION}/\$VERSION/" -e "s/{DISTRO}/\$DISTRO/" ${OUT_CAT}
#sed -i "s/{VERSION}/$VERSION/" -e "s/{DISTRO}/$DISTRO/" ${OUT_CAT}
那么我以哪种方式迟到呢?似乎应该非常简单直接.
So in which way am I being a tard? Seems like it should be very simple and straight forward.
这是该脚本的简化版本,因此您可以知道我在做什么以及变量来自何处.
Here is s tripped down version of the script so you can tell what I am doing and where variables are coming from.
#!/bin/bash
VERSION="0.9.0"
DEV_DIR=../lib
DIST_DIR=../dist
WEB_DIR=/d/Android_Dev/GitHUB/WEB/h5c3/dist
OUT_CAT_DEBUG=h5c3.debug.cat
OUT_MIN_DEBUG=h5c3.debug.min
OUT_ZIP_DEBUG=$DIST_DIR/h5c3.debug.gz
OUT_CAT_RELEASE=$DIST_DIR/h5c3.release.cat
OUT_MIN_RELEASE=h5c3.release.min
OUT_ZIP_RELEASE=$DIST_DIR/h5c3.release.gz
echo Building Version "$1"...
if [ "$1" == debug ]; then
DISTRO="Developer"
OUT_CAT=$OUT_CAT_DEBUG
OUT_MIN=$OUT_MIN_DEBUG
OUT_ZIP=$OUT_ZIP_DEBUG
# empty it out
> ${OUT_CAT}
cat $DEV_DIR/packed.js >> ${OUT_CAT}
#Development support
cat $DEV_DIR/h5c3_debug.js >> ${OUT_CAT}
elif [ "$1" == release ]; then
DISTRO="Production"
OUT_CAT=$OUT_CAT_RELEASE
OUT_MIN=$OUT_MIN_RELEASE
OUT_ZIP=$OUT_ZIP_RELEASE
# empty it out
> ${OUT_CAT}
cat $DEV_DIR/packed.js >> ${OUT_CAT}
cat $DEV_DIR/h5c3_release.js >> ${OUT_CAT}
else
#publish.sh debug
#publish.sh release
exit 2
fi
#Build Game Core
cat $DEV_DIR/core/stacktrace.js >> ${OUT_CAT}
#Build Externals
cat $DEV_DIR/ext/base64.js >> ${OUT_CAT}
#Build Engine
cat $DEV_DIR/engine/boot.js >> ${OUT_CAT}
#Build Internal Components
cat $DEV_DIR/components/component.js >> ${OUT_CAT}
#Build Systems
cat $DEV_DIR/systems/system.js >> ${OUT_CAT}
#Build Framework
cat $DEV_DIR/framework/page.js >> ${OUT_CAT}
#Build Web App
cat $DEV_DIR/webapp/game.js >> ${OUT_CAT}
if [ "$1" == debug ]; then
#Development support
cat $DEV_DIR/debugger/profiler.js >> ${OUT_CAT}
cat $DEV_DIR/debugger/console.js >> ${OUT_CAT}
cat $DEV_DIR/debugger/debug.js >> ${OUT_CAT}
cat $DEV_DIR/debugger/developer.js >> ${OUT_CAT}
fi
if [ "$1" == release ]; then
echo Removing any debug calls from ${OUT_CAT}
sed -i '/this.debug/d' ${OUT_CAT}
fi
echo "Inserting distribution & version in ${OUT_CAT}"
${OUT_CAT} | sed -i "s/{VERSION}/\$VERSION/" -e "s/{DISTRO}/\$DISTRO/" ${OUT_CAT}
echo Minimizing...[${OUT_CAT} to ${OUT_MIN}]
> ${OUT_MIN}
java -jar yuicompressor-2.4.7.jar ${OUT_CAT} -o ${OUT_MIN}
#echo Compressing...[${OUT_MIN} to ${OUT_ZIP}]
#gzip --best --force ${OUT_MIN}
echo Copying to WEB Folder...
cp --update ${OUT_MIN} ${WEB_DIR}
echo Complete.
exit
推荐答案
似乎是您正在尝试结合使用sed管道和使用文件.以下将完全正常运行,
It seems to be that you're trying a combination of piping to sed and using a file. The following will work perfectly fine,
sed -i -e "s/{VERSION}/${VERSION}/" -e "s/{DISTRO}/${DISTRO}/" ${OUT_CAT}
您不需要转义$
,也不需要通过管道传送到sed
,因为您正在使用-i
来就地修改文件.如果每个表达式都不止一个,则还需要对每个表达式使用-e
.
You don't need to escape the $
, and you don't pipe into sed
since you're using -i
to modify the file in-place. You also need to use -e
for each expression when their are more than one.
这是sed手册页中的位,用于指示使用-e
选项(强调我的问题)的问题,
Here is the bit in the sed manual pages that gives the indication of the issue with -e
option (emphasis mine),
如果未提供-e,-expression,-f或--file选项,则第一个 non-option参数用作要解释的sed脚本. 全部 其余参数是输入文件的名称;如果没有输入文件 指定,然后读取标准输入.
If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read.
这篇关于如何使用sed用shell变量替换文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!