shell - 如何添加一个变量来计算脚本被调用的次数?

标签 shell

我正在编写一个脚本,以便在脚本运行后将数据添加到文件中。 由于我需要转发邮件数据,因此我需要一个变量来分配调用脚本的次数。

我正在使用一个存储计数值的临时文件。每次调用脚本时,都会调用变量 temp,该值会递增并存储在 count 中。增加的值然后存储回温度。

count=$((temp + 1))
echo $count > temp
printf "%d\t%10s\t" "$count" "$datet" >> table

这是我正在使用的代码,但温度没有增加......?

最佳答案

在执行其他操作之前先读取之前的值:

temp=$(cat temp)
count=$((temp + 1))
echo "$count" > temp
printf "%d\t%10s\t" "$count" "$datet" >> table

测试

$ echo "0" > temp
$ ./a
$ cat temp 
1
$ ./a
$ cat temp 
2

关于shell - 如何添加一个变量来计算脚本被调用的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302210/

相关文章:

java - 获取管道起始处进程的 pid

swift - 从 Xcode 按钮运行 Shell 脚本

shell - 从子 shell 运行 shell 命令

shell - FFmpeg - 连接未知格式的视频

Linux Shell 脚本剪切包含路径的变量的最后一部分

bash - 在 bash 中检查 isatty

bash - 在 shell 脚本中迭代名称为folder0100、folder0101 到folder1100 的文件夹

linux - centOS中的困惑路径

linux - 纯文本文件转换为html文件

linux - 在 bash 脚本中使用 grep 命令将变量值传递给 --include 参数