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

Bash脚本循环处理XML并调用Java传递内容故障求助

Hey there! Let's figure out why your Bash script is acting up and get it working properly. I’ll walk through common issues and share a robust version of the script that handles edge cases.

Common Troubleshooting & Fixed Script

1. First, Check the Basics

  • Directory Permissions: Make sure your script has read/write access to /source, /active, and /archive. Run ls -ld /source /active /archive to check permissions, and use chmod to adjust if needed (e.g., chmod 755 /source).
  • XML File Presence: Verify there are actual XML files in /source when the script runs. Add a quick check at the start: echo "Found $(ls /source/*.xml 2>/dev/null | wc -l) XML files in /source" to confirm.
  • Java Program Validity: Test your Java command manually first. For example, run java -jar YourProgram.jar "$(cat /active/test.xml)" in the terminal to make sure the Java app works as expected with XML input.

2. Robust Reference Script

Here’s an improved version of your script that handles failures, special characters in filenames, and empty directories:

#!/bin/bash

# Define directory paths
SOURCE_DIR="/source"
ACTIVE_DIR="/active"
ARCHIVE_DIR="/archive"

# Create directories if they don't exist
for dir in "$SOURCE_DIR" "$ACTIVE_DIR" "$ARCHIVE_DIR"; do
    if [ ! -d "$dir" ]; then
        mkdir -p "$dir"
        echo "Created missing directory: $dir"
    fi
done

# Process each XML file in /source
for xml_file in "$SOURCE_DIR"/*.xml; do
    # Skip if no XML files are found (avoids matching the literal "/*.xml")
    if [ ! -f "$xml_file" ]; then
        echo "No XML files found in $SOURCE_DIR"
        break
    fi

    filename=$(basename "$xml_file")
    active_path="$ACTIVE_DIR/$filename"
    archive_path="$ARCHIVE_DIR/$filename"

    # Move to active directory
    if mv "$xml_file" "$active_path"; then
        echo "Moved $filename to $ACTIVE_DIR"
    else
        echo "Failed to move $filename to $ACTIVE_DIR — skipping this file"
        continue
    fi

    # Read XML content and run Java program
    xml_content=$(cat "$active_path")
    # Replace this with your actual Java command
    if java -jar YourJavaApp.jar "$xml_content"; then
        echo "Successfully processed $filename with Java program"
    else
        echo "Java program failed to process $filename"
        # Optional: Move failed files to an error subdirectory instead of archiving
        # mkdir -p "$ARCHIVE_DIR/errors"
        # mv "$active_path" "$ARCHIVE_DIR/errors/$filename"
        # continue
    fi

    # Archive the file
    if mv "$active_path" "$archive_path"; then
        echo "Archived $filename to $ARCHIVE_DIR"
    else
        echo "Failed to archive $filename"
    fi
done

echo "Script finished running"

3. Key Improvements & Notes

  • Quoted Variables: All paths/filenames are wrapped in double quotes to handle spaces or special characters (like file name with spaces.xml).
  • Error Handling: Each critical step (move, Java call) checks for success and skips/fails gracefully instead of crashing the whole script.
  • Empty Directory Check: The script avoids trying to process a non-existent file when /source is empty.
  • Large XML Files: If your XML files are huge, passing the content as a parameter might hit shell limits. Instead, pass the file path to your Java program (e.g., java -jar YourApp.jar "$active_path") and let the Java app read the file directly—it’s more efficient.

4. Debugging Tips

If you still run into issues, add set -x at the very top of your script (right after #!/bin/bash). This enables debug mode, which prints every command the script runs—perfect for pinpointing exactly where things go wrong.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:16:10