变量bash脚本,保持它的价值从上一次运行 [英] Variable in Bash Script that keeps it value from the last time running

查看:139
本文介绍了变量bash脚本,保持它的价值从上一次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建持久的可变值的Bash脚本?

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

例如,我一个变量初始化成0时,脚本(具体的时间限制内)首次运行,每一次自动变量增加脚本运行。

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.

推荐答案

你不能,但你可以用一个文件来做到这一点。

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

这篇关于变量bash脚本,保持它的价值从上一次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆