如何在C Shell脚本中存储文件名?runner.csh使用场景问询
Got it, let's build exactly what you need—a C Shell script called runner.csh that reads filenames from your test list file (like list_of_tests.list) and executes commands with them. Here's a step-by-step breakdown with flexible options:
runner.csh Script First, write this script to handle basic test list processing. It includes error checking, skips empty lines/comments, and works with most filename cases:
#!/bin/csh # Check if we got the required test list file argument if ($#argv != 1) then echo "Usage: $0 <test_list_file>" echo "Example: $0 list_of_tests.list" exit 1 endif set test_list = $1 # Verify the test list file exists if (! -f "$test_list:q") then echo "Error: Test list file '$test_list:q' doesn't exist!" exit 1 endif # Loop through each filename in the list foreach test_file (`cat "$test_list:q"`) # Skip empty lines and lines starting with # (comments) set trimmed_file = "$test_file:q" if ($trimmed_file == "" || "$trimmed_file" =~ "#*") continue echo "Processing test file: $trimmed_file" # Replace the line below with your actual command # Example: Run a test executable on the file # ./my_test_runner "$trimmed_file" end
Key Details:
$test_file:qquotes the filename to handle spaces or special characters (super important if your test filenames have spaces!)- The script checks for valid input first, so you get clear error messages instead of silent failures
- It skips comment lines (starting with
#) and empty lines to keep things clean
Run this command in your terminal to give the script permission to run:
chmod +x runner.csh
Option 1: Hardcode Your Command
Edit the ./my_test_runner "$trimmed_file" line to match your actual command (e.g., gcc "$trimmed_file" -o test_output or ./run_test_suite "$trimmed_file"). Then run it with:
./runner.csh list_of_tests.list
Option 2: Pass the Command as an Argument (Flexible!)
If you want to use different commands without editing the script, modify runner.csh to accept the command as input:
#!/bin/csh # Check for enough arguments: test list + command + optional args if ($#argv < 2) then echo "Usage: $0 <test_list_file> <command> [command_args...]" echo "Example: $0 list_of_tests.list ./run_test.sh -verbose" exit 1 endif set test_list = $1 shift 1 # Move past the test list to capture the command and its args if (! -f "$test_list:q") then echo "Error: Test list file '$test_list:q' doesn't exist!" exit 1 endif foreach test_file (`cat "$test_list:q"`) set trimmed_file = "$test_file:q" if ($trimmed_file == "" || "$trimmed_file" =~ "#*") continue echo "Executing: $argv:q $trimmed_file" # Run the passed command with the test filename $argv:q "$trimmed_file" end
Now you can run it with any command, like:
./runner.csh list_of_tests.list ./my_test_tool -debug
Handling Filenames with Spaces (Advanced)
If your test filenames have spaces, the foreach loop might split them incorrectly. Use a while + read approach instead for better handling:
#!/bin/csh if ($#argv != 1) then echo "Usage: $0 <test_list_file>" exit 1 endif set test_list = $1 if (! -f "$test_list:q") then echo "Error: Test list file '$test_list:q' doesn't exist!" exit 1 endif # Use while read to preserve spaces in filenames cat "$test_list:q" | while read -r test_file set trimmed_file = "$test_file:q" if ($trimmed_file == "" || "$trimmed_file" =~ "#*") continue echo "Processing: $trimmed_file" ./my_test_runner "$trimmed_file" end
内容的提问来源于stack exchange,提问作者kindbit

