You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术咨询:如何实现脚本每次运行时内部变量值不同及Bash脚本问题

Solutions to Your Bash Script Questions

Let's tackle your two questions one by one, with practical examples you can adapt directly.


1. Getting Dynamic Expected Values Each Time the Script Runs

If you need your script's internal variable to pull a different expected value on every run, here are three common approaches depending on your use case:

Option 1: Random Values (for non-deterministic ranges)

If the expected value just needs to be a random number within a specific range (e.g., 70-100% for your file check), use Bash's built-in RANDOM variable:

# Generate a random expected percentage between 70 and 100
expected_percent=$(( RANDOM % 31 + 70 ))  # 31 = 100-70+1 to include both endpoints

Option 2: Cycle Through a Predefined List

If you have a fixed set of expected values you want to rotate through (e.g., 50%, 75%, 90%, 100%), track the last used index in a file to pick the next value each run:

# Define your list of expected values
expected_values=(50 75 90 100)
# File to store the last index we used (persists between runs)
index_tracker="/tmp/expected_value_index.txt"

# Initialize the tracker if it doesn't exist
[[ ! -f $index_tracker ]] && echo 0 > "$index_tracker"

# Get current index and corresponding value
current_index=$(cat "$index_tracker")
current_expected=${expected_values[$current_index]}

# Update index for next run (cycle back to 0 when we reach the end)
next_index=$(( (current_index + 1) % ${#expected_values[@]} ))
echo "$next_index" > "$index_tracker"

# Now use $current_expected in your script logic
echo "Today's expected percentage: $current_expected%"

Option 3: Pull from a Dynamic Local Source

If the expected value is managed externally (e.g., updated by another script or user), read it from a local file:

# Path to a file that holds the current expected value
expected_value_file="/path/to/expected_value.txt"

# Read the value (make sure the file only contains the number/percentage)
current_expected=$(cat "$expected_value_file")

2. Bash Script for File Check, Status Tracking, and HTML Email

Let's build a complete script that addresses your requirements: checks folder file count every 30 minutes, tracks verification status, appends data to a log, and sends an HTML table via email.

Full Script Example

#!/bin/bash

# --------------------------
# Configuration (Edit These)
# --------------------------
EXPECTED_TOTAL=100          # Total number of files we expect
CUTOFF_TIME="2024-05-20 18:00:00"  # Deadline (YYYY-MM-DD HH:MM:SS)
WATCH_DIR="/path/to/your/target/folder"
DATA_LOG="/tmp/file_check_history.csv"
EMAIL_TO="your.email@example.com"
EMAIL_SUBJECT="File Ingestion Status Update"
EXPECTED_THRESHOLD=80       # % of files needed to mark as verified

# --------------------------
# Initialize Log File
# --------------------------
# Add CSV headers if log doesn't exist
if [[ ! -f $DATA_LOG ]]; then
    echo "Timestamp,File Count,Percentage,Verification Status" > "$DATA_LOG"
fi

# --------------------------
# Calculate Current Metrics
# --------------------------
# Count files (adjust find command if you need to filter by file type, e.g., -name "*.csv")
CURRENT_COUNT=$(find "$WATCH_DIR" -type f | wc -l)

# Calculate percentage (avoid division by zero)
if [[ $EXPECTED_TOTAL -eq 0 ]]; then
    CURRENT_PERCENT="0%"
else
    CURRENT_PERCENT="$(( (CURRENT_COUNT * 100) / EXPECTED_TOTAL ))%"
fi

# --------------------------
# Determine Verification Status
# --------------------------
CURRENT_TIME=$(date +"%Y-%m-%d %H:%M:%S")
VERIFICATION_STATUS="未验证"

# Mark as verified if threshold is met OR deadline has passed
if [[ ${CURRENT_PERCENT%?} -ge $EXPECTED_THRESHOLD ]] || [[ "$CURRENT_TIME" > "$CUTOFF_TIME" ]]; then
    VERIFICATION_STATUS="已验证"
fi

# --------------------------
# Append Data to Log
# --------------------------
echo "$CURRENT_TIME,$CURRENT_COUNT,$CURRENT_PERCENT,$VERIFICATION_STATUS" >> "$DATA_LOG"

# --------------------------
# Generate HTML Table
# --------------------------
HTML_TABLE="<html><body>
<h3>File Ingestion Status History</h3>
<table border='1' cellpadding='5' cellspacing='0'>
<thead>
<tr bgcolor='#f0f0f0'>
<th>Timestamp</th>
<th>Files Received</th>
<th>Percentage of Total</th>
<th>Verification Status</th>
</tr>
</thead>
<tbody>"

# Read log lines (skip header) and build table rows
tail -n +2 "$DATA_LOG" | while IFS=, read -r TS COUNT PERCENT STATUS; do
    # Highlight verified rows for clarity
    if [[ "$STATUS" == "已验证" ]]; then
        HTML_TABLE+="<tr bgcolor='#d4edda'>"
    else
        HTML_TABLE+="<tr>"
    fi
    HTML_TABLE+="<td>$TS</td><td>$COUNT</td><td>$PERCENT</td><td>$STATUS</td></tr>"
done

HTML_TABLE+="</tbody></table></body></html>"

# --------------------------
# Send HTML Email
# --------------------------
# Use mailx (adjust command if your system uses sendmail or another tool)
echo -e "Content-Type: text/html\n\n$HTML_TABLE" | mailx -s "$EMAIL_SUBJECT" "$EMAIL_TO"

Key Notes:

  • Status Logic: The script marks status as "已验证" if either the file percentage meets/exceeds your threshold, or the deadline has passed (whichever comes first).
  • Log Persistence: All runs append data to a CSV log, so the HTML table will grow with each check.
  • Email Formatting: The HTML table includes subtle styling (gray header, green background for verified rows) to make it easier to read.
  • Cron Job Setup: To run every 30 minutes, add this to your crontab (run crontab -e):
    */30 * * * * /path/to/your/script.sh >> /tmp/script_run_log.txt 2>&1
    

Troubleshooting Tips:

  • Ensure mailx is installed (on Debian/Ubuntu: sudo apt install mailutils; on RHEL/CentOS: sudo yum install mailx).
  • Verify the find command counts the correct files (add filters like -name "*.csv" if needed).
  • Double-check date formats for CUTOFF_TIME (must match date +"%Y-%m-%d %H:%M:%S" output).

内容的提问来源于stack exchange,提问作者shruk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:13:21