bash - Is my variable being overwritten in bash?
问题描述
I'm writing a shell script to do a git pull and increment the version of my web app hosted on ubuntu.
#!/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
is a one line text file that contains the current version, and is updated when the user enters a new version.
the ${line}
variable echos properly in the while statement, but is blank when i run it as my regex in the sed
command. Why?
解决方案
line
gets overwritten on the second call to read
(of the while
loop) which determines that you have reached the end of the file. If app-version.txt
is really a one-line file (or more importantly, you only care about the first line), you don't need a loop. Just read the first line:
#!/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
推荐阅读
- apache - Apache URL 到文件
- java - YouTube 分析示例 java 程序,在允许用户同意并验证我的应用程序后,我收到 404 错误响应
- excel - 如何找到表格的最后一行并清除某些列?
- react-native - 如何避免我的键盘向上移动我的页脚
- r - 如何在R中为仪表的指针设置动画?
- logging - 仅为选定的 pod 激活 kubernetes 日志
- java - 如何将 xml InputStream 转换为小打印意图?
- python - 无法单击此按钮 selenium webdriver python
- python - 如何从python导入弧度文件,我必须如何打印出一个大数字
- javascript - 使用 pdf.js 时无法读取未定义的属性“GlobalWorkerOptions”