编写Bash脚本遍历目录批量执行XSLT转换(Cygwin环境)
Hey there! I get that scripting can feel totally overwhelming when you're just starting out, so let's break this down step by step to get your batch XSLT conversion done smoothly in Cygwin.
First, Make Sure You Have xsltproc Installed
Before we dive in, we need to confirm xsltproc is available in your Cygwin environment. Open the Cygwin terminal and run:
xsltproc --version
If you get an error saying the command isn't found, fire up the Cygwin setup.exe, search for the libxslt package, and install it—this will add xsltproc to your system.
Create a Simple Batch Script
We'll write a bash script to automate finding all those metadata.xml files and converting them. Here's exactly what to do:
- Open the Cygwin terminal and navigate to the folder where your
translation.xsltis stored (this should also be the parent folder of yourdatadirectory, unless you want to adjust paths later). - Create a new script file using a beginner-friendly editor like
nano:nano convert_metadata.sh - Paste the following code into the editor:
#!/bin/bash # Update this path if your translation.xslt is in a different folder XSLT_FILE="translation.xslt" # Loop through every subfolder inside the "data" directory for dir in data/*/; do # Grab the name of the folder (without the full path) folder_name=$(basename "$dir") # Define the path to the metadata.xml file we want to process metadata_path="$dir/metadata.xml" # Check if the metadata.xml file exists before trying to convert it if [ -f "$metadata_path" ]; then echo "Processing $metadata_path..." # Run the XSLT conversion and save output as [folder_name].rdf xsltproc "$XSLT_FILE" "$metadata_path" > "${folder_name}.rdf" echo "Successfully created ${folder_name}.rdf!" else echo "Warning: $metadata_path doesn't exist — skipping this folder." fi done - Save the file by pressing
Ctrl+O, hit Enter to confirm the filename, then exitnanowithCtrl+X.
Make the Script Executable and Run It
Now we need to give the script permission to run. In the terminal, run:
chmod +x convert_metadata.sh
Then execute the script with:
./convert_metadata.sh
Quick Tips for Newbies
- If your
datafolder isn't in the same directory as the script, replacedata/*/with the full path to the data folder (e.g.,/home/yourusername/documents/data/*/). - If
translation.xsltis stored elsewhere, update theXSLT_FILEline to its full path (e.g.,/home/yourusername/stylesheets/translation.xslt). - All the generated
.rdffiles will save in the same directory where you run the script.
内容的提问来源于stack exchange,提问作者Joseph Shea

