Home  >  Article  >  Backend Development  >  How to use shell to modify the contents of a specified file

How to use shell to modify the contents of a specified file

WBOY
WBOYOriginal
2016-12-01 00:25:572529browse

现在要写一个shell 更新完静态资源后自动添加其版本号

比如有webroot/a.php

<code class="php">define('STATIC_VERSION', '2016.11.25.01.stable-v1');</code>

执行脚本后将STATIC_VERSION常量中的2016.11.25.01.stable-v1,更改为:当前年.当前月.当前日.两位随机数.可定义的字符串

这种需求该怎样去实现呀?感谢各位专业大神

回复内容:

现在要写一个shell 更新完静态资源后自动添加其版本号

比如有webroot/a.php

<code class="php">define('STATIC_VERSION', '2016.11.25.01.stable-v1');</code>

执行脚本后将STATIC_VERSION常量中的2016.11.25.01.stable-v1,更改为:当前年.当前月.当前日.两位随机数.可定义的字符串

这种需求该怎样去实现呀?感谢各位专业大神

保存下面代码为build.sh文件

<code>#!/bin/bash

FILENAME=$1
VERSION_STR=$2
# 这里需要使用自己的随机数生成方式
RAND_STR=03

VERSION=`date +%Y.%m.%d.$RAND_STR.$VERSION_STR`

sed "/define('STATIC_VERSION'/ c define('STATIC_VERSION', '$VERSION')" $FILENAME
</code>

之后需要添加执行权限chmod +x build.sh

使用方式:

<code>./build.sh test.php stable-v1</code>

谢邀,勉强写个。
由于实际应用中,采用行替换非常不灵活,此答案忽略,采用楼下的正则匹配。
version='2016.11.25.01.stable-v2'
sed -i "3cdefine('STATIC_VERSION', '$version');" ./test.php

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn