shell脚本:替换内容

Jil*_*448 2 bash shell awk sed

我有以下信息的文件

FileName:VersionInfo.properties

Installed Version:13.7.0-2
Previous Version:13.6.0-12
Run Code Online (Sandbox Code Playgroud)

FileName:rollback.sh

#!/bin/bash
uninstall_rpm ClientInfrastructure $version
Run Code Online (Sandbox Code Playgroud)

如何从早期版本的VersionInfo.properties文件中将值转换为变量$ version.$ version的值应为13.6.0-12

jay*_*ngh 5

sed 解:

version=$(sed -n 's/Previous Version:\(.*\)/\1/p' VersionInfo.properties)
Run Code Online (Sandbox Code Playgroud)

要在脚本中使用它,您可以执行以下操作:

#!/bin/bash
version=$(sed -n 's/Previous Version:\(.*\)/\1/p' VersionInfo.properties)
uninstall_rpm ClientInfrastructure "$version"
Run Code Online (Sandbox Code Playgroud)

如果这就是您脚本中的全部内容,那么您基本上可以在命令行中执行此操作.

uninstall_rpm ClientInfrastructure "$(sed -n 's/Previous Version:\(.*\)/\1/p' VersionInfo.properties)"
Run Code Online (Sandbox Code Playgroud)

  • @anubhava哈哈,我开始编写`awk`解决方案然后看到已发布的两个版本.从`grep`开始,发布了.所以`sed`是我的最后选择.我变老了......跟不上这里的每个人都不太快......;) (2认同)