bash中的变量是否被覆盖?

时间:2020-09-11 13:43:58

标签: bash git sed

我正在编写一个shell脚本来进行git pull并增加ubuntu上托管的Web应用程序的版本。

#!/bin/bash
#Date: September 10th, 2020
#Description: A shell script to pull the latest version to development, and increment the version
#
#Get current version from text file.
while IFS= read -r line; do echo "The current is: ${line}"; done < app-version.txt #${line} outputs version
#Prompt user for new version.
echo "Enter the new version and press [ENTER]."
read ver
#Increment version
echo "Updating Version to: ${ver}"
> app-version.txt
echo $ver > app-version.txt
#Pull latest version.
echo "Pulling files..."
git pull
echo "${ver}" #outputs version entered by user
echo "${line}" #outputs nothing
sed -i -e "s/${line}/${ver}/" sw-base-copy.js # sed: -e expression #1, char 0: no previous regular expression

app-version.txt是一个单行文本文件,包含当前版本,并且在用户输入新版本时更新。

${line}变量在while语句中正确回显,但是当我在sed命令中将其作为正则表达式运行时为空白。为什么?

1 个答案:

答案 0 :(得分:3)

line在对read循环的while的第二次调用中被覆盖,这确定您已到达文件末尾。如果app-version.txt确实是一个单行文件(或更重要的是,您只关心第一行),则不需要循环。只需阅读第一行:

#!/bin/bash

IFS= read -r line < app-version.txt
echo "The current is: $line"

echo "Enter the new version and press [ENTER]."
read ver
echo "Updating Version to: ${ver}"

echo "$ver" > app-version.txt

echo "Pulling files..."
git pull
sed -i -e "s/${line}/${ver}/" sw-base-copy.js