简体   繁体   English

如何使用sed用shell变量替换文件中的字符串

[英]how to use sed to replace a string in a file with a shell variable

Geez, I tried the MAN pages, and several posts here. Geez,我试过MAN页面,这里有几个帖子。 I guess I'm just stupid because I', not getting it. 我想我只是愚蠢因为我',而不是得到它。

I have a javascript file that has {VERSION} and {DISTRO} where the variable string values should be replaced like this. 我有一个javascript文件,其中包含{VERSION}和{DISTRO},其中变量字符串值应该像这样替换。

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}'
};

And this command running for my shell. 这个命令运行我的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. 看起来它应该非常简单直接。

[EDIT] 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

It seems to be that you're trying a combination of piping to sed and using a file. 看起来你正在尝试将sed和使用文件组合在一起。 The following will work perfectly fine, 以下将完美无缺,

sed -i -e "s/{VERSION}/${VERSION}/" -e "s/{DISTRO}/${DISTRO}/" ${OUT_CAT}

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. 你不需要转义$ ,因为你使用-i来就地修改文件,所以你不管道进入sed You also need to use -e for each expression when their are more than one. 当每个表达式不止一个时,您还需要为每个表达式使用-e

EDIT: Here is the bit in the sed manual pages that gives the indication of the issue with -e option (emphasis mine), 编辑:这是sed手册页中的位,用-e选项(强调我的)给出了问题的指示,

If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. 如果没有给出-e, - expression,-f或--file选项,则将第一个非选项参数作为要解释的sed脚本。 All remaining arguments are names of input files ; 所有剩余的参数都是输入文件的名称 ; if no input files are specified, then the standard input is read. 如果未指定输入文件,则读取标准输入。

You don't need to escape the $ : 你不需要逃避$

$ DISTRO="Developer"

$ VERSION="0.9.0"

$ sed -e "s/{VERSION}/$VERSION/" -e "s/{DISTRO}/$DISTRO/"  file
var MyObject = {

    /**
     * @property {string} VERSION Holds the current version of the framework
     */
    VERSION:    '0.9.0',
    /**
     * @property {string} DISTRO Holds the distrobution tag of the framework
     */
    DISTRO:     'Developer'
};

And what is $OUT_CAT_DEBUG that you are piping in? 你输入的$OUT_CAT_DEBUG是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM