bash - 在 bash 中创建“首次运行”通知?
问题描述
在 bash 脚本中发出“首次运行”通知的最佳方法是什么?我试着export FIRSTRUN="no"
先放,然后放
`if [ -z "$FIRSTRUN" ]; then
echo "It seems that this may be your first time running this script.";
echo "Please ${GREEN}test whether the needed components are installed${RESET}.";
echo "";
fi`
但它只是在每次运行脚本时执行。如果我将ifexport FIRSTRUN="no"
部分放在后面,它永远不会运行代码的if部分。所以这行不通。
我是新手,所以请帮忙:D
解决方案
您无法从脚本中修改父 shell 状态,因此尝试导出变量将不起作用。
您可以使用配置文件来存储一个变量,该变量确定脚本是否已经运行:
#!/bin/bash
# load vars from config at the start of the script
if [[ -f ~/.your_script.conf ]]; then
. ~/.your_script.conf
fi
# check whether var has been set to determine first run
if [[ -z $has_run ]]; then
echo 'first run'
# set variable in config file for next time
echo 'has_run=1' >> ~/.your_script.conf
fi
或者,如果这是您拥有的唯一“配置”,您可以在第一次运行后创建一个文件并检查其是否存在以确定脚本是否已运行。
推荐阅读
- docker - 编译一个容器化的 rust 应用程序以在 raspberry pi 4 (arm64/armv8) 上运行。无法执行“没有这样的文件或目录”
- java - 我如何在 android studio Game Level Selector-Android studio,java 上创建
- reactjs - onClick 附加到超链接不运行
- javascript - 使用 Javascript 设置值 A 选择
- python - 在 PySpark 上将日期时间转换为日期
- android - 是否可以为 Android 中的 ImageView 或 Button 赋值?
- python - 如何将 python --version stdout 保存到文本文件?
- uwp - 在 UWP NavigationView 中更改后退按钮的前景色
- html - 无法使主标签扩展到显示器中的所有空间
- google-cloud-platform - 如何将请求的响应正文格式化为 Google Cloud Platform API 请求的预测端点