如何编写Bash脚本读取CSV文件并按计划执行测试
Bash Script to Run Scheduled Tests from CSV
Here's a practical Bash script that fulfills your requirements, with clear explanations of how each part extracts values from your CSV and handles scheduled execution:
The Script
#!/bin/bash # Replace with your actual CSV file path CSV_PATH="your_tests.csv" # Step 1: Extract total number of tests from the first line total_tests=$(head -n 1 "$CSV_PATH" | cut -d',' -f2 | xargs) echo "Found $total_tests tests scheduled in the CSV." # Step 2: Loop through each test entry (skip first 2 lines: count line + header) tail -n +3 "$CSV_PATH" | head -n "$total_tests" | while IFS=',' read -r test_id start_time jmx_file csv1 csv2 csv3; do # Clean up any extra whitespace from fields start_time=$(echo "$start_time" | xargs) jmx_file=$(echo "$jmx_file" | xargs) csv1=$(echo "$csv1" | xargs) csv2=$(echo "$csv2" | xargs) csv3=$(echo "$csv3" | xargs) # Step 3: Calculate wait time until the scheduled start time current_epoch=$(date +%s) # Parse target time; if it's already passed today, use tomorrow's instance target_epoch=$(date -d "$start_time" +%s 2>/dev/null) if [ "$target_epoch" -lt "$current_epoch" ]; then target_epoch=$(date -d "tomorrow $start_time" +%s) fi wait_seconds=$((target_epoch - current_epoch)) echo "Test $test_id: Waiting $wait_seconds seconds until $(date -d @$target_epoch) to start..." sleep "$wait_seconds" # Step 4: Execute the test script with extracted parameters echo "Starting Test $test_id: ./execute.sh $jmx_file $csv1 $csv2 $csv3" # Uncomment the line below to run the actual command # ./execute.sh "$jmx_file" "$csv1" "$csv2" "$csv3" done
Key Breakdown of How It Works
- Extracting Test Count: We use
head -n 1to grab the first line of the CSV, thencut -d',' -f2to pull the second field (the test count), andxargsto strip any extra whitespace. - Reading Test Entries:
tail -n +3skips the first two lines (the count line and header row), thenhead -n "$total_tests"ensures we only process the number of tests specified. Thewhile IFS=',' readsplits each line into variables matching your CSV columns. - Handling Scheduled Time: We convert both current and target times to epoch seconds (total seconds since 1970) to calculate the wait duration. If the target time has already passed today, we automatically use the same time tomorrow to avoid immediate execution for overnight tests.
- Executing the Test: After waiting the required time, we call
execute.shwith the JMX and CSV file parameters extracted from the current row.
Important Notes
- Ensure your CSV has no unexpected spaces in fields (the
xargscalls clean up minor whitespace, but consistent formatting helps avoid issues). - Grant execute permissions to both this script and
execute.shwithchmod +x your_script.sh execute.sh. - If your system uses a non-UTC timezone, adjust the
datecommands to enforce UTC parsing (e.g.,date -d "$start_time UTC" +%s). - For long-running scripts, run it in the background with
nohup ./your_script.sh &to avoid interruptions if you log out, and add logging (e.g.,>> test_execution.log 2>&1at the end of the script) to track activity.
内容的提问来源于stack exchange,提问作者keshav goyal
相关产品推荐
相关产品推荐

