Bash脚本中的变量,使其保持上次运行时的值

时间:2022-06-21 19:32:45

Can I create a Bash script with persistent variable-values?

我可以使用持久变量值创建Bash脚本吗?

For example, I initialize a variable with 0 when the script runs for the first time (during a specific time limit), and the variable increases automatically with every time the script is running.

例如,当脚本第一次运行时(在特定时间限制内),我将变量初始化为0,并且每次脚本运行时变量都会自动增加。

3 个解决方案

#1


14  

you can't, but you can use a file to do it

你不能,但你可以用一个文件来做

#!/bin/sh

# if we don't have a file, start at zero
if [ ! -f "/tmp/value.dat" ] ; then
  value=0

# otherwise read the value from the file
else
  value=`cat /tmp/value.dat`
fi

# increment the value
value=`expr ${value} + 1`

# show it to the user
echo "value: ${value}"

# and save it for next time
echo "${value}" > /tmp/value.dat

#2


3  

I'm afraid you have to save the state in a file somewhere. The trick is to put it somewhere the user will be able to write to.

我担心你必须将状态保存在某个文件中。诀窍是把它放在用户能够写入的地方。

yourscriptvar=0

if [ -e "$HOME/.yourscriptvar" ] ; then
    yourscriptvar=$(cat "$HOME/.yourscriptvar")
fi

# do something in your script

#save it output the file
echo $yourscriptvar > "$HOME/.yourscriptvar"

#3


3  

I had the same problem a few days ago and I've written my own tool to do the work because there is not other way to do something similar.

几天前我遇到了同样的问题而且我已经编写了自己的工具来完成工作,因为没有其他方法可以做类似的事情。

gvar is a pure Bash key-value store where each user has a different collection of data. The records are stored in the user's home directory.

gvar是一个纯Bash键值存储,每个用户都有不同的数据集合。记录存储在用户的主目录中。

Here it is the most interesting functions from the source code:

这是源代码中最有趣的功能:

get_variable() {
  cat $FILE | grep -w $1 | cut -d'=' -f2
}

set_variable() {
  echo $1=$2 >> $FILE
}

remove_variable() {
  sed -i.bak "/^$1=/d" $FILE
}

#1


14  

you can't, but you can use a file to do it

你不能,但你可以用一个文件来做

#!/bin/sh

# if we don't have a file, start at zero
if [ ! -f "/tmp/value.dat" ] ; then
  value=0

# otherwise read the value from the file
else
  value=`cat /tmp/value.dat`
fi

# increment the value
value=`expr ${value} + 1`

# show it to the user
echo "value: ${value}"

# and save it for next time
echo "${value}" > /tmp/value.dat

#2


3  

I'm afraid you have to save the state in a file somewhere. The trick is to put it somewhere the user will be able to write to.

我担心你必须将状态保存在某个文件中。诀窍是把它放在用户能够写入的地方。

yourscriptvar=0

if [ -e "$HOME/.yourscriptvar" ] ; then
    yourscriptvar=$(cat "$HOME/.yourscriptvar")
fi

# do something in your script

#save it output the file
echo $yourscriptvar > "$HOME/.yourscriptvar"

#3


3  

I had the same problem a few days ago and I've written my own tool to do the work because there is not other way to do something similar.

几天前我遇到了同样的问题而且我已经编写了自己的工具来完成工作,因为没有其他方法可以做类似的事情。

gvar is a pure Bash key-value store where each user has a different collection of data. The records are stored in the user's home directory.

gvar是一个纯Bash键值存储,每个用户都有不同的数据集合。记录存储在用户的主目录中。

Here it is the most interesting functions from the source code:

这是源代码中最有趣的功能:

get_variable() {
  cat $FILE | grep -w $1 | cut -d'=' -f2
}

set_variable() {
  echo $1=$2 >> $FILE
}

remove_variable() {
  sed -i.bak "/^$1=/d" $FILE
}